您好,欢迎访问三七文档
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//请补全代码}StatusPush(SqStack&S,SElemTypee){//在栈S中插入元素e为新的栈顶元素//请补全代码}StatusPop(SqStack&S,SElemType&e){//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR//请补全代码}StatusGetTop(SqStackS,SElemType&e){//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR//请补全代码}intStackLength(SqStackS){//返回栈S的元素个数//请补全代码}StatusStackTraverse(SqStackS){//从栈顶到栈底依次输出栈中的每个元素SElemType*p=(SElemType*)malloc(sizeof(SElemType));p=______________________//请填空if(______________________)printf(TheStackisEmpty!);//请填空else{printf(TheStackis:);p--;while(______________________)//请填空{printf(%d,*p);______________________//请填空}}printf(\n);returnOK;}intmain(){inta;SqStackS;SElemTypex,e;if(______________________)//判断顺序表是否创建成功,请填空{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(______________________)printf(PushError!\n);//判断Push是否合法,请填空elseprintf(TheElement%disSuccessfullyPushed!\n,x);break;case2:if(______________________)printf(PopError!\n);//判断Pop是否合法,请填空elseprintf(TheElement%disSuccessfullyPoped!\n,e);break;case3:if(______________________)printf(GetTopError!\n);//判断GetTop是否合法,请填空elseprintf(TheTopElementis%d!\n,e);break;case4:printf(TheLengthoftheStackis%d!\n,______________________);//请填空break;case5:______________________//请填空break;case0:return1;}}}Input测试样例格式说明:根据菜单操作:1、输入1,表示要实现Push操作,紧跟着输入要Push的元素2、输入2,表示要实现Pop操作3、输入3,返回栈顶元素4、输入4,返回栈的元素个数5、输入5,表示从栈顶到栈底输出栈的所有元素6、输入0,表示程序结束SampleInput121416534252220SampleOutputAStackHasCreated.1:Push2:Pop3:GettheTop4:ReturntheLengthoftheStack5:LoadtheStack0:ExitPleasechoose:TheElement2isSuccessfullyPushed!1:Push2:Pop3:GettheTop4:ReturntheLengthoftheStack5:LoadtheStack0:ExitPleasechoose:TheElement4isSuccessfullyPushed!1:Push2:Pop3:GettheTop4:ReturntheLengthoftheStack5:LoadtheStack0:ExitPleasechoose:TheElement6isSuccessfullyPushed!1:Push2:Pop3:GettheTop4:ReturntheLengthoftheStack5:LoadtheStack0:ExitPleasechoose:TheStackis:6421:Push2:Pop3:GettheTop4:ReturntheLengthoftheStack5:LoadtheStack0:ExitPleasechoose:TheTopElementis6!1:Push2:Pop3:GettheTop4:ReturntheLengthoftheStack5:LoadtheStack0:ExitPleasechoose:TheLengthoftheStackis3!1:Push2:Pop3:GettheTop4:ReturntheLengthoftheStack5:LoadtheStack0:ExitPleasechoose:TheElement6isSuccessfullyPoped!1:Push2:Pop3:GettheTop4:ReturntheLengthoftheStack5:LoadtheStack0:ExitPleasechoose:TheStackis:421:Push2:Pop3:GettheTop4:ReturntheLengthoftheStack5:LoadtheStack0:ExitPleasechoose:TheElement4isSuccessfullyPoped!1:Push2:Pop3:GettheTop4:ReturntheLengthoftheStack5:LoadtheStack0:ExitPleasechoose:TheElement2isSuccessfullyPoped!1:Push2:Pop3:GettheTop4:ReturntheLengthoftheStack5:LoadtheStack0:ExitPleasechoose:PopError!1:Push2:Pop3:GettheTop4:ReturntheLengthoftheStack5:LoadtheStack0:ExitPleasechoose:ProviderYqm8584循环队列的基本操作时间限制: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//请补全代码}StatusEnQueue(SqQueue&Q,QElemTypee){//插入元素e为Q的新的队尾元素//请补全代码}StatusDeQueue(SqQueue&Q,QElemType&e){//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR//请补全代码}StatusGetHead(SqQueueQ,QElemType&e){//若队列不空,则用e返回队头元素,并返回OK,否则返回ERROR//请补全代码}intQueueLength(SqQueueQ){//返回Q的元素个数//请补全代码}StatusQueueTraverse(SqQueueQ){//若队列不空,则从队头到队尾依次输出各个队列元素,并返回OK;否则返回ERROR.inti;i=Q.front;if(______________________)printf(TheQueueisEmpty!);//请填空else{printf(TheQueueis:);while(______________________)//请填空{printf(%d,______________________);//请填空i=______________________;//请填空}}printf(\n);returnOK;}intmain(){inta;SqQueueS;QElemTypex,e;if(______________________)//判断顺序表是否创建成功,请填空{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(______________________)printf(EnterError!\n);//判断入队是否合法,请填空elseprintf(TheElement%disSuccessfullyEntered!\n,x);break;case2:if(______________________)printf(DeleteError!\n);//判断出队是否合法,请填空elseprintf(TheElement%disSuccessfullyDeleted!\n,e);break;case3:if(______________________)printf(GetHeadError!\n);//判断GetHead是否合法,请填空elseprintf(TheHeadoftheQueueis%d!\n,e);break;case4:printf(TheLengthoftheQueueis%d!\n,_____
本文标题:数据结构第二实验
链接地址:https://www.777doc.com/doc-3381661 .html