欢迎来到天天文库
浏览记录
ID:21026203
大小:65.50 KB
页数:7页
时间:2018-10-18
《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
此文档下载收益归作者所有