c++程序设计教程参考答案第6章科学出版社.docx

上传人:scccc 文档编号:13593483 上传时间:2022-01-19 格式:DOCX 页数:5 大小:66.86KB
返回 下载 相关 举报
c++程序设计教程参考答案第6章科学出版社.docx_第1页
第1页 / 共5页
c++程序设计教程参考答案第6章科学出版社.docx_第2页
第2页 / 共5页
c++程序设计教程参考答案第6章科学出版社.docx_第3页
第3页 / 共5页
c++程序设计教程参考答案第6章科学出版社.docx_第4页
第4页 / 共5页
c++程序设计教程参考答案第6章科学出版社.docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《c++程序设计教程参考答案第6章科学出版社.docx》由会员分享,可在线阅读,更多相关《c++程序设计教程参考答案第6章科学出版社.docx(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+)ata=i+1;if(i=9)nodei.next=0;else nodei.next=&nodei+1;cout 链表为: ;int s=sum(h);cout 各结点的数据域之和是: sendl

2、;习题 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

3、=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+)ata=i+1;node2i.data=2*i;if(i=9)node1i.next=0;node2i.next=0;else node1i.next=&node1i+1;node2i.

4、next=&node2i+1;cout 链表1 为:;print(h1);cout 链表2 为:;print(h2);h=merge(h1,h2);n.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)ata;if(h=0)head=p;p-next=0;else ata=2*i;h=build(L,10);cout

5、链表为: ;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-n

6、ext!=h)coutdata;p=p-next;coutdataendl;NODE* build(int n)/ 建立链表 hNODE *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()int n,m;cout请输入人数:cinn;NODE *h=build(n);cout链表为:print(h);coutdataendl;delete p;)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1