#include <stdio.h>
#include <stdlib.h>
#define ElemType int
#define MaxSize 50
typedef struct{
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack *s){//初始化栈
s->top=-1;
}
void StackEmpty(SqStack s){//判断栈是否为空
if(s.top==-1){
printf("栈空\n");
}
}
void Push(SqStack *s){//进栈
ElemType x;
if(s->top==MaxSize-1){
printf("栈满\n");
}
printf("请依次输入你要进栈的元素:");
scanf("%d",&x);
while(x !=9999){
s->data[++s->top]=x;
scanf("%d",&x);
}
}
void Pop(SqStack *s,int i){//出栈
ElemType x;
if(s->top==-1){
printf("栈空\n");
}
printf("你出栈的元素依次为:");
for(int j=1;j<=i;j++){
x=s->data[s->top--];
printf("%5d",x);
}
printf("\n");
}
void PrintStack_Push(SqStack s){//进栈后遍历
if(s.top==-1){
printf("栈空\n");
}
printf("进栈后自底向上遍历栈:");
for(int i=0;i<=s.top;i++){
printf("%5d",s.data[i]);
}
printf("\n");
}
void PrintStack_Pop(SqStack s){//出栈后遍历
if(s.top==-1){
printf("栈空\n");
}
printf("出栈后自底向上遍历栈:");
for(int i=0;i<=s.top;i++){
printf("%5d",s.data[i]);
}
printf("\n");
}
int GetTop(SqStack s){//读取栈顶元素
ElemType x;
if(s.top==-1){
printf("栈空\n");
}
x=s.data[s.top];
return x;
}
int main(){
SqStack s;
InitStack(&s);
StackEmpty(s);
Push(&s);
PrintStack_Push(s);
Pop(&s,3);
PrintStack_Pop(s);
printf("%d",GetTop(s));
}