欢迎来到天天文库
浏览记录
ID:38111274
大小:225.50 KB
页数:5页
时间:2019-05-25
《栈的相关操作》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、/*栈的相关操作*/#include#definemaxsize64#definen10typedefintdatatype;typedefstruct{datatypedata[maxsize];inttop;}seqstack;seqstack*s;/*置空栈*/SETNULL(seqstack*s){s->top=-1;printf("Setnullover!");}/*判栈空*/intEMPTY(s)seqstack*s;{if(s->top==-1){return1;}else{return0;}}/*进栈*/seqstac
2、k*PUSH(s,x)seqstack*s;datatypex;{inta[n];inti;for(i=0;itop==maxsize-1){printf("overflow");returnNULL;}else{s->top++;s->data[s->top]=x;printf("%d",s->data[s->top]);}}returns;}/*退栈*/datatypePOP(s)seqstack*s;{if(EMPTY(s))printf("underflow");el
3、se{s->top--;printf("%d",s->data[s->top+1]);}}/*取栈顶*/datatypeTOP(s)seqstack*s;{if(EMPTY(s)){printf("stackisempty");returnNULL;}elseprintf("%d",s->data[s->top]);}/*主函数*/voidmain(){intc;do{printf("*******************************************************************************");p
4、rintf("ttttZhanxiangguancaozuo");printf("-------------------------------------------------------------------------------");printf("tt1t2t3t4t5t6");printf("-------------------------------------------------------------------------------");printf("ttSetnullEmpt
5、ytPushtPoptToptExit");printf("*******************************************************************************");printf("Pleaseenteryourchoice:");scanf("%d",&c);switch(c){case1:{SETNULL(s);printf("Continue");}break;case2:{EMPTY(s);if(EMPTY(s))printf("Thestackisempty!"
6、);elseprintf("Thestackisnotempty!");printf("Continue");}break;case3:{datatypex;printf("Pleaseinputyourdatas:");PUSH(s,x);printf("Continue");}break;case4:{POP(s);printf("Continue");}break;case5:{TOP(s);printf("Continue");}break;case6:exit(0);break;}}while(c!=6);
7、getch();}运行结果为:进栈:退栈:取栈顶:置空栈:判栈空:
此文档下载收益归作者所有