#include<stdio.h>
#include<stdlib.h>
//栈的顺序存储
#define MAXSIZE 100 //栈中元素的最大个数
typedef int SElemType; //健壮性
typedef struct {
SElemType data[MAXSIZE]; //存放栈中元素
int top; //栈顶指针,类似于数组下标
} SqStack;
//初始化
void Init(SqStack *S){
S->top = -1; //初始化栈顶指针
}
//判空
int Empty(SqStack *S){
if(S->top==-1)
return 1;
else
return 0;
}
//置空
void Destroy(SqStack *S){
S->top=-1;
}
//进栈
void Push(SqStack *S,SElemType e){
if(S->top == MAXSIZE - 1){//栈满 也可用top=Stacksize+1判断
printf("栈满");
return;
}
S->top++;//栈顶指针加一
S->data[S->top]=e;//e入栈
}
//出栈,删除栈顶元素
int Pop(SqStack *S){
if(S->top==-1){//栈空,无法删除
printf("栈空");
return 0;
}
int x=S->data[S->top];
S->top--;//先出栈,指针再减一
return 1;
}
//打印出链表
void put(SqStack *S){
int i;
for(i=0;i <= S->top;i++){
printf("%d ",S->data[i]);
}
printf("\n");
}
int main(){
SqStack S;
Init(&S);
Push(&S,1);//入栈
Push(&S,2);
Push(&S,3);
Push(&S,4);
put(&S);
Pop(&S);
put(&S);
}