1、基本操作
InitStack(&S) //初始化一个空栈S
StackEmpty(S) //判断是否为空,空返回true
Push(&S,x) //进栈
Pop(&S,&x) //出栈,弹出栈顶元素,并用x返回
GetTop(S,&x) //读栈顶元素,用x返回
DestroyStack(&S) //销毁栈,释放空间
2、结构
顺序栈
typedef struct{
ElemType data[MaxSize];
int top;
}SqStack;
链式栈
typdef struct LinkNode{
ElemType data;
struct LinkNode *next;
}*LiStack;
3、基本操作
(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,ElemType &x){
if(S.top=-1) //栈空,报错
return false;
x=S.data[S.top]; //读取栈顶
return true;