堆栈_顺序存储_数组单栈
#include<bits\stdc++.h>
using namespace std;
/* 数组单栈 */
#define MaxSize 100
typedef struct SNode *Stack;
struct SNode{
int Data[MaxSize];
int Top; /* 栈顶 */
};
/* 入栈 */
void Push(Stack PtrS,int item)
{
if(PtrS->Top==MaxSize-1)
{
cout<<"栈满"; return;
}
else
{
PtrS->Data[++(PtrS->Top)]=item; // ++在前面先+1再其余操作
return;
}
}
/* 出栈 */
int Pop(Stack PtrS)
{
if(PtrS->Top==-1)
{
cout<<"堆栈空";
return -1;
}
else
{
PtrS->Top--;
return PtrS->Data[PtrS->Top+1];
}
}
int main()
{
struct SNode s;
Stack S=&s; /* 与下一行 两种定义方式 */
// Stack S=(Stack)malloc(sizeof(struct SNode));
S->Top=-1; /* 初始化 */
int n,m,i;
cin>>n;
i=n;
while(n--)
{
cin>>m;
Push(S,m);
}
while(i--)
{
int k=Pop(S);
cout<<k<<endl;
}
return 0;
}