顺序栈的简单操作
根据老师的代码进行改编
#include<iostream>
#include<cstdlib>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S){ //建立一个空栈
S.base=new ElemType[MAXSIZE];
if(!S.base)
exit(OVERFLOW);
S.top=S.base;
S.stacksize=MAXSIZE;
return OK;
}
Status Push(SqStack &S,ElemType &e){ //将数字压入空栈
if(S.top-S.base==MAXSIZE)
return ERROR;
*(S.top++)=e;
return OK;
}
Status Pop(SqStack &S,ElemType &e){//将栈顶元素删除
if(S.top==S.base)
return ERROR;
e=*(--S.top);
return OK;
}
Status GetTop(SqStack &S,ElemType &e){//获取栈顶元素
if(S.top==S.base)
return ERROR;
e=*(S.top-1);
return OK;
}
int main(){
SqStack s;
ElemType n;
ElemType m;
int i,j,k,t;
cout<<"进栈的次序依次为:"<<endl;
if(InitStack(s)==OK)
for(i=1;i<=12;i++){
Push(s,i);
cout<<i<<" ";
}
cout<<"栈顶出栈次序依次为:"<<endl;
while(GetTop(s,n)==OK){
cout<<n<<" ";
Pop(s,m);
}
return 0;
}

浙公网安备 33010602011771号