数据结构题集C语言版答案严蔚敏吴+伟民.docx
- 文档编号:7055276
- 上传时间:2023-01-16
- 格式:DOCX
- 页数:37
- 大小:25.19KB
数据结构题集C语言版答案严蔚敏吴+伟民.docx
《数据结构题集C语言版答案严蔚敏吴+伟民.docx》由会员分享,可在线阅读,更多相关《数据结构题集C语言版答案严蔚敏吴+伟民.docx(37页珍藏版)》请在冰豆网上搜索。
数据结构题集C语言版答案严蔚敏吴+伟民
第一章
16
voidDescend(int&x,int&y,int&z)
{
intt;
if(x if(x if(y } 17 StatusFibonacci(intk,intm,int&f) /*求k阶斐波那契序列的第m项的值f*/ { inti,j,sum,temp[20]; if(k<2||m<0)returnERROR; if(m elseif(m==k-1)f=1; else {for(i=0;i<=k-2;i++) temp[i]=0; temp[k-1]=1; for(i=k;i<=m;i++) {sum=0; for(j=i-k;j sum+=temp[j]; temp[i]=sum; } f=temp[m]; } returnOK; }//Fibonacci 18 voidScores(ResultType*result,ScoreType*score) /*求各校的男、女总分和团体总分,并依次存入数组score*/ /*假设比赛结果已经储存在result[]数组中,*/ /*并以特殊记录{"",male,'',"",0}(域scorce=0)*/ /*表示结束*/ { inti=0; chars; while(result[i].sport! =NULL) { switch(result[i].schoolname) { case'A': score[0].totalscore+=result[i].score; if(result[i].gender==male)score[0].malescore+=result[i].score; elsescore[0].femalescore+=result[i].score; break; case'B': score[1].totalscore+=result[i].score; if(result[i].gender==male)score[1].malescore+=result[i].score; elsescore[1].femalescore+=result[i].score; break; case'C': score[2].totalscore+=result[i].score; if(result[i].gender==male)score[2].malescore+=result[i].score; elsescore[2].femalescore+=result[i].score; break; case'D': score[3].totalscore+=result[i].score; if(result[i].gender==male)score[3].malescore+=result[i].score; elsescore[3].femalescore+=result[i].score; break; case'E': score[4].totalscore+=result[i].score; if(result[i].gender==male)score[4].malescore+=result[i].score; elsescore[4].femalescore+=result[i].score; break; } i++; } for(s='A';s<='E';s++) { printf("School%c: \n",s); printf("Totalscoreofmale: %d\n",score[i].malescore); printf("Totalscoreoffemale: %d\n",score[i].femalescore); printf("Totalscoreofall: %d\n\n",score[i].totalscore); } } 19 StatusSeries(intARRSIZE,inta[]) /*求i! *2^i序列的值并依次存入长度为ARRSIZE的数组a;*/ /*若所有值均不超过MAXINT,则返回OK,否则返回OVERFLOW*/ { intlast,i; last=1; for(i=1;i<=ARRSIZE;i++) { a[i-1]=last*2*i; if(a[i-1]>MAXINT) returnOVERFLOW; last=a[i-1]; } returnOK; } 20 floatPolynomial(intn,inta[],floatx) /*求一元多项式的值P(x)。 */ /*数组a的元素a[i]为i次项的系数,i=0,...,n*/ { intj,i; floaty,s; y=0; for(j=n;j>0;j--) y=x*(y+a[j]); s=y+a[0]; return(s); } 第二章 12 charCompare(SqListA,SqListB) //比较顺序表A和B, //返回'<',若A //'=',若A=B; //'>',若A>B { inti; for(i=1;i<=A.length&&i<=B.length;i++) if(A.elem[i]! =B.elem[i]) returnA.elem[i]>B.elem[i]? '>': '<'; if(A.length==B.length)return'='; returnA.length>B.length? '>': '<'; } 14 intLength(LinkListL) //Returnthelengthofthelinkedlist //whoseheadnodeispointedby'L' { LNode*p; intk; p=L; for(k=0;(p->next)! =NULL;p=p->next,k++); returnk; } 19 voidDeleteSome(LinkList&L,ElemTypemink,ElemTypemaxk) /*Deletealltheelementswhichvalueisbetweenminkand*/ /*maxkfromthesinglesortedLinkListwithheadpointerL.*/ { LinkListp,q,pre; pre=L; while(pre->next&&pre->next->data<=mink) pre=pre->next; p=pre->next; while(p&&p->data { q=p; p=p->next; free(q); } pre->next=p; } 21 voidInverse(SqList&L) { inti,j; ElemTypep; for(i=0,j=L.length-1;i { p=L.elem[j]; L.elem[j]=L.elem[i]; L.elem[i]=p; } } 21 voidInverse(LinkList&L) /*对带头结点的单链表L实现就地逆置*/ { LinkListp,q,s; p=L->next; q=p->next; if(p->next! =NULL&&q->next! =NULL) { s=q->next;p->next=NULL; while(s->next) { q->next=p;p=q;q=s;s=s->next; } q->next=p;s->next=q;L->next=s; } } 31 ElemTypeDeleteNode(LinkLists) /*删除指针s所指结点的前驱结点,并返回被删结点的元素值*/ {LNode*p; inte; p=s; while(p->next->next! =s) p=p->next; e=p->next->data; p->next=s; returne; } 41 voidDifference(LinkedPoly&pa) /*稀疏多项式pa以循环链表作存储结构,*/ /*将此链表修改成它的导函数,并释放无用结点*/ { LinkedPolyp,q,t; p=pa->next; t=pa; while(p! =pa) { if(p->exp==0) { q=p; p=q->next; t->next=p; free(q); } p->coef*=p->exp; p->exp--; t=p; p=p->next; } } 13 LinkListLocate(LinkList&L,ElemTypex) //If'x'inthelinkedlistwhoseheadnodeispointed //by'L',thenreturnpointerhapointingnode'x', //otherwisereturn'NULL' { LNode*p; for(p=L->next;p&&p->data! =x;p=p->next); returnp; } 16 StatusDeleteAndInsertSub(LinkList&la,LinkList&lb, inti,intj,intlen) //la和lb分别指向两个单链表中第一个结点,*/ /*本算法是从la表中删去自第i个元素起共len个元素,*/ /*并将它们插入到lb表中第j个元素之前,*/ /*若lb表中只有j-1个元素,则插在表尾。 */ { if(i<=0||j<=0||len<=0) returnINFEASIBLE; LNode*s,*t,*o,*r; intx=0,y=0,z=1; s=(LinkList)malloc(sizeof(LNode)); t=(LinkList)malloc(sizeof(LNode)); s->next=la; t->next=lb; la=s; lb=t; while(s->next! =NULL&&x<(i-1)) { s=s->next; x++; } if(s->next==NULL&&x<(i-1)) { s=la; t=lb; la=la->next; lb=lb->next; free(s); free(t); returnINFEASIBLE; } r=o=s->next; while(o! =NULL&&z { o=o->next; z++; } if(o==NULL||z { s=la; t=lb; la=la->next; lb=lb->next; free(s); free(t); returnINFEASIBLE; } while(t->next! =NULL&&y<(j-1)) { t=t->next; y++; } if(t->next==NULL&&y<(j-1)) { s=la; t=lb; la=la->next; lb=lb->next; free(s); free(t); returnINFEASIBLE; } s->next=o->next; o->next=t->next; t->next=r; s=la; t=lb; la=la->next; lb=lb->next; free(s); free(t); returnOK; } 20 voidPurge(LinkList&L) { LinkListp,t,r; p=L; t=p->next; while(p->next->next! =NULL) { p=p->next; t=t->next; while(t->next! =NULL&&t->data==p->data) { r=t; p->next=t->next; free(r); t=t->next; } } if(t->next==NULL&&p->data==t->data) {r=t; p->next=t->next; free(r); } } 23 voidMerge(LinkListha,LinkListhb,LinkList&hc) /*依题意,合并带头结点的单链表ha和hb为hc*/ { LNode*p; LNode*q; LNode*s; LNode*t; p=ha->next;q=hb->next;hc=ha; while(p&&q) { s=p->next;p->next=q; if(s) { t=q->next;q->next=s; } p=s;q=t; } } 26 voidIntersect(LinkList&hc,LinkListha,LinkListhb) { LNode*p,*q,*s,*pc; pc=hc; p=ha->next;q=hb->next; while(p&&q) { if(p->data elseif(p->data>q->data)q=q->next; else { s=(LNode*)malloc(sizeof(LNode)); s->data=p->data; pc->next=s;pc=s; p=p->next;q=q->next; } } } 32 voidPerfectBiLink(BiLinkList&CL) { BiNode*p; for(p=CL;! p->next->prev;p=p->next) p->next->prev=p; } 33 voidSplit(LinkList&lc,LinkList&ld,LinkList&lo,LinkListll) { LNode*p,*q,*r,*s; s=ll->next; lc=(LinkList)malloc(sizeof(LNode));p=lc; ld=(LinkList)malloc(sizeof(LNode));q=ld; lo=(LinkList)malloc(sizeof(LNode));r=lo;//建立头结点 while(s) { if((s->data>='A'&&s->data<='Z')||(s->data>='a'&&s->data<='z')) { p->next=s;p=s; } elseif(s->data>='0'&&s->data<='9') { q->next=s;q=s; } else { r->next=s;r=s; } s=s->next; } p->next=lc;q->next=ld;r->next=lo; } 37 voidReverseEven(BiLinkList&L) { BiNode*p; p=L->next; if(p->next==L) { p->prev=L;L->prev=p; } else{ while(p->next! =L&&p->next->next! =L) { p->next=p->next->next; p=p->next; } if(p->next==L) p->next=L->prev->prev; elsep->next=L->prev; p=p->next; while(p->prev->prev! =L) { p->next=p->prev->prev; p=p->next; } p->next=L; for(p=L;p->next! =L;p=p->next) p->next->prev=p; L->prev=p; } } 39 floatEvaluate(SqPolypn,floatx) /*pn.data[i].coef存放ai,*/ /*pn.data[i].exp存放ei(i=1,2,...,m)*/ /*本算法计算并返回多项式的值。 不判别溢出。 */ /*入口时要求0≤e1 { PolyTerm*q; floatxp,sum; xp=1;q=pn.data; sum=0; intex=0; while(q->coef! =NULL) { while(ex {xp*=x; ex++;} sum+=q->coef*xp; q++; } returnsum; } 第三章 15 StatusInitStack(TwoWayStack&tws,intsize) { tws.elem=(SElemType*)malloc(size*sizeof(SElemType)); if(! tws.elem)exit(OVERFLOW); tws.size=size; tws.top[0]=0; tws.top[1]=size-1; returnOK; } StatusPush(TwoWayStack&tws,inti,SElemTypex) { if(tws.top[0]+1>tws.top[1])returnERROR; elseif(i==0) { tws.elem[tws.top[0]++]=x; } else {tws.elem[tws.top[1]--]=x; } returnOK; } StatusPop(TwoWayStack&tws,inti,SElemType&x) {if(i==0) { if(tws.top[0]==0) returnERROR; else { x=tws.elem[--tws.top[0]]; } } else { if(tws.top[1]==tws.size-1) returnERROR; else { x=tws.elem[++tws.top[1]]; } } returnOK; } 16 voidSwitchYard(SqListtrain,char*s) /*顺序表train表示列车,其元素取值H或S,*/ /*分别表示硬席或软席车厢;*/ /*以U和O分别表示入栈和出栈操作;*/ /*函数用参数s以UO串的形式,返回对该列车*/ /*进行软席在前,硬席在后的编组的操作序列。 */ {ElemType*p; inti,count=0,j; p=train.elem; while(*p) {if(*p=='H'){s[i]='U';++count;} elseif(*p=='S'){s[i]='U';s[i+1]='O';i=i+1;} p++;i++; } for(j=1;j<=count;j++)s[i++]='O'; } 19 StatusMatchCheck(SqListexp) /*顺序表exp表示表达式;*/ /*若exp中的括号配对,则返回TRUE,否则返回FALSE*/ {SElemTypep; inti; floatj;j=exp.length%2;if(j)returnFALSE; SqStacks; if(exp.elem[0]==')'||exp.elem[0]==']'||exp.elem[0]=='}')returnFALSE; InitStack(s); for(i=0;i {switch(exp.elem[i]) {case'(': {Push(s,exp.elem[i]);break;} case'[': {Push(s,exp.elem[i]);break;} case'{': {Push(s,exp.elem[i]);break;} case')': {GetTop(s,p);if(p=='(')Pop(s,p);break;} case']': {GetTop(s,p);if(p=='[')Pop(s,p);break;} case'}': {GetTop(s,p);if(p=='{')Pop(s,p);break;} } } if(StackEmpty(s))returnTRUE; elsereturnFALSE; } 21 char*RPExpression(char*e) /*返回表达式e的逆波兰式*/ {inti; char*p,c; p=(char*)malloc(strlen(e)*sizeof(char)); Stacks; InitStack(s); for(;*e! ='\0';e++) {if((*e! ='+')&&(*e! ='-')&&(*e! ='*')&&(*e! ='/')&&(*e! ='(')&&(*e! =')')) {*(p+i)=*e; i++; } else {switch(*e) {case'(': Push(s,*e);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 语言版 答案 严蔚敏吴 伟民