//顺序栈的实现
#include<stdio.h>
#define Maxsize 200
typedef struct SqStack{
int data[Maxsize];
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 x){
if(S.top == Maxsize - 1)
return false;
S.data[++S.top]=x;
return true;
}
//出栈
int Pop(SqStack &S,int x){
if(S.top == -1)
return -1;
x=S.data[S.top];
return x;
}
//读栈顶元素
bool GetTop(SqStack S,int &x){
if(S.top==-1)
return false;
x = S.data[S.top];
return true;
}
int main(){
SqStack stack;
InitStack(stack);
int x,j,result;
bool empty;
printf("(入栈操作)请输入要入栈元素的个数:\n");
scanf("%d",&j);
printf("请输入入栈元素:\n");
for(int i = 0;i < j;i++){
scanf("%d",&x);
Push(stack,x);
}
GetTop(stack,result);
printf("栈顶元素是:\t%d\n",result);
empty = StackEmpty(stack);
if(empty)
printf("当前栈为空!\n");
else
printf("当前栈不为空\n");
printf("出栈结果如下:\n");
printf("\t┌───────────────┐\n");
while(stack.top != -1){
result = Pop(stack,result);
printf("\t│\t%d\t│\n",result);
if(stack.top!=0)
printf("\t├───────────────┤\n");
else
printf("\t└───────────────┘\n");
stack.top--;
}
empty = StackEmpty(stack);
if(empty)
printf("当前栈为空!\n");
else
printf("当前栈不为空\n");
return 0;
}