资源描述:
《数据结构课件2第3_5章作业.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、【3.15】#defineSTACKSIZE50#defineSTACKINCREMENT10typedefstrcutDStack{ElemType*elem;inttop1,top2;intstacksize;}DStack;StatusInitStack(DStack&S){if(!S.elem=newElemType[STACKSIZE])exit(OVERFLOW);S.stacksize=STACKSIZE;S.top1=0;S.top2=STACKSIZE-1;returnOK;}top1top2elemStatusPush(DStack&S,inti,ElemTypex
2、){if(i!=0&&i!=1)returnERROR;if(S.top1>S.top2){newbase=realloc(S.elem,stacksize,STACKINCREMENT);if(!newbase)exit(OVERFLOW);if(S.elem)delete[]S.elem;S.elem=newbase;for(j=S.stacksize-1;j>S.top2;j--)S.elem[j+STACKINCREMENT]=S.elem[j];S.stacksize+=STACKINCREMENT;S.top2+=SATCKINCREMENT;}if(i==0){S.ele
3、m[top1++]=x;}else{S.elem[top2--]=x;}returnOK;}top1top2elemStatusPop(DStack&S,inti,ElemType&e){if(i!=0&&1!=1)returnERROR;if(i==0){if(S.top1==0)returnERROR;e=S.elem[--S.top1];}else{if(S.top2==S.stacksize-1)returnERROR;e=S.elem[++S.top2];}returnOK;}【3.17】intIsSymmetry(){InitStack(S);cin>>ch;while(c
4、h!='@'&&ch!='&'){push(S,ch);cin>>ch;}if(ch=='&'){cin>>ch;while(ch!='@'){if(StackEmpty(S))returnFLASE;Pop(S,e);if(ch!=e)returnFLASE;cin>>ch;}}if(ch=='@'&&!StackEmpty(S))returnFLASE;returnTRUE;}【3.21】StatusreversePolistnotion(charexp[],charrPolist[]){//表达式以'#'结束InitStack(S);i=0;k=0;while(exp[i]!='
5、#'
6、
7、!StackEmpty(S)){switch(exp[i]){(isvariable):rPolist[k++]=exp[i++];break;(isoperator)
8、
9、(is'#'):GetTop(S,e);while(!StackEmpty(S)&&comp(e>=exp[i]){Pop(S,rPolist[k++]);GetTop(S,e);}if(exp[i]!=’#’)Push(S,exp[i++]);break;}}rPolist[k]='#';returnOK;}【3.22】Statuscalculate(charrPolist[]){InitStack(S);
10、for(i=0;rPolist[i]!='#';i++){if(rPolist[i]isvariable)Push(S,rPolist[i]);else{Pop(S,p2);Pop(S,p1);Push(S,operate(p1,rPolist,p2));}}}【3.27(1)】intakm((intm,intn){if(m==0)returnn+1;elseif(n==0)returnakm(m-1,1);else{t=akm(m,n-1);returnakm(m-1,t);}}注意:不能给函数名赋值【3.28】typedefstructNode{ElemTypedata;struc
11、tNode*next;}NODE,StatutsInitQueue(QUEUE&Q){Q.rear=newNODE;if(!Q.rear)returnOVERFLOW;Q.rear->next=Q.rear;returnOK;}typedefstructQueue{Node*rear;}QUEUE;StatutsEnQueue(QUEUE&Q,ElemTypee){s=newNODE;if(!s)returnOVERFLOW;s->data=e