=p)*top[0]--=x;elsecerr<<"Stackoverflow!";}课后答案网else{if(top[1]top[0])return*top[1]--;elsecerr<<"Stackempty!";}returnOK;}若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cn};//链栈的数据结构及方法的定义typedefstructNodeType{ElemTypedata;NodeType*next;}NodeType,*LinkType;typedefstruct{LinkTypetop;intsize;}Stack;voidInitStack(Stack&s){s.top=NULL;s.size=0;}voidDestroyStack(Stack&s){LinkTypep;while(s.top){p=s.top;s.top=p->next;deletep;s.size--;}课后答案网}voidClearStack(Stack&s){LinkTypep;www.hackshp.cnwhile(s.top){p=s.top;s.top=p->next;deletep;s.size--;}}intStackLength(Stacks){returns.size;}若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cnStatusStackEmpty(Stacks){if(s.size==0)returnTRUE;elsereturnFALSE;}StatusGetTop(Stacks,ElemType&e){if(!s.top)returnERROR;else{e=s.top->data;returnOK;}}StatusPush(Stack&s,ElemTypee){LinkTypep;p=newNodeType;if(!p)exit(OVERFLOW);p->next=s.top;s.top=p;p->data=e;s.size++;returnOK;}课后答案网StatusPop(Stack&s,ElemType&e){LinkTypep;if(s.top){www.hackshp.cne=s.top->data;p=s.top;s.top=p->next;deletep;s.size--;}returnOK;}//从栈顶到栈底用Visit()函数遍历栈中每个数据元素voidStackTraverse(Stacks,Status(*Visit)(ElemTypee)){LinkTypep;若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cnp=s.top;while(p)Visit(p->data);}3.16假设如题3.1所属火车调度站的入口处有n节硬席或软席车厢(分别以H和S表示)等待调度,试编写算法,输出对这n节车厢进行调度的操作(即入栈或出栈操作)序列,以使所有的软席车厢都被调整到硬席车厢之前。解:intmain(){Stacks;charBuffer[80];inti=0,j=0;InitStack(s);cout<<"请输入硬席(H)和软席车厢(S)序列:";cin>>Buffer;cout<#includetypedefstruct{intx;inty;}PosType;typedefstruct{intColor;intVisited;PosTypeseat;}ElemType;#include"d:VC99Stack.h"#defineM8#defineN8ElemTypeg[M][N];课后答案网voidCreateGDS(ElemTypeg[M][N]);voidShowGraphArray(ElemTypeg[M][N]);voidRegionFilling(ElemTypeg[M][N],PosTypeCurPos,intNewColor);intmain()www.hackshp.cn{CreateGDS(g);ShowGraphArray(g);PosTypeStartPos;StartPos.x=5;StartPos.y=5;intFillColor=6;RegionFilling(g,StartPos,FillColor);cout<0&&!g[CurPos.x-1][CurPos.y].Visited&&g[CurPos.x-1][CurPos.y].Color==OldColor)Push(s,g[CurPos.x-1][CurPos.y]);if(CurPos.y0&&www.hackshp.cn!g[CurPos.x][CurPos.y-1].Visited&&g[CurPos.x][CurPos.y-1].Color==OldColor)Push(s,g[CurPos.x][CurPos.y-1]);}}voidCreateGDS(ElemTypeg[M][N]){inti,j;for(i=0;i=j)returnTRUE;elsereturnFALSE;}3.22如题3.21的假设条件,试写一个算法,对以逆波兰式表示的表达式求值。解:charCalVal_InverPoland(charBuffer[])若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cn{StackOpnd;InitStack(Opnd);inti=0;charc;ElemTypee1,e2;while(Buffer[i]!="#"){if(!IsOperator(Buffer[i])){Push(Opnd,Buffer[i]);}else{Pop(Opnd,e2);Pop(Opnd,e1);c=Cal(e1,Buffer[i],e2);Push(Opnd,c);}i++;}returnc;}charCal(charc1,charop,charc2){intx,x1,x2;charch[10];ch[0]=c1;课后答案网ch[1]=" ";x1=atoi(ch);ch[0]=c2;ch[1]=" ";www.hackshp.cnx2=atoi(ch);switch(op){case"+":x=x1+x2;break;case"-":x=x1-x2;break;case"*":x=x1*x2;break;若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cncase"/":x=x1/x2;break;default:break;}itoa(x,ch,10);returnch[0];}3.23如题3.21的假设条件,试写一个算法,判断给定的非空后缀表达式是否为正确的逆波兰表达式,如果是,则将它转化为波兰式。解:#include#include#include#include"d:VC99DSConstant.h"typedefcharARRAY[30];typedefARRAYElemType;typedefstructNodeType{ElemTypedata;NodeType*next;}NodeType,*LinkType;typedefstruct{LinkTypetop;intsize;}Stack;课后答案网voidInitStack(Stack&s);StatusPush(Stack&s,ElemTypee);StatusPop(Stack&s,ElemTypee);StatusIsOperator(charc);www.hackshp.cnStatusStackEmpty(Stacks);StatusInvToFroPoland(chara[]);intmain(){chara[30];cout<<"请输入逆波兰算术表达式字符序列:";cin>>a;if(InvToFroPoland(a))cout<="0"&&a[i]<="9"){ch[0]=a[i];ch[1]=" ";Push(s,ch);}elsereturnFALSE;}else{ch[0]=a[i];ch[1]=" ";if(!StackEmpty(s)){Pop(s,c2);if(!StackEmpty(s)){Pop(s,c1);strcat(ch,c1);课后答案网strcat(ch,c2);Push(s,ch);}elsereturnFALSE;}www.hackshp.cnelsereturnFALSE;}i++;}if(!StackEmpty(s)){Pop(s,c1);strcpy(a,c1);}elsereturnFALSE;if(!StackEmpty(s))returnFALSE;returnOK;}若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cnvoidInitStack(Stack&s){s.top=NULL;s.size=0;}StatusPush(Stack&s,ElemTypee){LinkTypep;p=newNodeType;if(!p)exit(OVERFLOW);p->next=s.top;s.top=p;strcpy(p->data,e);s.size++;returnOK;}StatusPop(Stack&s,ElemTypee){LinkTypep;if(s.top){strcpy(e,s.top->data);p=s.top;s.top=p->next;deletep;s.size--;课后答案网}returnOK;}StatusStackEmpty(Stacks)www.hackshp.cn{if(s.size==0)returnTRUE;elsereturnFALSE;}StatusIsOperator(charc){char*p="#+-*/";while(*p){if(*p==c)returnTRUE;p++;若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cn}returnFALSE;}3.24试编写如下定义的递归函数的递归算法,并根据算法画出求g(5,2)时栈的变化过程。⎧0m=,0n≥0g(m,n=)⎨⎩g(m−2,1n+n)m>,0n≥0解:intg(intm,intn);intmain(){intm,n;cout<<"请输入m和n的值:";cin>>m>>n;if(n>=0)cout<0)return(g(m-1,2*n)+n);elsereturn0;}假设主函数的返回地址为0,递归函数3条语句的地址分别为1、2、3。3064课后答案网313232163383440523.25试写出求递归函数F(n)的递归算法,并消除递归:www.hackshp.cn⎧⎪n+1n=0F()n=⎨⎪⎩n⋅F(nn)>02解:#include#defineN20intmain(){inti;inta[N];intn;cout<<"请输入n:";若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cncin>>n;for(i=0;idoubleSqrt(doubleA,doublep,doublee);intmain(){doubleA,p,e;cout<<"请输入Ape:";cin>>A>>p>>e;cout<-e&&(p*p-A)1){课后答案网e1.nval=e.nval;Pop(s,e);e.mval--;e.nval=e1.nval+1;}www.hackshp.cn}while(StackLength(s)!=1||e.mval!=0);returne.nval+1;}0,akm(2,1)021g=akm(2,0)1,akm(2,0)620akm=akm(m-1,1)=akm(1,1)2,akm(1,1)411g=akm(m,n-1)=akm(1,0)3,akm(1,0)610akm=akm(m-1,1)=akm(0,1)4,akm(0,1)401akm=n+1=2退栈0,akm(2,1)021g=akm(2,0)1,akm(2,0)620akm=akm(m-1,1)=akm(1,1)若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cn2,akm(1,1)411g=akm(m,n-1)=akm(1,0)3,akm(1,0)610akm=akm(m-1,1)=akm(0,1)=2退栈0,akm(2,1)021g=akm(2,0)1,akm(2,0)620akm=akm(m-1,1)=akm(1,1)2,akm(1,1)411g=akm(m,n-1)=akm(1,0)=2;akm=akm(m-1,g)=akm(0,2);3,akm(0,2)702akm=n+1=3退栈0,akm(2,1)021g=akm(2,0)1,akm(2,0)620akm=akm(m-1,1)=akm(1,1)2,akm(1,1)411g=akm(m,n-1)=akm(1,0)=2;akm=akm(m-1,g)=akm(0,2)=3;退栈0,akm(2,1)021g=akm(2,0)1,akm(2,0)620akm=akm(m-1,1)=akm(1,1)=3退栈0,akm(2,1)021g=akm(2,0)=3;akm=akm(1,3)1,akm(1,3)613g=akm(1,2);akm(m-1,g)2,akm(1,2)612g=akm(1,1);akm(m-1,g)3,akm(1,1)611g=akm(1,0);akm(m-1,g)4,akm(1,0)610akm=akm(0,1)5,akm(0,1)401akm(0,1)=2退栈0,akm(2,1)021g=akm(2,0)=3;akm=akm(1,3)1,akm(1,3)613g=akm(1,2);akm(m-1,g)2,akm(1,2)612g=akm(1,1);akm(m-1,g)3,akm(1,1)611课后答案网g=akm(1,0);akm(m-1,g)4,akm(1,0)610akm=akm(0,1)=2退栈0,akm(2,1)021g=akm(2,0)=3;akm=akm(1,3)1,akm(1,3)613g=akm(1,2);akm(m-1,g)2,akm(1,2)612www.hackshp.cng=akm(1,1);akm(m-1,g)3,akm(1,1)611g=akm(1,0)=2;akm(m-1,g)=akm(0,2)4,akm(0,2)702akm=n+1=3退栈0,akm(2,1)021g=akm(2,0)=3;akm=akm(1,3)1,akm(1,3)613g=akm(1,2);akm(m-1,g)2,akm(1,2)612g=akm(1,1);akm(m-1,g)3,akm(1,1)611g=akm(1,0)=2;akm(m-1,g)=akm(0,2)=3退栈0,akm(2,1)021g=akm(2,0)=3;akm=akm(1,3)1,akm(1,3)613g=akm(1,2);akm(m-1,g)2,akm(1,2)612g=akm(1,1)=3;akm(m-1,g)=akm(0,3)3,akm(0,3)703akm=n+1=4退栈若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cn0,akm(2,1)021g=akm(2,0)=3;akm=akm(1,3)1,akm(1,3)613g=akm(1,2);akm(m-1,g)2,akm(1,2)612g=akm(1,1)=3;akm(m-1,g)=akm(0,3)=4退栈0,akm(2,1)021g=akm(2,0)=3;akm=akm(1,3)1,akm(1,3)613g=akm(1,2)=4;akm(m-1,g)=akm(0,4)2,akm(0,4)704akm=n+1=5退栈0,akm(2,1)021g=akm(2,0)=3;akm=akm(1,3)1,akm(1,3)613g=akm(1,2)=4;akm(m-1,g)=akm(0,4)=5退栈0,akm(2,1)021g=akm(2,0)=3;akm=akm(1,3)=5退栈akm(2,1)=5;3.28假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点(注意不设头指针),试编写相应的队列初始化、入队列何处队列的算法。解:typedefintElemType;typedefstructNodeType{ElemTypedata;NodeType*next;}QNode,*QPtr;typedefstruct{QPtrrear;intsize;}Queue;课后答案网StatusInitQueue(Queue&q){q.rear=NULL;q.size=0;returnOK;www.hackshp.cn}StatusEnQueue(Queue&q,ElemTypee){QPtrp;p=newQNode;if(!p)returnFALSE;p->data=e;if(!q.rear){q.rear=p;p->next=q.rear;}else{若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cnp->next=q.rear->next;q.rear->next=p;q.rear=p;}q.size++;returnOK;}StatusDeQueue(Queue&q,ElemType&e){QPtrp;if(q.size==0)returnFALSE;if(q.size==1){p=q.rear;e=p->data;q.rear=NULL;deletep;}else{p=q.rear->next;e=p->data;q.rear->next=p->next;deletep;}q.size--;returnOK;}3.29如果希望循环队列中的元素都能得到利用,则需设置一个标志域tag,并以tag的值为0和1来区分课后答案网,尾指针和头指针值相同时的队列状态是“空”还是“满”。试编写与此结构相应的入队列和出队列的算法,并从时间和空间角度讨论设标志和不设标志这两种方法的使用范围(如当循环队列容量较小而队列中每个元素占的空间较多时,哪一种方法较好)。解:#defineMaxQSize4www.hackshp.cntypedefintElemType;typedefstruct{ElemType*base;intfront;intrear;Statustag;}Queue;StatusInitQueue(Queue&q){q.base=newElemType[MaxQSize];if(!q.base)returnFALSE;q.front=0;若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cnq.rear=0;q.tag=0;returnOK;}StatusEnQueue(Queue&q,ElemTypee){if(q.front==q.rear&&q.tag)returnFALSE;else{q.base[q.rear]=e;q.rear=(q.rear+1)%MaxQSize;if(q.rear==q.front)q.tag=1;}returnOK;}StatusDeQueue(Queue&q,ElemType&e){if(q.front==q.rear&&!q.tag)returnFALSE;else{e=q.base[q.front];q.front=(q.front+1)%MaxQSize;q.tag=0;}returnOK;}设标志节省存储空间,但运行时间较长。不设标志则正好相反。3.30假设将循环队列定义为:以域变量rear和length分别指示循环队列中队尾元素的位置和内含元素的个数。试给出此循环队列的队满条件,并写出相应的入队列和出队列的算法(在出队列的算法中要返回队课后答案网头元素)。解:#defineMaxQSize4typedefintElemType;typedefstruct{www.hackshp.cnElemType*base;intrear;intlength;}Queue;StatusInitQueue(Queue&q){q.base=newElemType[MaxQSize];if(!q.base)returnFALSE;q.rear=0;q.length=0;returnOK;}若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cnStatusEnQueue(Queue&q,ElemTypee){if((q.rear+1)%MaxQSize==(q.rear+MaxQSize-q.length)%MaxQSize)returnFALSE;else{q.base[q.rear]=e;q.rear=(q.rear+1)%MaxQSize;q.length++;}returnOK;}StatusDeQueue(Queue&q,ElemType&e){if((q.rear+MaxQSize-q.length)%MaxQSize==q.rear)returnFALSE;else{e=q.base[(q.rear+MaxQSize-q.length)%MaxQSize];q.length--;}returnOK;}3.31假设称正读和反读都相同的字符序列为“回文”,例如,‘abba’和‘abcba’是回文,‘abcde’和‘ababab’则不是回文。试写一个算法判别读入的一个以‘@’为结束符的字符序列是否是“回文”。解:StatusSymmetryString(char*p){Queueq;课后答案网if(!InitQueue(q))return0;Stacks;InitStack(s);ElemTypee1,e2;while(*p){www.hackshp.cnPush(s,*p);EnQueue(q,*p);p++;}while(!StackEmpty(s)){Pop(s,e1);DeQueue(q,e2);if(e1!=e2)returnFALSE;}returnOK;}3.32试利用循环队列编写求k阶菲波那契序列中前n+1项的算法,要求满足:f≤max而f>max,nn+1若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cn其中max为某个约定的常数。(注意:本题所用循环队列的容量仅为k,则在算法执行结束时,留在循环队列中的元素应是所求k阶菲波那契序列中的最后k项)解:intFibonacci(intk,intn){if(k<1)exit(OVERFLOW);Queueq;InitQueue(q,k);ElemTypex,e;inti=0;while(i<=n){if(i=k){//队列求和x=sum(q);DeQueue(q,e);EnQueue(q,x);}i++;}returnq.base[(q.rear+q.MaxSize-1)%q.MaxSize];}课后答案网3.33在顺序存储结构上实现输出受限的双端循环队列的入列和出列(只允许队头出列)算法。设每个元素表示一个待处理的作业,元素值表示作业的预计时间。入队列采取简化的短作业优先原则,若一个新提交的作业的预计执行时间小于队头和队尾作业的平均时间,则插入在队头,否则插入在队尾。解://Filename:Queue.hwww.hackshp.cntypedefstruct{ElemType*base;intfront;intrear;Statustag;intMaxSize;}DQueue;StatusInitDQueue(DQueue&q,intsize){q.MaxSize=size;q.base=newElemType[q.MaxSize];if(!q.base)returnFALSE;若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cnq.front=0;q.rear=0;q.tag=0;returnOK;}StatusEnDQueue(DQueue&q,ElemTypee){if(q.front==q.rear&&q.tag)returnFALSE;if(q.front==q.rear&&!q.tag){//空队列q.base[q.rear]=e;q.rear=(q.rear+1)%q.MaxSize;if(q.rear==q.front)q.tag=1;}else{//非空非满if(e<(q.base[q.front]+q.base[(q.rear+q.MaxSize-1)%q.MaxSize])/2){//从队头入队q.front=(q.front+q.MaxSize-1)%q.MaxSize;q.base[q.front]=e;if(q.rear==q.front)q.tag=1;}else{//从队尾入队q.base[q.rear]=e;q.rear=(q.rear+1)%q.MaxSize;if(q.rear==q.front)q.tag=1;}}returnOK;课后答案网}StatusDeDQueue(DQueue&q,ElemType&e){if(q.front==q.rear&&!q.tag)returnFALSE;else{//非空队列www.hackshp.cne=q.base[q.front];q.front=(q.front+1)%q.MaxSize;q.tag=0;}returnOK;}//Filename:XT333.cpp主程序文件#include#includetypedefintElemType;#include"D:VC99Queue.h"若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cnintmain(){intt1,t2,t3,t4;ElemTypee;cout<<"请输入作业a1、a2、a3、a4的执行时间:";cin>>t1>>t2>>t3>>t4;DQueuedq;InitDQueue(dq,5);EnDQueue(dq,t1);EnDQueue(dq,t2);EnDQueue(dq,t3);EnDQueue(dq,t4);while(dq.front!=dq.rear||dq.tag){DeDQueue(dq,e);cout<>ch;www.hackshp.cninti=0;while(ch[i]){if(ch[i]=="P")cout<#include#defineMaxSize128classString{char*ch;intcurlen;public:String(constString&ob);String(constchar*init);String();~String();voidStrAssign(Stringt);intStrCompare(Stringt);课后答案网intStrLength();voidConcat(Stringt);StringSubString(intstart,intlen);voidshow();};www.hackshp.cnString::String(constString&ob){ch=newchar[MaxSize+1];if(!ch)exit(1);curlen=ob.curlen;strcpy(ch,ob.ch);}String::String(constchar*init){ch=newchar[MaxSize+1];if(!ch)exit(1);curlen=strlen(init);若侵犯了您的版权利益,敬请来信告知!www.hackshp.cn
课后答案网:www.hackshp.cnstrcpy(ch,init);}String::String(){ch=newchar[MaxSize+1];if(!ch)exit(1);curlen=0;ch[0]=" ";}String::~String(){deletech;curlen=0;}voidString::StrAssign(Stringt){strcpy(ch,t.ch);curlen=t.curlen;}intString::StrCompare(Stringt){returnstrcmp(ch,t.ch);}intString::StrLength(){returncurlen;}课后答案网voidString::Concat(Stringt){strcat(ch,t.ch);curlen=curlen+t.curlen;}www.hackshp.cnStringString::SubString(intstart,intlen){Stringtemp;inti,j;if(start>=0&&start+len<=curlen&&len>0){temp.curlen=len;for(i=0,j=start;i