首页 7、顺序栈的基本操作

7、顺序栈的基本操作

举报
开通vip

7、顺序栈的基本操作7、顺序栈的基本操作 #include #include #define OK 1 #define ERROR 0 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int SElemType; typedef int Status; struct SqStack { SElemType *base; SElemType *top; int stacksize; }; Status InitStack(SqStac...

7、顺序栈的基本操作
7、顺序栈的基本操作 #include #include #define OK 1 #define ERROR 0 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int SElemType; typedef int Status; struct SqStack { SElemType *base; SElemType *top; int stacksize; }; Status InitStack(SqStack &S) { S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); S.stacksize=STACK_INIT_SIZE; S.top=S.base; return OK; } Status Push(SqStack &S,SElemType e) { if(S.top-S.base>=S.stacksize) { S.base=(SElemType *)realloc(S.base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(SElemType)); S.top=S.base+S.stacksize; S.stacksize=STACK_INIT_SIZE+STACKINCREMENT; } *S.top++=e; return OK; } Status Pop(SqStack &S,SElemType &e) { if(S.top!=S.base) { e=*--S.top; return OK; } else return ERROR; } Status GetTop(SqStack S,SElemType &e) { if(S.top!=S.base) { e=*(S.top-1); return OK; } else return ERROR; } int StackLength(SqStack S) { return (S.top-S.base); } Status StackTraverse(SqStack S) { SElemType *p=(SElemType *)malloc(sizeof(SElemType)); p = S.top-1; if(S.top==S.base) printf("The Stack is Empty!"); else { printf("The Stack is: "); while(p>=S.base) { printf("%d ", *p); p--; } } printf("\n"); return OK; } int main() { int a; SqStack S; SElemType x, e; if(InitStack(S)) { printf("A Stack Has Created.\n"); } while(1) { printf("1:Push \n2:Pop \n3:Get the Top \n4:Return the Length of the Stack\n5:Load the Stack\n0:Exit\nPlease choose:\n"); scanf("%d",&a); switch(a) { case 1: scanf("%d", &x); if(!Push(S,x)) printf("Push Error!\n"); else printf("The Element %d is Successfully Pushed!\n", x); break; case 2: if(!Pop(S,e)) printf("Pop Error!\n"); else printf("The Element %d is Successfully Poped!\n", e); break; case 3: if(!GetTop(S,e))printf("Get Top Error!\n"); else printf("The Top Element is %d!\n", e); break; case 4: printf("The Length of the Stack is %d!\n",StackLength(S)); break; case 5: StackTraverse(S); break; case 0: return 1; } } }
本文档为【7、顺序栈的基本操作】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_729658
暂无简介~
格式:doc
大小:16KB
软件:Word
页数:5
分类:互联网
上传时间:2017-09-28
浏览量:310