/*堆栈的顺序存储实现与测试*/
#include<iostream>
using namespace std;
#define ERROR -100 /*定义函数出错时的返回值*/
#define maxsize 100/*定义数组空间最大值*/
typedef int ElementType;
typedef struct SNode *Duizhan;/*定义间接访问堆栈的数据类型*/
/*定义堆栈结构体*/
struct SNode{
ElementType Data[maxsize];/*定义数组空间*/
int top;/*栈顶指针,指向栈顶元素的数组下标*/
};
/*入栈,给定一个堆栈,把一个元素压入栈顶*/
void Push(Duizhan Ptrs,ElementType item){
if(Ptrs->top==maxsize-1){
cout<<"full"<<endl;
return;
}/*栈满*/
Ptrs->Data[++(Ptrs->top)]=item;/**/
return;
}
/*出栈,指定堆栈栈顶元素出栈,并返回该元素值*/
ElementType Pop(Duizhan Ptrs){
if(Ptrs->top==-1){
cout<<"empty"<<endl;
return ERROR;
}/*空栈*/
return Ptrs->Data[(Ptrs->top)--];
}
int main(){
Duizhan S;/*创建一个堆栈*/
S=(Duizhan)malloc(sizeof(struct SNode));/*分配内存空间*/
S->top=-1;/*初始化,设置为一个空栈*/
int v[3];
int k,n;
for(k=0;k<3;k++){
cin>>v[k];
Push(S,v[k]);
}/*依次将三个元素压入栈顶*/
n=Pop(S);
cout<<n<<endl;
return 0;
}
#include<iostream>
using namespace std;
#define ERROR -100 /*定义函数出错时的返回值*/
#define maxsize 100/*定义数组空间最大值*/
typedef int ElementType;
typedef struct SNode *Duizhan;/*定义间接访问堆栈的数据类型*/
/*定义堆栈结构体*/
struct SNode{
ElementType Data[maxsize];/*定义数组空间*/
int top;/*栈顶指针,指向栈顶元素的数组下标*/
};
/*入栈,给定一个堆栈,把一个元素压入栈顶*/
void Push(Duizhan Ptrs,ElementType item){
if(Ptrs->top==maxsize-1){
cout<<"full"<<endl;
return;
}/*栈满*/
Ptrs->Data[++(Ptrs->top)]=item;/**/
return;
}
/*出栈,指定堆栈栈顶元素出栈,并返回该元素值*/
ElementType Pop(Duizhan Ptrs){
if(Ptrs->top==-1){
cout<<"empty"<<endl;
return ERROR;
}/*空栈*/
return Ptrs->Data[(Ptrs->top)--];
}
int main(){
Duizhan S;/*创建一个堆栈*/
S=(Duizhan)malloc(sizeof(struct SNode));/*分配内存空间*/
S->top=-1;/*初始化,设置为一个空栈*/
int v[3];
int k,n;
for(k=0;k<3;k++){
cin>>v[k];
Push(S,v[k]);
}/*依次将三个元素压入栈顶*/
n=Pop(S);
cout<<n<<endl;
return 0;
}