严蔚敏数据结构完整答案

上传人:m**** 文档编号:477017701 上传时间:2023-01-23 格式:DOC 页数:117 大小:274.50KB
返回 下载 相关 举报
严蔚敏数据结构完整答案_第1页
第1页 / 共117页
严蔚敏数据结构完整答案_第2页
第2页 / 共117页
严蔚敏数据结构完整答案_第3页
第3页 / 共117页
严蔚敏数据结构完整答案_第4页
第4页 / 共117页
严蔚敏数据结构完整答案_第5页
第5页 / 共117页
点击查看更多>>
资源描述

《严蔚敏数据结构完整答案》由会员分享,可在线阅读,更多相关《严蔚敏数据结构完整答案(117页珍藏版)》请在金锄头文库上搜索。

1、严蔚敏数据结构(C语言版)习题集答案第一章 绪论1.16 void print_descending(int x,int y,int z)/按从大到小顺序输出三个数scanf(%d,%d,%d,&x,&y,&z);if(xy) xy; /为表示交换的双目运算符,以下同if(yz) yz;if(xy) xy; /冒泡排序printf(%d %d %d,x,y,z);/print_descending 1.17 Status fib(int k,int m,int &f)/求k阶斐波那契序列的第m项的值f int tempd;if(k2|m0) return ERROR; if(mk-1) f=0

2、;else if (m=k-1 | m=k) f=1;else for(i=0;i=k-2;i+) tempi=0; tempk-1=1;tempk=1; /初始化 sum=1; j=0; for(i=k+1;i=m;i+,j+) /求出序列第k至第m个元素的值 tempi=2*sum-tempj; f=tempm;return OK;/fib分析: k阶斐波那契序列的第m项的值fm=fm-1+fm-2+.+fm-k =fm-1+fm-2+.+fm-k+fm-k-1-fm-k-1 =2*fm-1-fm-k-1所以上述算法的时间复杂度仅为O(m). 如果采用递归设计,将达到O(km). 即使采用

3、暂存中间结果的方法,也将达到O(m2). 1.18 typedef struct char *sport; enummale,female gender; char schoolname; /校名为A,B,C,D或E char *result; int score; resulttype; typedef struct int malescore; int femalescore; int totalscore; scoretype; void summary(resulttype result )/求各校的男女总分和团体总分,假设结果已经储存在result 数组中scoretype scor

4、eMAXSIZE;i=0;while(resulti.sport!=NULL) switch(resulti.schoolname) case A: score 0 .totalscore+=resulti.score; if(resulti.gender=0) score 0 .malescore+=resulti.score; else score 0 .femalescore+=resulti.score; break; case B: score 0 .totalscore+=resulti.score; if(resulti.gender=0) score 0 .malescore+

5、=resulti.score; else score 0 .femalescore+=resulti.score; break; i+;for(i=0;i5;i+) printf(School %d:n,i); printf(Total score of male:%dn,scorei.malescore); printf(Total score of female:%dn,scorei.femalescore); printf(Total score of all:%dnn,scorei.totalscore);/summary 1.19 Status algo119(int aARRSIZ

6、E)/求i!*2i序列的值且不超过maxintlast=1;for(i=1;i=ARRSIZE;i+)ai-1=last*2*i; if(ai-1/last)!=(2*i) reurn OVERFLOW; last=ai-1; return OK;/algo119分析:当某一项的结果超过了maxint时,它除以前面一项的商会发生异常. 1.20 void polyvalue()float temp;float *p=a;printf(Input number of terms:);scanf(%d,&n);printf(Input value of x:);scanf(%f,&x);print

7、f(Input the %d coefficients from a0 to a%d:n,n+1,n);p=a;xp=1;sum=0; /xp用于存放x的i次方for(i=0;i=n;i+) scanf(%f,&temp); sum+=xp*(temp); xp*=x;printf(Value is:%f,sum);/polyvalue第二章 线性表2.10 Status DeleteK(SqList &a,int i,int k)/删除线性表a中第i个元素起的k个元素if(i1|ka.length) return INFEASIBLE;for(count=1;i+count-1va.list

8、size) return ERROR;va.length+;for(i=va.length-1;va.elemix&i=0;i-) va.elemi+1=va.elemi;va.elemi+1=x;return OK;/Insert_SqList 2.12 int ListComp(SqList A,SqList B)/比较字符表A和B,并用返回值表示结果,值为1,表示AB;值为-1,表示AB;值为0,表示A=Bfor(i=1;i=A.length&iB.elemi?1:-1;if(A.length=B.length) return 0;return A.lengthB.length?1:-1

9、; /当两个字符表可以互相比较的部分完全相同时,哪个较长,哪个就较大/ListComp 2.13 LNode* Locate(LinkList L,int x)/链表上的元素查找,返回指针for(p=l-next;p&p-data!=x;p=p-next);return p;/Locate 2.14 int Length(LinkList L)/求链表的长度for(k=0,p=L;p-next;p=p-next,k+);return k;/Length 2.15 void ListConcat(LinkList ha,LinkList hb,LinkList &hc)/把链表hb接在ha后面形

10、成链表hchc=ha;p=ha;while(p-next) p=p-next;p-next=hb;/ListConcat 2.16 见书后答案. 2.17 Status Insert(LinkList &L,int i,int b)/在无头结点链表L的第i个元素之前插入元素bp=L;q=(LinkList*)malloc(sizeof(LNode);q.data=b;if(i=1) q.next=p;L=q; /插入在链表头部else while(-i1) p=p-next; q-next=p-next;p-next=q; /插入在第i个元素的位置/Insert 2.18 Status Del

11、ete(LinkList &L,int i)/在无头结点链表L中删除第i个元素if(i=1) L=L-next; /删除第一个元素else p=L; while(-i1) p=p-next; p-next=p-next-next; /删除第i个元素/Delete 2.19 Status Delete_Between(Linklist &L,int mink,int maxk)/删除元素递增排列的链表L中值大于mink且小于maxk的所有元素p=L;while(p-next-datanext; /p是最后一个不大于mink的元素if(p-next) /如果还有比mink更大的元素 q=p-next; while(q-datanext; /q是第一个不小于maxk的元素 p-next=q;/Delete_Between 2.20 Status Delete_Equal(Linklist &L)/删除元素递增排列的链表L中所有值相同的元素p=L-next;q

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 医学/心理学 > 基础医学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号