欢迎来到天天文库
浏览记录
ID:34562771
大小:33.00 KB
页数:4页
时间:2019-03-08
《顺序栈的创建进栈与出栈》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#include#include#defineMAXNUM100typedefdoubleElemtype;/*定义顺序栈的存储结构*/typedefstruct{Elemtypestack[MAXNUM];inttop;}SqStack;/*初始化顺序栈*/voidInitStack(SqStack*p){p->top=-1;if(!p)printf("error");p->top=-1;}/*入栈*/voidPush(SqStack*p,
2、Elemtypex){if(p->toptop++;p->stack[p->top]=x;}else{printf("Overflow!");}}/*出栈*/ElemtypePop(SqStack*p){Elemtypex;if(p->top!=-1){x=p->stack[p->top];printf("%lf",p->stack[p->top]);p->top--;return(x);}else{printf("Underflow!");return(0);}}/*遍历顺序栈
3、*/voidOutStack(SqStack*p){chari;printf("");if(p->top<0)printf("ThisisaEmptyStack!");for(i=p->top;i>=0;i--)printf("%lf",p->stack[i]);}/*主函数*/intmain(){SqStack*q;q=(SqStack*)malloc(sizeof(SqStack));InitStack(q);intcord;doubley;inti,n;Elemtypea;do{printf("*
4、*************Menu**************");printf("*1InitStack*");printf("*2Push*");printf("*3Pop*");printf("*4PrintStack*");printf("**************************************");printf("Choose(1234):");scanf("%d",&cord);printf("");switch(cord){case1
5、:{q=(SqStack*)malloc(sizeof(SqStack));InitStack(q);OutStack(q);}break;case2:{printf("你想压几个元素进栈:");scanf("%d",&n);printf("输入这%d个元素:",n);for(i=1;i<=n;i++){scanf("%lf",&a);Push(q,a);}OutStack(q);}printf("PushSuccessfully!!!");break;case3:{printf("你想几个元素出栈:"
6、);scanf("%d",&n);printf("出栈的%d个元素为:",n);while(n--){Pop(q);}}break;case4:printf("栈中元素分别为:");OutStack(q);break;default:exit(0);}}while(cord<=4);return0;}
此文档下载收益归作者所有