#include <stdio.h>
#include <stdlib.h>
#define MAX 20
typedef struct{
int arr[MAX];
int top;
}SeqStack;
SeqStack *Init(SeqStack *s)
{
s=(SeqStack *)malloc(sizeof(SeqStack));
s->top=-1;
return s;
}
int Push(SeqStack *s,int x)
{
int index=s->top+1;
int i;
if(index>=MAX-1) return 0;
else{
s->top++;
printf("入栈数据为:");
s->arr[s->top]=x;
return 1;}
}
Pop(SeqStack *s,int *x)
{
printf("Top指向%d\n",s->top);
int i;
if(s->top==-1) return 0;
else{
*x=s->arr[s->top];
printf("弹栈数据为:");
printf("%d",s->arr[s->top]);
s->top--;}
}
Print(SeqStack *s)
{
printf("Top指向%d\n",s->top);
int index=s->top;
int i;
if(s->top==-1) return 0;
for(i=index;i>-1;i--)
{
printf("第%d个数据为:",i+1);
printf("%d\n",s->arr[i]);
}
}
Give(SeqStack *s)
{
if(s->top==-1)return 0;
else return(printf("栈顶元素为%d",s->arr[s->top]));
}
int main()
{
int x,i;
SeqStack *p;
SeqStack *a;
a=Init(p);
printf("入栈数据为:");
for(i=0;i<5;i++)
{
scanf("%d",&x);
Push(a,x);
}
Print(a);
Pop(a,&x);
Print(a);
Give(a);
}