#include
#include
#define MAXSZIE 100
typedef int ElemType;
typedef struct SqStack{ElemType *top;ElemType *base;int sizestack;
}SqStack;
SqStack *InitStack(SqStack &s)
{s.base=(int*)malloc(sizeof(int)*MAXSZIE);if(!s.base)return 0;s.top=s.base;s.sizestack=MAXSZIE;return 0;
}
void pushStack(SqStack &s)
{int e;scanf("%d",&e);if(s.top-s.base==s.sizestack) printf("内存已满,无法继续压栈!");else*s.top++=e;
}
void DisplayStack(SqStack &s)
{if(s.base==s.top)printf("栈空!");int *p;p=s.top;while(p>s.base){p--;printf("%d->",*p);}
}
int popStack(SqStack &s,ElemType &e)
{if(s.base==s.top){printf("栈已空!");}e=*(--s.top); return e;
}
void CleanStack(SqStack &s)
{if(s.base==s.top)printf("栈已空!");s.base=s.top;
}
void destory_stack(SqStack &s)
{if(s.base){free(s.base);s.base=NULL;s.top=NULL;s.sizestack=0;}
}
int GetTop(SqStack s,ElemType &e)
{if(s.base==s.top)printf("栈已空!");e=*(--s.top);return e;
}
int StackEmpty(SqStack s)
{if(s.base==s.top)return 1;elsereturn 0;
}
int main()
{SqStack s;InitStack(s);int n;printf("你要输入多少数据:");scanf("%d",&n);for(int i=1;i<=n;i++){printf("请输入第%d个压栈数据:",i);pushStack(s);}printf("该栈顶元素为:");int v;GetTop(s,v);printf("%d",v);printf("\n");int x,y;printf("请输入出栈数据个数:");scanf("%d",&x);for(int i=1;i<=x;i++){popStack(s,y);printf("%d->",y);}printf("\n");printf("余下数据如下\n");DisplayStack(s);CleanStack(s);printf("\n清栈后,栈空否:%d(1:空 0:否)",StackEmpty(s));destory_stack(s);
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!