华南农业大学数据结构上机答案实验2

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

8583顺序栈的基本操作时间限制:1000MS内存限制:1000K提交次数:530通过次数:212题型:编程题语言:无限制Description创建一个空的顺序栈,并实现栈的入栈、出栈、返回栈的长度、返回栈顶元素、栈的遍历等基本算法。请将下#includemalloc.h#includestdio.h#defineOK1#defineERROR0#defineSTACK_INIT_SIZE100//存储空间初始分配量#defineSTACKINCREMENT10//存储空间分配增量typedefintSElemType;//定义栈元素类型typedefintStatus;//Status是函数的类型,其值是函数结果状态代码,如OK等structSqStack{SElemType*base;//在栈构造之前和销毁之后,base的值为NULLSElemType*top;//栈顶指针intstacksize;//当前已分配的存储空间,以元素为单位};//顺序栈StatusInitStack(SqStack&S){//构造一个空栈S,该栈预定义大小为STACK_INIT_SIZE//请补全代码S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));if(!S.base)returnERROR;S.top=S.base;S.stacksize=STACK_INIT_SIZE;returnOK;}StatusPush(SqStack&S,SElemTypee){//在栈S中插入元素e为新的栈顶元素//请补全代码if(S.top-S.base=S.stacksize){S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));if(!S.base)returnERROR;S.top=S.base+S.stacksize;S.stacksize+=STACKINCREMENT;}*S.top++=e;returnOK;}StatusPop(SqStack&S,SElemType&e){//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR//请补全代码if(S.top==S.base)returnERROR;e=*--S.top;returnOK;}StatusGetTop(SqStackS,SElemType&e){//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR//请补全代码if(S.top==S.base)returnERROR;e=*(S.top-1);returnOK;}intStackLength(SqStackS){//返回栈S的元素个数//请补全代码returnS.top-S.base;}StatusStackTraverse(SqStackS){//从栈顶到栈底依次输出栈中的每个元素SElemType*p=(SElemType*)malloc(sizeof(SElemType));p=S.top;//请填空if(S.top==S.base)printf(TheStackisEmpty!);//请填空else{printf(TheStackis:);p--;while(p=S.base)//请填空{printf(%d,*p);p--;//请填空}}printf(\n);returnOK;}intmain(){inta;SqStackS;SElemTypex,e;if(InitStack(S))//判断顺序表是否创建成功,请填空{printf(AStackHasCreated.\n);}while(1){printf(1:Push\n2:Pop\n3:GettheTop\n4:ReturntheLengthoftheStack\n5:LoadtheStack\n0:Exit\nPleasechoose:\n);scanf(%d,&a);switch(a){case1:scanf(%d,&x);if(!Push(S,x))printf(PushError!\n);//判断Push是否合法,请填空elseprintf(TheElement%disSuccessfullyPushed!\n,x);break;case2:if(!Pop(S,e))printf(PopError!\n);//判断Pop是否合法,请填空elseprintf(TheElement%disSuccessfullyPoped!\n,e);break;case3:if(!GetTop(S,e))printf(GetTopError!\n);//判断GetTop是否合法,请填空elseprintf(TheTopElementis%d!\n,e);break;case4:printf(TheLengthoftheStackis%d!\n,StackLength(S));//请填空break;case5:StackTraverse(S);//请填空break;case0:return1;}}}8584循环队列的基本操作时间限制:1000MS内存限制:1000K提交次数:366通过次数:157题型:编程题语言:无限制Description创建一个空的循环队列,并实现入队、出队、返回队列的长度、返回队头元素、队列的遍历等基本算法。请将下面的程序补充完整。#includemalloc.h#includestdio.h#defineOK1#defineERROR0typedefintStatus;//Status是函数的类型,其值是函数结果状态代码,如OK等typedefintQElemType;#defineMAXQSIZE100//最大队列长度(对于循环队列,最大队列长度要减1)typedefstruct{QElemType*base;//初始化的动态分配存储空间intfront;//头指针,若队列不空,指向队列头元素intrear;//尾指针,若队列不空,指向队列尾元素的下一个位置}SqQueue;StatusInitQueue(SqQueue&Q){//构造一个空队列Q,该队列预定义大小为MAXQSIZE//请补全代码Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));if(!Q.base)returnERROR;Q.front=Q.rear=0;returnOK;}StatusEnQueue(SqQueue&Q,QElemTypee){//插入元素e为Q的新的队尾元素//请补全代码if((Q.rear+1)%MAXQSIZE==Q.front)returnERROR;Q.base[Q.rear]=e;Q.rear=(Q.rear+1)%MAXQSIZE;returnOK;}StatusDeQueue(SqQueue&Q,QElemType&e){//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR//请补全代码if(Q.front==Q.rear)returnERROR;e=Q.base[Q.front];Q.front=(Q.front+1)%MAXQSIZE;returnOK;}StatusGetHead(SqQueueQ,QElemType&e){//若队列不空,则用e返回队头元素,并返回OK,否则返回ERROR//请补全代码if(Q.front==Q.rear)returnERROR;e=Q.base[Q.front];returnOK;}intQueueLength(SqQueueQ){//返回Q的元素个数//请补全代码return(Q.rear+MAXQSIZE-Q.front)%MAXQSIZE;}StatusQueueTraverse(SqQueueQ){//若队列不空,则从队头到队尾依次输出各个队列元素,并返回OK;否则返回ERROR.inti;i=Q.front;if(Q.front==Q.rear)printf(TheQueueisEmpty!);//请填空else{printf(TheQueueis:);while(iQ.rear)//请填空{printf(%d,Q.base[i]);//请填空i=i+1;//请填空}}printf(\n);returnOK;}intmain(){inta;SqQueueS;QElemTypex,e;if(InitQueue(S))//判断顺序表是否创建成功,请填空{printf(AQueueHasCreated.\n);}while(1){printf(1:Enter\n2:Delete\n3:GettheFront\n4:ReturntheLengthoftheQueue\n5:LoadtheQueue\n0:Exit\nPleasechoose:\n);scanf(%d,&a);switch(a){case1:scanf(%d,&x);if(!EnQueue(S,x))printf(EnterError!\n);//判断入队是否合法,请填空elseprintf(TheElement%disSuccessfullyEntered!\n,x);break;case2:if(!DeQueue(S,e))printf(DeleteError!\n);//判断出队是否合法,请填空elseprintf(TheElement%disSuccessfullyDeleted!\n,e);break;case3:if(!GetHead(S,e))printf(GetHeadError!\n);//判断GetHead是否合法,请填空elseprintf(TheHeadoftheQueueis%d!\n,e);break;case4:printf(TheLengthoftheQueueis%d!\n,QueueLength(S));//请填空break;case5:QueueTraverse(S);//请填空break;case0:return1;}}}8585栈的应用——进制转换时间限制:1000MS内存限制:1000K提交次数:320通过次数:203题型:编程题语言:无限制Description利用顺序栈的基本操作算法,编写满足下列要求的数制转换程序:对于输入的任意一个非负十进制整数,打印输出与其等值的八进制数。#includemalloc.h#includestdio.h#includestdlib.h#defineOVERFLOW-1#defineOK1#defineERROR0#defineSTACK_INIT_SIZE20//存储空间初始分配量#defineSTACKINCREMENT10//存储空间分配增量typedefintSElemType;//定义栈元素类型typedefintStatus;//Status是函数的类型,其值是函数结果状态代码,如OK等typedefstructSqStack{SElemType*base;//在栈构造之前和销毁之后,base的值为NULLSElemType*top;//栈顶指针intstacksize;//当前已分配的存储空间,以元素为单位}SqStack;//顺序栈StatusInitStack(SqStack&S){//构造一个空栈S,该栈预定义大小为STACK_INIT_SIZES.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));if(!S.base)exit(OVERFLOW);S.top=S.base;S.stacksize=STACK_INIT_SIZE;returnOK;}StatusPush(SqStack&S,SElemTypee){//在栈S

1 / 22
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功