C语言数据结构之栈的基本操作.doc_第1页
C语言数据结构之栈的基本操作.doc_第2页
C语言数据结构之栈的基本操作.doc_第3页
C语言数据结构之栈的基本操作.doc_第4页
C语言数据结构之栈的基本操作.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C语言数据结构之栈的基本操作1. 程序结构图 mainGetTopStackEmptyNizhiPopPushStackLengthInitStack2. 算法及其功能函数InitStack(SqStack *S) /*创建栈*/GetTop(SqStack *S,SElemType e) /*取栈顶元素并用e返回*/Push(SqStack *S,SElemType e) /*插入e为栈顶元素*/Pop(SqStack *S,SElemType *e) /*删除栈顶元素并用e返回*/StackEmpty(SqStack S) /*判断栈是否为空*/StackLength(SqStack S) /*求栈的长度*/Nizhi(SqStack S) /*将栈中元素逆置*/3. 源代码#include #include #define OK 1#define OVERFLOW -2typedef int Status;typedef int SElemType;#define STACK_INIT_SIZE 100 #define STACKINCREMENT 10typedef structSElemType *base;SElemType *top;int stacksize;SqStack;Status InitStack(SqStack *S)int i,n;SElemType e;S-base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType); if(!S-base) exit (OVERFLOW);S-top=S-base;S-stacksize=STACK_INIT_SIZE;printf(请输入栈内的数据个数:);scanf(%d,&n);if(n=S-stacksize)S-base=(SElemType *)realloc(S-base,(S-stacksize+STACKINCREMENT)*sizeof(SElemType);if(!S-base) exit(OVERFLOW); for(i=0;itop)=e;S-top=S-top+1;return OK;Status Init(SqStack *S)S-base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType); if(!S-base) exit (OVERFLOW);S-top=S-base;S-stacksize=STACK_INIT_SIZE;return OK;Status GetTop(SqStack *S,SElemType e) if(S-top=S-base) return 0;e=*(S-top-1);return e;Status Push(SqStack *S,SElemType e)if(S-top-S-base=S-stacksize)S-base=(SElemType *)realloc(S-base,(S-stacksize+STACKINCREMENT)*sizeof(SElemType);if(!S-base)exit (OVERFLOW);S-top=S-base+S-stacksize;S-stacksize+=STACKINCREMENT;printf(请输入要插入的栈顶元素:);scanf(%d,&e);*S-top+=e;return OK;void PushSq(SqStack *S,SElemType e)if(S-top-S-base=S-stacksize)S-base=(SElemType *)realloc(S-base,(S-stacksize+STACKINCREMENT)*sizeof(SElemType);if(!S-base)exit (OVERFLOW);S-top=S-base+S-stacksize;S-stacksize+=STACKINCREMENT;*S-top+=e;Status Pop(SqStack *S,SElemType *e)if(S-top=S-base) return 0;*e=*-S-top;return *e;Status StackEmpty(SqStack S)if(S.base=S.top)return 1; else return 0;Status StackLength(SqStack S) int i=0; while(S.topS.base) S.base+; i+; return i;Status Nizhi(SqStack S)SqStack A,B;SElemType e;if (Init(&A)=OVERFLOW) printf(构造失败,退出程序!n);if (Init(&B)=OVERFLOW) printf(构造失败,退出程序!n);while(!StackEmpty(S) Pop(&S,&e); PushSq(&A,e);while(!StackEmpty(A) Pop(&A,&e); PushSq(&B,e);printf(逆置后n);while(!StackEmpty(B) Pop(&B,&e);printf(%dn,e); PushSq(&S,e);return OK;void main() SqStack S,M; SElemType e; int select,N,xuanze,X; do printf(1:构造栈Sn); printf(2:判断栈S是否为空n ); printf(3:用e返回S的栈顶元素n); printf(4:插入元素e为新的栈顶元素n); printf(5:删除S的栈顶元素,并用e返回其值n); printf(6:返回栈S的元素个数n); printf(7: 逆置栈Sn); printf(8: 数值转换进制n); printf(0:结束n); scanf(%d,&select); switch (select) case 1: if (InitStack(&S)=OVERFLOW) printf(构造失败,退出程序!n);else printf(构造成功!n);break;case 2:if(StackEmpty(S)=1) printf(栈为空n);else printf(栈非空n);break;case 3:if(StackEmpty(S)=1) printf(错误!此表为空表!n);break;e=GetTop(&S,e);printf(e=%dn,e);break;case 4: if(StackEmpty(S)=1) printf(错误!此表为空表!n);break;if(Push(&S,e)=OVERFLOW) printf(插入元素e失败n);else printf(插入元素e成功!n);break;case 5:if(StackEmpty(S)=1) printf(错误!此表为空表!n);break;e=Pop(&S,&e);printf(e=%dn,e);break;case 6:printf(栈的长度为%dn,StackLength(S);break;case 7:if(StackEmpty(S)=1) printf(错误!此表为空表!n);break;if(Nizhi(S)=1) printf(逆置成功n);else printf(逆置失败n);break;case 8: Init(&M); printf(请输入任意非负十进制整数:nN=);scanf(%d,&N);printf(请选择将该整数转换成:n); do printf(1:2进制n); printf(2:8进制n ); printf(3:12进制n); printf(4:16进制n); printf(5:任意进制n); scanf(%d,&xuanze); switch (xuanze) case 1:X=2;break; case 2:X=8;break; case 3:X=12;break; case 4:X=16;break; case 5:printf(转换成X进制,X=); scanf(%d,&X);break; case 0:break;default:printf(输入选择出错!n); while (!xuanze); p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论