C++程序设计教程参考答案第6章 科学出版社_第1页
C++程序设计教程参考答案第6章 科学出版社_第2页
C++程序设计教程参考答案第6章 科学出版社_第3页
C++程序设计教程参考答案第6章 科学出版社_第4页
C++程序设计教程参考答案第6章 科学出版社_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

习题1 结点数据之和:#includestruct Nodeint data;Node *next;int sum(Node* head)int s=0;while(head)s+=head-data;if(head-next)coutdata;else coutdatanext;return s;void main()Node node10,*h=node;for(int i=0;i10;i+)/建立链表nodei.data=i+1;if(i=9)nodei.next=0;else nodei.next=&nodei+1;cout链表为:;int s=sum(h);cout各结点的数据域之和是:sendl;习题2 合并链表:#includestruct Nodeint data;Node *next;void print(Node* head)while(head)if(head-next)coutdata;else coutdatanext;Node* merge(Node* head1,Node* head2)Node* head,*p;if(head1-datadata)head=head1;head1=head1-next;elsehead=head2;head2=head2-next;p=head;while(head1&head2)if(head1-datadata)p-next=head1;head1=head1-next;elsep-next=head2;head2=head2-next;p=p-next;if(head1)p-next=head1;else if(head2)p-next=head2;return head;void main()Node node110,*h1=node1,*h;Node node210,*h2=node2;for(int i=0;i10;i+)/建立链表node1i.data=i+1;node2i.data=2*i;if(i=9)node1i.next=0;node2i.next=0;else node1i.next=&node1i+1;node2i.next=&node2i+1;cout链表1为:;print(h1);cout链表2为:;print(h2);h=merge(h1,h2);cout合并后的链表为:;print(h);习题3 删除x结点:#includestruct Linkint data;Link *next;void print(Link* h)while(h)if(h-next)coutdata;else coutdatanext;Link* build(Link* h,int n)/用数组h建立链表head Link *head=0,*p;for(int i=0;idata=hi.data;if(h=0)head=p;p-next=0;else /把p插入head之前p-next=head;head=p;return head;Link* find(Link* h,int x)/不包括头结点为x的情况Link *p;while(h)p=h;h=h-next;if(h=0)return 0;if(h-data=x)return p;return 0;int dele(Link* h,int x)Link* p1=find(h,x);if(!p1)return -1;Link* p2=p1-next;p1-next=p2-next;delete p2;return 0;void release(Link* h)Link *p;while(h)p=h;h=h-next;delete p;void main()Link L10,*h;for(int i=0;i10;i+)Li.data=2*i;h=build(L,10);cout链表为:;print(h);int flag,x;coutx;if(h-data=x)/处理头结点为x的情况Link *p1=h;h=h-next;delete p1;flag=0;else flag=dele(h,x);/处理头结点不为x的情况if(flag=-1)cout链表中无值为x的结点。n;else cout删除值为x的结点后,链表为:n;print(h);release(h);习题4 链表约瑟夫环:#includestruct NODEint data;NODE *next;void print(NODE* h)NODE *p=h;while(p-next!=h)coutdata;p=p-next;coutdataendl;NODE* build(int n)/建立链表h NODE *h=0,*p,*pend;for(int i=1;idata=i;if(h=0) h=p;else pend-next=p;/把p插入pend之后pend=p;pend-next=h;return h;NODE* number(NODE* h,int m)NODE *p=h,*p1;while(p-next!=p)for(int i=1;inext;p1-next=p-next;coutdatanext;return p;void main()i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论