資源描述:
《順序棧的創(chuàng)建進棧與出棧》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、#include#include#include#defineMAXNUM100typedefdoubleElemtype;/*定義順序棧的存儲結(jié)構(gòu)*/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]);}/*主函數(shù)*/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;}