#include "stdio.h"
#include "malloc.h"
#define MAXSIZE 100
typedef int datatype;
#define NULL 0
typedef struct{
datatype stack[MAXSIZE];
int top;
}seqstack;
seqstack * InitStack(seqstack *s){
s=(seqstack *)malloc(sizeof(seqstack));
if(s!=NULL){
s->top=-1;
return s;
}else{
printf("分配内存失败!");
return NULL;
}
}
int IsEmpty(seqstack *s){
if(s->top<0){
return 1;
}else{
return 0;
}
}
int Push(seqstack *s,datatype e){
if(s->top==MAXSIZE-1){
printf("占满溢出错误!\n");
return 0;
}else{
s->top++;
s->stack[s->top]=e;
return s->top;
}
}
//出栈
datatype Pop(seqstack *s){
datatype x;
if(IsEmpty(s)){
printf("下溢出错误!");
return 0;
}else{
x=s->stack[s->top];
s->top--;//先->再--
return x;
}
}
//取栈顶
datatype GetPop(seqstack *s){
if(IsEmpty(s)){
printf("栈是空的");
return 0;
}else{
return s->stack[s->top];
}
}
main(){
int i;
seqstack *s;
s=InitStack(s);
for(i=0;i<50;i++){
Push(s,i+1);
}
printf("%d\n",GetPop(s));//50
for(i=0;i<50;i++){
printf("%3d",Pop(s));
}
}
栈
最新推荐文章于 2018-09-15 21:29:28 发布