/**
* @author huihut
* @E-mail:huihut@outlook.com
* @version 201699
*
*
*/
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
//5
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
//
#define LONGTH 5
//
typedef int Status;
typedef int ElemType;
//
typedef struct {
ElemType *elem;
int top;
int size;
int increment;
} SqSrack;
//
Status InitStack_Sq(SqSrack &S, int size, int inc); //
Status DestroyStack_Sq(SqSrack &S); //
Status StackEmpty_Sq(SqSrack S); //STRUEFALSE
void ClearStack_Sq(SqSrack &S); //S
Status Push_Sq(SqSrack &S, ElemType e); //eS
Status Pop_Sq(SqSrack &S, ElemType &e); //Se
Status GetTop_Sq(SqSrack S, ElemType &e); //Se
//
Status InitStack_Sq(SqSrack &S, int size, int inc) {
S.elem = (ElemType *)malloc(size * sizeof(ElemType));
if (NULL == S.elem) return OVERFLOW;
S.top = 0;
S.size = size;
S.increment = inc;
return OK;
}
//
Status DestroyStack_Sq(SqSrack &S) {
free(S.elem);
S.elem = NULL;
return OK;
}
//STRUEFALSE
Status StackEmpty_Sq(SqSrack S) {
if (0 == S.top) return TRUE;
return FALSE;
}
//S
void ClearStack_Sq(SqSrack &S) {
if (0 == S.top) return;
S.size = 0;
S.top = 0;
}
//eS
Status Push_Sq(SqSrack &S, ElemType e) {
ElemType *newbase;
if (S.top >= S.size) {
newbase = (ElemType *)realloc(S.elem, (S.size + S.increment) * sizeof(ElemType));
if (NULL == newbase) return OVERFLOW;
S.elem = newbase;
S.size += S.increment;
}
S.elem[S.top++] = e;
return OK;
}
//Se
Status GetTop_Sq(SqSrack S, ElemType &e) {
if (0 == S.top) return ERROR;
e = S.elem[S.top - 1];
return e;
}
//Se
Status Pop_Sq(SqSrack &S, ElemType &e) {
if (0 == S.top) return ERROR;
e = S.elem[S.top - 1];
S.top--;
return e;
}
int main() {
//S
SqSrack S;
//
int size, increment, i;
//
size = LONGTH;
increment = LONGTH;
ElemType e, eArray[LONGTH] = { 1, 2, 3, 4, 5 };
//
printf("------\n");
printf("Ssize%d\nSincrement%d\n", size, increment);
printf("\n");
for (i = 0; i < LONGTH; i++) {
printf("%d\t", eArray[i]);
}
printf("\n");
//
if (!InitStack_Sq(S, size, increment)) {
printf("\n");
exit(0);
}
printf("\n");
//
for (i = 0; i < S.size; i++) {
if (!Push_Sq(S, eArray[i])) {
printf("%d\n", eArray[i]);
exit(0);
}
}
printf("\n");
//
if(StackEmpty_Sq(S)) printf("S\n");
else printf("S\n");
//S
printf("S\n");
printf("%d\n", GetTop_Sq(S, e));
//S
printf("S\n");
for (i = 0, e = 0; i < S.size; i++) {
printf("%d\t", Pop_Sq(S, e));
}
printf("\n");
//S
ClearStack_Sq(S);
printf("S\n");
return 0;
}