c语言数据结构之栈的基本操作

c语言数据结构之栈的基本操作

ID:21026203

大小:65.50 KB

页数:7页

时间:2018-10-18

c语言数据结构之栈的基本操作_第1页
c语言数据结构之栈的基本操作_第2页
c语言数据结构之栈的基本操作_第3页
c语言数据结构之栈的基本操作_第4页
c语言数据结构之栈的基本操作_第5页
资源描述:

《c语言数据结构之栈的基本操作》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、C语言数据结构之栈的基本操作一.程序结构图mainGetTopStackEmptyNizhiPopPushStackLengthInitStack二.算法及其功能函数InitStack(SqStack*S)/*创建栈*/GetTop(SqStack*S,SElemTypee)/*取栈顶元素并用e返回*/Push(SqStack*S,SElemTypee)/*插入e为栈顶元素*/Pop(SqStack*S,SElemType*e)/*删除栈顶元素并用e返回*/StackEmpty(SqStackS)/*判断栈是否为空*/StackLength(SqStackS)/*求栈的

2、长度*/Nizhi(SqStackS)/*将栈中元素逆置*/三.源代码#include#include#defineOK1#defineOVERFLOW-2typedefintStatus;typedefintSElemType;#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT10typedefstruct{SElemType*base;SElemType*top;intstacksize;}SqStack;StatusInitStack(SqStack*S){inti,n;SElemTy

3、pee;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->ba

4、se)exit(OVERFLOW);}for(i=0;itop)=e;S->top=S->top+1;}returnOK;}StatusInit(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;returnOK;}StatusGetTop(SqSt

5、ack*S,SElemTypee){if(S->top==S->base)return0;e=*(S->top-1);returne;}StatusPush(SqStack*S,SElemTypee){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+=

6、STACKINCREMENT;}printf("请输入要插入的栈顶元素:");scanf("%d",&e);*S->top++=e;returnOK;}voidPushSq(SqStack*S,SElemTypee){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->sta

7、cksize+=STACKINCREMENT;}*S->top++=e;}StatusPop(SqStack*S,SElemType*e){if(S->top==S->base)return0;*e=*--S->top;return*e;}StatusStackEmpty(SqStackS){if(S.base==S.top)return1;elsereturn0;}StatusStackLength(SqStackS){inti=0;while(S.top>S.base){S.base++;i++;}returni;}StatusNiz

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。