一、顺序栈的实现
#define MaxSize 50
typedef struct{
Elemtype data[MaxSize];
int top;
} SqStack;
二、顺序栈的基本运算
1. 初始化
void InitStack(SqStack &S){
S.top = -1;
}
2. 栈判空
bool StackEmpty(SqStack S){
if(S.top == -1)
return true;
else
return false;
}
3. 进栈
bool Push(SqStack &S, ElemType x){
if(S.top == MaxSize - 1)
return false;
S.data[++S.top] = x;
return true;
}
4. 出栈
bool Pop(SqStack &S, ElemType &x){
if(S.top == -1)
return false;
x = S.data[S.top--];
return true;
}
5. 读栈顶元素
bool GetTop(SqStack S, ElemTpye &x){
if(S.top == -1)
return false;
x = S.data[S.top];
return true;
}
三、栈的链式存储结构
typedef struct Linknode{
ElemType data;
struct Linknode *next;
}LiStack;