#include<iostream>
using namespace std;
typedef int SElemType; //取别名
#define ERROR 0
#define OK 1
#define MAXSIZE 100
//定义
typedef struct StackNode{
SElemType data;
struct StackNode *next;
}StackNode,*LinkStack;
//初始化
int InitStack(LinkStack &S){
S=NULL;
return OK;
}
//判空
bool StackEmpty(LinkStack S){
if(S==NULL)
return true;
else
return false;
}
//入栈
int Push(LinkStack &S,SElemType e){
LinkStack p;
p=new StackNode;
p->data=e;
p->next=S;
S=p;
return OK;
}
//出栈
int Pop(LinkStack &S,SElemType &e){
if(S==NULL)//栈空
return ERROR;
LinkStack p;
e=S->data;
p=S;
S=S->next;
delete p;
return OK;
}
//取栈顶元素
SElemType GetTop(LinkStack &S){
if(S!=NULL)
return S->data;
return ERROR;
}
int main()
{
LinkStack s;
bool flag;
SElemType e;
//初始化
InitStack(s);
//判空
flag=StackEmpty(s);
if(flag)
cout<<"栈空"<<endl;
else
cout<<"栈非空"<<endl;
//入栈
cout<<"请填写入栈的数据:"<<endl;
cin>>e;
while(e!=-9999){
Push(s,e);
cout<<"请填写入栈的数据:"<<endl;
cin>>e;
}
//取栈顶元素
e=GetTop(s);
if(e==0)
cout<<"没有栈顶元素:"<<endl;
cout<<"栈顶元素为:"<<e<<endl;
//出栈
while(!StackEmpty(s)){
Pop(s,e);
cout<<e<<" ";
}
return 0;
}