清华大学严蔚敏数据结构课后题答案共70页word资料

上传人:re****.1 文档编号:563786393 上传时间:2023-11-10 格式:DOCX 页数:66 大小:90.70KB
返回 下载 相关 举报
清华大学严蔚敏数据结构课后题答案共70页word资料_第1页
第1页 / 共66页
清华大学严蔚敏数据结构课后题答案共70页word资料_第2页
第2页 / 共66页
清华大学严蔚敏数据结构课后题答案共70页word资料_第3页
第3页 / 共66页
清华大学严蔚敏数据结构课后题答案共70页word资料_第4页
第4页 / 共66页
清华大学严蔚敏数据结构课后题答案共70页word资料_第5页
第5页 / 共66页
点击查看更多>>
资源描述

《清华大学严蔚敏数据结构课后题答案共70页word资料》由会员分享,可在线阅读,更多相关《清华大学严蔚敏数据结构课后题答案共70页word资料(66页珍藏版)》请在金锄头文库上搜索。

1、第一章绪论1.16void 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_descending1.17Status fib(int k,int m,int &f)/求k阶斐波那契序列的第m项的值fint tempd;if(k2|m0) return ERROR;if(mk-1) f=0;else if (m=k-1) f=1;e

2、lsefor(i=0;i=k-2;i+) tempi=0;tempk-1=1; 初始化for(i=k;i=m;i+) 求出序列第k至第m个元素的值sum=0;for(j=i-k;ji;j+) sum+=tempj;tempi=sum;f=tempm;return OK;/fib分析:通过保存巳经计算出来的结果,此方法的时间复杂度仅为O(mQ).如果采用递归编程(大多数人都会首 先想到递归方法),则时间复杂度将高达O(k-m).1.18typedef struct(char *sport;enummale,female gender;char schoolname; /校 名为A,B,C, D或

3、E char *result;int score; resulttype;typedef structint malescore;int femalescore;int totalscore; scoretype;void summary(resulttype result )/求各校的男女总分和团体总分,假设结果巳经储存在result数组中scoretype score;i=0;while(resulti.sport!=NULL)switch(resulti.schoolname)case A:score 0 .totalscore+=resulti.score;if(resulti.gen

4、der=0) score 0 .malescore+=resulti.score;else score 0 .femalescore+=resulti.score;break;case B:score.totalscore+=resulti.score;if(resulti.gender=0) score.malescore+=resulti.score;else score.femalescore+=resulti.score;break;i+;for(i=0;i5;i+)printf(School %d:n,i);printf(Total score of male:%dn”,scorei

5、.malescore);printf(Total score of female:%dn”,scorei.femalescore);printf(Total score of all:%dnn,scorei.totalscore);/summary1.19Status algo119(int aARRSIZE)/求 i!*2Ai 序列的值且不超过 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分析:当某一项的结果超过了

6、 maxint时,它除以前面一项的商会发生异常.1.20void polyvalue()float ad;float *p=a;printf(Input number of terms:);scanf(%d”,&n);printf(Input the %d coefficients from a0 to a%d:n,n,n);for(i=0;i=n;i+) scanf(%f,p+);printf(Input value of x:);scanf(%f”,&x);p=a;xp=1;sum=0; /xp用于存放x的i次方for(i=0;i=n;i+)sum+=xp*(*p+);xp*=x;prin

7、tf(Value is:%f,sum);/polyvalue第二章线性表2.10Status DeleteK(SqList &a,int i,int k)/删除线性表a中第i个元素起的k个元素 if(i1llka.length) return INFEASIBLE;for(count=1;i+count-1va.listsize) 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_SqList2.12int ListComp(

8、SqList A,SqList B)/比较字符表A和B,并用返回值表示结果,值为正,表示AB;值为负,表示Anext;p&p-data!=x;p=p-next);return p;/Locate2.14int Length(LinkList L)/求 链表的长度for(k=0,p=L;p-next;p=p-next,k+);return k;/Length2.15void ListConcat(LinkList ha,LinkList hb,LinkList &hc)/把链表 hb 接在 ha 后面形成链表 hchc=ha;p=ha;while(p-next) p=p-next;p-next=

9、hb;/ListConcat2.16见书后答案.2.17Status 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; 插入在链表头部elsewhile(-i1) p=p-next;q-next=p-next;p-next=q; /插入在第i个元素的位置/Insert2.18Status Delete(LinkList &L,int i)/在无头结点链表L中删除第i个元素_if(i=1) L=L-next

10、; 删除第一个元素elsep=L;while(-i1) p=p-next;p-next=p-next-next; /删除第 i 个元素/Delete2.19Status 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;/Dele

11、te_Between2.20Status Delete_Equal(Linklist &L)/删除元素递增排列的链表L中所有值相同的元素p=L-next;q=p-next; /p,q 指向相邻两元素while(p-next)if(p-data!=q-data)p=p-next;q=p-next; /当相邻两元素不相等时,p,q都向后推一步elsewhile(q-data=p-data)free(q);q=q-next;p-next=q;p=q;q=p-next; /当相邻元素相等时删除多余元素/else/while/Delete_Equal 2.21void reverse(SqList &A

12、)/顺序表的就地逆置for(i=1,j=A.length;ij;i+,j-)A.elemiA.elemj;/reverse2.22void LinkList_reverse(Linklist &L)/链表的就地逆置;为简化算法,假设表长大于2p=L-next;q=p-next;s=q-next;p-next=NULL;while(s-next)q-next=p;p=q;q=s;s=s-next; 把L的元素逐个插入新表表头q-next=p;s-next=q;L-next=s;/LinkList_reverse分析:本算法的思想是,逐个地把L的当前元素q插入新的链表头部,p为新表表头.2.23void merge1(LinkList &A,LinkList &B,LinkList &C)/把链表 A 和 B 合并为 C,A 和 B 的元素间隔排列,且使用原存储空间p=A-next;q=B-next;C=A;while(p&q)_s=p-next;p-next=q; 将 B 的元素插入if(s)_t=q-next;q-next=s; 如A非空,将A的元素插入

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

当前位置:首页 > 学术论文 > 其它学术论文

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