欢迎来到天天文库
浏览记录
ID:51426646
大小:33.52 KB
页数:5页
时间:2020-03-24
《栈的基本操作(C语言实现).doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、//栈的基本操作#defineERROR0#defineOVERFLOW-2#defineStack_init_size100#defineStackincrement10#include#includetypedefintSElemType;typedefstruct{SElemType*base;SElemType*top;intstacksize;}SqStack;voidmain(){SElemTypee;SqStackS;voidInitStack(SqStack*S);//构造栈的声明voidGetTo
2、p(SqStack*S,SElemTypee);//取栈顶元素函数声明voidPush(SqStack*S,SElemTypee);//进栈函数声明voidPop(SqStack*S,SElemTypee);//弹出栈顶元素函数声明printf("构造一个栈并读入数据:");InitStack(&S);//构造栈GetTop(&S,e);//取栈顶元素Push(&S,e);//进栈Pop(&S,&e);//弹出栈顶元素printf("");GetTop(&S,e);//取栈顶元素}voidInitStack(SqStack*S)//构造栈{in
3、ti,n;SElemTypee;S->base=(SElemType*)malloc(Stack_init_size*sizeof(SElemType));//if(!S->base)voidexit(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
4、)*sizeof(SElemType));//if(!S->base)voidexit(OVREFLOW);}for(i=0;itop)=e;S->top=S->top+1;}}voidGetTop(SqStack*S,SElemTypee)//取栈顶元素函数{if(S->top==S->base)returnERROR;e=*(S->top-1);printf("取出栈顶元素:%d",e);}voidPush(SqStack*S,SElemTypee)//进栈
5、函数{if(S->top-S->base>=S->stacksize){S->base=(SElemType*)realloc(S->base,(S->stacksize+Stackincrement)*sizeof(SElemType));//if(!S->base)voidexit(OVREFLOW);S->top=S->base+S->stacksize;S->stacksize=S->stacksize+Stackincrement;}printf("请输入要插入的栈顶元素:");scanf("%d",&e);*S->top=e;S->top+
6、+;}voidPop(SqStack*S,SElemTypee)//弹出栈顶元素函数{if(S->top==S->base)returnERROR;S->top--;e=*S->top;printf("要删除栈顶元素:%d",e);}
此文档下载收益归作者所有