




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、习题1 结点数据之和:#include<iostream.h>struct Nodeint data;Node *next;int sum(Node* head)int s=0;while(head)s+=head->data;if(head->next)cout<<head->data<<"->"else cout<<head->data<<endl;head=head->next;return s;void main()Node node10,*h=node;for(int
2、i=0;i<10;i+)/建立链表nodei.data=i+1;if(i=9)nodei.next=0;else nodei.next=&nodei+1;cout<<"链表为:"int s=sum(h);cout<<"各结点的数据域之和是:"<<s<<endl;习题2 合并链表:#include<iostream.h>struct Nodeint data;Node *next;void print(Node* head)while(head)if(head->next)co
3、ut<<head->data<<"->"else cout<<head->data<<endl;head=head->next;Node* merge(Node* head1,Node* head2)推荐精选Node* head,*p;if(head1->data<head2->data)head=head1;head1=head1->next;elsehead=head2;head2=head2->next;p=head;while(head1&&head
4、2)if(head1->data<head2->data)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;i<10;i+)/建立链表node1i.data=
5、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结点:#include<iostream.h>struct Linkint
6、 data;Link *next;void print(Link* h)while(h)if(h->next)cout<<h->data<<"->"else cout<<h->data<<endl;h=h->next;Link* build(Link* h,int n)/用数组h建立链表head Link *head=0,*p;for(int i=0;i<n;i+)p=new Link;p->data=hi.data;if(h=0)head=p;p->next=0;else /把
7、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 rele
8、ase(Link* h)Link *p;while(h)p=h;h=h->next;delete p;void main()Link L10,*h;for(int i=0;i<10;i+)Li.data=2*i;h=build(L,10);cout<<"链表为:"print(h);int flag,x;cout<<"请输入x的值:"cin>>x;if(h->data=x)/处理头结点为x的情况Link *p1=h;h=h->next;delete p1;flag=0;else flag=dele
9、(h,x);/处理头结点不为x的情况if(flag=-1)cout<<"链表中无值为"<<x<<"的结点。n"else cout<<"删除值为"<<x<<"的结点后,链表为:n"print(h);推荐精选release(h);习题4 链表约瑟夫环:#include<iostream.h>struct NODEint data;NODE *next;void print(NODE* h)NODE *p=h;while(p->ne
10、xt!=h)cout<<p->data<<"->"p=p->next;cout<<p->data<<endl;NODE* build(int n)/建立链表h NODE *h=0,*p,*pend;for(int i=1;i<=n;i+)p=new NODE;p->data=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
11、*p=h,*p1;while(p->next!=p)for(int i=1;i<m;i+)p1=p;p=p->next;p1->next=p->next;cout<<p->data<<" "delete p;p=p1->next;推荐精选return p;void main()int n,m;cout<<"请输入人数:"cin>>n;NODE *h=build(n);cout<<"链表为:"print(h);cout<<"请输入报数终止值
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 个人借款担保人合同
- 电子商务网络推广合作免责协议
- 夫妻房屋财产约定协议书
- 商铺招商代理合同
- 三农村基层民主监督与管理制度化方案
- 电子签名认证技术研究开发合作协议
- 工业地产租赁合同
- 油漆工种劳务分包合同
- 小学禁毒宣传活动方案
- 楼梯口拆除改造合同
- 公司EHS知识竞赛题库附答案
- 社区健康促进工作计划
- 2025年度移动端SEO服务及用户体验优化合同
- 中小学《清明节活动方案》班会课件
- 特殊教育学校2024-2025学年度第二学期教学工作计划
- 宁夏银川一中2025届高三上学期第五次月考数学试卷(含答案)
- 2024年物联网安装调试员(初级工)职业资格鉴定考试题库(含答案)
- 《设备科安全培训》课件
- 延长石油招聘笔试题库
- 人教鄂教版六年级下册科学全册知识点
- 郑州市地图含区县可编辑可填充动画演示矢量分层地图课件模板
评论
0/150
提交评论