/* 顺序栈 */
#define MaxSize 50
typedef struct
{
ElemType data[MaxSize];
int top;
} SqStack;
// 栈顶指针:S.top,初始时设置S.top=-1;栈顶元素:S.data[S.top]
// 进栈操作:栈不满时,栈顶指针先加1,再送值到栈顶元素
// 出栈操作:栈非空时,先取栈顶元素值,再将栈顶指针减1
// 栈空条件:S.top==-1;栈满条件:S.top==MaxSize-1;栈长:S.top+1
// 初始化
void InitStack(SqStack &S){
S.top = -1;
}
// 判断空
void StackEmpty(SqStack &S){
return S.top == -1 ? true : false;
}
// 进栈
bool Push(SqStack &S, ElemType x){
if(S.top == MaxSize - 1)
return false;
S.data[++S.top] = x;
return true;
}
// 出栈
bool Pop(SqStack &S, ElemType &x){
if(S.top == -1)
return false;
x = S.data[S.top--];
return true;
}
// 读取栈顶元素
bool GetTop(SqStack S, ElemType &x){
if(S.top == -1)
return fasle;
x = S.data[S.top];
return true;
}
/* 链式栈 */
typedef struct Linknode{
ElemType data;
struct Linknode *next;
} *LiStack;