#include<stdio.h>
#include<stdlib.h>
#define max 1000
typedef struct
{
int elem[max];
int top;
}SeqStack;
//置空栈
SeqStack *InitStack(){
SeqStack *s;
s=(SeqStack *)malloc(sizeof(SeqStack));
s->top=-1;
return s;
}
//判空栈
int Empty(SeqStack *s){
if(s->top==-1) return 1;
return 0;
}
//入栈
int Push(SeqStack *s,int x){
if(s->top==max-1) return 0;
else{
s->top++;
s->elem[s->top]=x;
return 1;
}
}
//出栈
int Pop(SeqStack *s,int *x){
if(Empty(s)) return 0;
else{
*x=s->elem[s->top];
s->top--;
return 1;
}
}
//取栈顶元素
int GetTop(SeqStack *s){
if(Empty(s)) return 0;
else return (s->elem[s->top]);
}
main(){
SeqStack *q;
q=InitStack();;
int a;
scanf("%d",&a);
while(a != -1){
Push(q,a);
scanf("%d",&a);
}
int b=GetTop(q);
printf("栈顶元素是:%d\n",b);
int y;
Pop(q,&y);
printf("出栈的数据元素是:%d\n",y);
int c=GetTop(q);
printf("此时栈顶元素是:%d\n",c);
}
C语言顺序栈的基本操作
最新推荐文章于 2022-10-12 20:14:16 发布