#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語言順序棧的基本操作
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.