初学顺序栈
大体结构懂了,但是在我输出栈顶元素的时候输出始终为 1 ,不知道哪里出问题了,求解
感谢大佬们解答!
#include<iostream>
#define MAXSIZE 100
#define OVERFLOW -1
#define ERROR 0
#define OK 1
using namespace std;
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;//定义
bool InitStack(SqStack &s)
{
s.base=new int[MAXSIZE];
if(!s.base) exit (OVERFLOW);
s.top=s.base;
s.stacksize=MAXSIZE;
return OK;
}//创建
bool Push(SqStack &s,int e)
{
if(s.top-s.base==s.stacksize) return ERROR;
*s.top++=e;
return OK;
}//入栈
bool Pop(SqStack &s,int &e)
{
if(s.top==s.base)
return ERROR;
s.top=s.top-1;
e=*s.top;
return OK;
}//出栈
bool GetTop(SqStack s,int &k)
{
if(s.top==s.base)
return ERROR;
k=*(s.top-1);
return k;
}//取栈顶
int main()
{
SqStack s;
int e,j=1,i=1,k;
InitStack(s);
cin>>e;
while(e!=-1)
{
Push(s,e);
cin>>e;
j++;
}
e=GetTop(s,k);
cout<<"栈顶"<<e<<endl;
cout<<"栈数"<<j-1<<endl;
for(;j>1;j--){
Pop(s,e);
cout<<"取栈第"<<j-1<<"个为:"<<e<<endl;
}
return 0;
}