#include<iostream>
using namespace std;
#define maxsize 100
typedef struct{
int top;
int data[maxsize];}Sqstack;
Sqstack SqstackSet() //构造一个空栈
{
Sqstack S;
S.top=-1;
return S;
}
int SqstackEmpty(Sqstack S) //空栈判断
{
if(S.top==-1)
return 1;
else return 0;
}
Sqstack SqstackPush(Sqstack &S,int x) //入栈
{
if(S.top==maxsize-1)
{cout<<"栈满"<<endl;
return S;}
S.top++;
S.data[S.top]=x;
return S;
}
Sqstack SqstackPop(Sqstack &S,int *x) //出栈 该函数可以起到弹栈和显示栈顶的作用
{
if(SqstackEmpty(S)) //加一个以防外部忘记判断是否为空发生错误
{cout<<"栈空"<<endl;return S;}
*x=S.data[S.top];
S.top--;
return S;
}
int SqstackGettop(Sqstack S) //该函数仅显示栈顶
{
return S.data[S.top];
}
int main()
{ Sqstack m=SqstackSet();
cout<<"已构造一个空顺序栈,数据容量为100整型数据"<<endl;
int c,p;
cout<<"请输入数据 0结束"<<endl;
while(cin>>c&&c!=0)
{
SqstackPush(m,c);
}
cout<<"栈输入完成,现在开始依次弹出栈顶元素"<<endl;
while(!SqstackEmpty(m))
{
SqstackPop(m,&p);
cout<<p<<" ";
}
return 0;
}
顺序栈
最新推荐文章于 2020-11-18 21:04:19 发布