#include <iostream>
using namespace std;
/*顺序栈的定义*/
#define Stack_Size 100
typedef struct sqStack
{
char *elem;
int top;
int stackSize;//栈数组长度
}sqStack;
/*顺序栈的初始化*/
void initStack_Sq(sqStack &S)
{
S.elem=new char[Stack_Size];
S.top=-1;
S.stackSize=Stack_Size;
}
/*建立顺序栈*/
void creatStack_Sq(sqStack &S,int n)
{
initStack_Sq(S);//在这里忘了初始化栈,导致编译的时候出现错误。
for(int i=0;i<n;i++)
{
cin>>S.elem[i];
S.top++;
}
}
/*销毁顺序栈*/
void destroyStack_Sq(sqStack S)
{
delete []S.elem;
S.top=-1;
S.stackSize=0;
}
/*入栈*/
void push(sqStack &S,char x)
{
if(S.top==Stack_Size-1)
cout<<"Stack Overflow!";
S.elem[++S.top]=x;
}
/*出栈*/
char pop(sqStack &S)
{
char x;
if(S.top==-1)
cout<<"Stack Empty!";
x=S.elem[S.top--];
return x;
}
void main()
{
sqStack S;
creatStack_Sq(S,5);
push(S,'a');
cout<<pop(S)<<endl;
destroyStack_Sq(S);
}
标签:出栈,入栈,sqStack,int,top,C++,Stack,void,Sq From: https://blog.51cto.com/u_5173797/7251988