#include <stdio.h>
#define M 50
typedef struct node{
int data[M];
int top;
}SqStack;
void InitStack(SqStack &S){
S.top=-1;
}
bool StackEmpty(SqStack &S){
if(S.top==-1){
return true;
}
else{
return false;
}
}
bool Push(SqStack &S,int e){
if(S.top==M-1){
return false;
}
S.data[++S.top]=e;
return true;
}
bool Pop(SqStack &S,int &x){
if(S.top==-1){
return false;
}
x=S.data[S.top--];
return true;
}
bool GetTop(SqStack &S,int &x){
if(S.top==-1){
return false;
}
x=S.data[S.top];
}
void test(){
SqStack S;
InitStack(S);
if(StackEmpty(S)){
printf("栈空\n");
}
else{
printf("栈非空\n");
}
Push(S,1);
Push(S,2);
Push(S,3);
StackEmpty(S);
int a;
Pop(S,a);
GetTop(S,a);
printf("栈顶元素: %d",a);
}
int main() {
test();
return 0;
}
|