#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
#define INIT_SIZE 100 //存储空间初始分配量
#define INCREMENT 10 //存储空间分配增量
typedef struct{
SElemType* base;
SElemType* top;
int stackSize;
}Stack; //栈类型
Status InitStack(Stack &S){ //构造空栈S
S.base = (SElemType*)malloc(INIT_SIZE *sizeof(SElemType));
if (!S.base)
exit(OVERFLOW);//存储分配失败
S.top = S.base;
S.stackSize = INIT_SIZE;
return OK;
}//InitStack
Status StackEmpty(Stack S){
//若s为空返回TRUE,否则返回FALSE
if (S.top == S.base)
return TRUE;
return FALSE;
}//StackEmpty
Status Push(Stack &S, SElemType e){
//插入元素e为新的栈顶元素
if (S.top - S.base >= S.stackSize){//栈满,加空间
S.base = (SElemType *)realloc(S.base, (S.stackSize + INCREMENT)*sizeof(SElemType));
if (!S.base)
exit(OVERFLOW); //存储分配失败
S.top = S.base + S.stackSize;
S.stackSize += INCREMENT;
}
*S.top++ = e;
return OK;
}//push
Status Pop(Stack &S, SElemType &e){//若栈不空删除栈顶元素用e返回并返回OK,否则返回ERROR
if (S.top == S.base)
return ERROR;
e = *--S.top;
return OK;
}//Pop