1.顺序栈
#define MaxSize 50
typedef struct{
Elemtype data;
int top;
}SqStack;
2.链栈(使用时一般不用头结点)
typedef struct Linknode{
ElemType data;
struct Linknode *next;
} *LinkStack;
以下操作均为顺序栈
3.初始化
void InitStack(SqStack &s)
{
s.top=-1;
}
4.判空
bool StackEmpty(SqStack S)
{
if(S.top==-1)
return true;
else
return false;
}
5.进栈
bool Push(SqStack &S,ElemType x)
{
if(S.top==MaxSize-1)
return false;
S.data[++top]=x;
return true;
}
6.出栈
bool Pop(SqStack &S,ElemType &x)
{
if(S.top==-1)
return false;
x=S.data[top--];
return true;
}
7.读取栈顶元素
bool GetTop(SqStack S,ElemType &x)
{
if(S.top==-1)
return false;
x=S.data[top];
return true;
}