




已阅读5页,还剩12页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
计算机与信息学院数据结构实验报告专 业 班 级 学生姓名及学号 课程教学班号 任 课 教 师 实验指导教师 实验地点 2015 2016 学年第 2 学期说 明实验报告是关于实验教学内容、过程及效果的记录和总结,因此,应注意以下事项和要求:1每个实验单元在4页的篇幅内完成一份报告。“实验单元”指按照实验指导书规定的实验内容。若篇幅不够,可另附纸。2、各实验的预习部分的内容是进入实验室做实验的必要条件,请按要求做好预习。3实验报告要求:书写工整规范,语言表达清楚,数据和程序真实。理论联系实际,认真分析实验中出现的问题与现象,总结经验。4参加实验的每位同学应独立完成实验报告的撰写,其中程序或相关的设计图纸也可以采用打印等方式粘贴到报告中。严禁抄袭或拷贝,否则,一经查实,按作弊论取,并取消理论课考试资格。5实验报告作为评定实验成绩的依据。 实验序号及名称:实验一 单链表实验 实验时间 2016年 5 月 预习内容一、实验目的和要求(1)理解线性表的链式存储结构。(2)熟练掌握动态链表结构及有关算法的设计。(3)根据具体问题的需要,设计出合理的表示数据的链表结构,设计相关算法。二、实验任务说明1:本次实验中的链表结构均为带头结点的单链表。说明2:为使实验程序简洁直观,下面的部分实验程序中将所需要的函数以调用库函数的形式给出,并假设将库函数放在程序文件“linklist.h”中,同时假设该库函数文件中定义了链表结构中的指针类型为link,结点类型为node,并定义了部分常用运算。 例如构建链表、以某种方式显示链表、从文件中读入一个链表、跟踪访问链表结点等。 各运算的名称较为直观,并有相应的注释,因而易于理解和实现。三、实验准备方案,包括以下内容:(硬件类实验:实验原理、实验线路、设计方案等)(软件类实验:所采用的核心方法、框架或流程图及程序清单)实验准备方案: 构建库函数:定义了链表结构中的指针类型为link,结点类型为node,并定义了部分常用运算,如构建链表,显示链表,读取链表,访问链表等;流程: 略 实验内容一、实验用仪器、设备:个人计算机C-free5.0二、实验内容与步骤(过程及数据记录):求链表中第i个结点的指针(函数),若不存在,则返回NULL。实验测试数据基本要求:第一组数据:链表长度n10,i分别为5,n,0,n+1,n+2第二组数据:链表长度n=0,i分别为0,2node* list:address(int i)node *p = head-next;int n = 1;while (n != i&p != NULL)p = p-next;n+;if (p!=NULL) return p;else return NULL;第一组数据第二组数据在第i个结点前插入值为x的结点。实验测试数据基本要求:第一组数据:链表长度n10,x=100, i分别为5,n,n+1,0,1,n+2第二组数据:链表长度n=0,x=100,i=5errorcode list:insert(const int i, const int x)node *p;p = head;int n = 1;while (n != i&p != NULL)p = p-next;n+;if (ilength() + 1) return rangeerror;node *s = new node;s-data = x;s-next = p-next;p-next = s;count+;return success;删除链表中第i个元素结点。实验测试数据基本要求:第一组数据:链表长度n10,i分别为5,n,1,n+1,0 第二组数据:链表长度n=0, i=5errorcode list:delete_ele(const int i)node *p;p = head;int n = 1;while (n != i&p != NULL)p = p-next;n+;if (i count) return rangeerror;node *u;u = p-next;p-next = u-next;count-;delete u;return success;在一个递增有序的链表L中插入一个值为x的元素,并保持其递增有序特性。实验测试数据基本要求:链表元素为 (10,20,30,40,50,60,70,80,90,100),x分别为25,85,110和8errorcode list:orderinsert(int x)node *p = head;int n = 1;while (p-next != NULL)if (p-next-data next;else break;node *u = new node;u-data = x;u-next = p-next;p-next = u;count+;return success;将单链表中的奇数项和偶数项结点分解开,并分别连成一个带头结点的单链表,然后再将这两个新链表同时输出在屏幕上,并保留原链表的显示结果,以便对照求解结果。实验测试数据基本要求:第一组数据:链表元素为 (1,2,3,4,5,6,7,8,9,10,20,30,40,50,60)第二组数据:链表元素为 (10,20,30,40,50,60,70,80,90,100)void separate(list&A,list&B,list&C) node*LA;node*LB;node*p;node*q;node*u;node*s; LA=A.get_head(); LB=B.get_head(); q=LA;p=LA-next;s=LB; if(p-data%2=0) u=p;p=p-next;q-next=p; s-next=u; s=s-next; else p=p-next;q=q-next; 求两个递增有序链表L1和L2中的公共元素,并以同样方式连接成链表L3。实验测试数据基本要求: 第一组第一个链表元素为 (1,3,6,10,15,16,17,18,19,20)第二个链表元素为 (1,2,3,4,5,6,7,8,9,10,18,20,30)第二组第一个链表元素为 (1,3,6,10,15,16,17,18,19,20)第二个链表元素为 (2,4,5,7,8,9,12,22)第三组第一个链表元素为 ()第二个链表元素为 (1,2,3,4,5,6,7,8,9,10)bingji(list A,list B,list&C) node*LA; node*LB; node*LC; node*a;node*b; LC=C.get_head(); LA=A.get_head(); LB=B.get_head(); a=LA-next;b=LB-next; while(a!=NULL&b!=NULL) if(a-datadata) a=a-next; else if(a-datab-data) b=b-next; else node*c=new node; c-data=a-data;LC-next=c;LC=c;C.count+; a=a-next;b=b-next; LC-next=NULL; CPP文件附加:#include #include enum error_codesuccess,arrange_error; typedef struct nodeint data;node*next;node;class listpublic: list(); int length()const; list(); node* get_element(int locate)const; node*locate(const int x)const; error_code charu(const int i); error_code insert(const int locate,const int i); error_code delete_element(const int i); node* get_head()return head; void separate(list&A,list&B); int bingji(list A,list B,list&C); void create_R();void list:show(); private: int count; node*head ;node*rear ;list:list() head=new node; head-next=NULL; count=0; int list:length() const node*p=head-next; int count=0; while(p!=NULL) count+; p=p-next; return count;void list:create_R()int x;cout请输入链表中的数值,按-1后结束创建x;node*rear=head;while(x!=-1)count+;node*s=new node;s-data=x;rear-next=s;rear=s;rear-next=NULL;cinx; node * list :get_element(int locate)constif(count=0) return 0; elseif(locate=count)return 0;elsenode*p=head; int k=0;while(p!=NULL&knext;k+;return p; void list:show() node*p=head; while(p!=NULL) coutdatanext; error_code list:insert(const int locate,const int i) if(count=0) node*s=new node; s-data=i; s-next=NULL; head-next=s; rear=s; count=1; return success; elseif (locatecount+1) return arrange_error; else node*p=head;int j=0; while(j!=locate-1&p!=NULL) p=p-next;j+; node*s=new node; s-data=i; s-next=p-next; p-next=s; count+;return success; error_code list:charu(const int i) node*p=head;while(p!=NULL&p-next!=NULL) if(p-data=i&inext-data) node*s=new node; s-data=i; s-next=p-next; p-next=s; count+; else p=p-next;if(p-next=NULL) node*s=new node; s-data=i; s-next=NULL; p-next=s; count+; return success; error_code list:delete_element(const int i) node *p=head; int j=0; while(j!=i-1&p!=NULL) p=p-next;j+; if(icount) return arrange_error; node*u=new node; u=p-next; p-next=u-next; delete u; count-; return success; void separate(list&A,list&B) node*LA;node*LB;node*p;node*q;node*u;node*s; LA=A.get_head(); LB=B.get_head(); q=LA;p=LA-next;s=LB; while(p!=NULL) if(p-data%2=0) u=p;p=p-next;q-next=p; s-next=u; s=s-next; else p=p-next;q=q-next; void separate(list&A,list&B,list&C) node*LA;node*LB;node*p;node*q;node*u;node*s; LA=A.get_head(); LB=B.get_head(); q=LA;p=LA-next;s=LB; if(p-data%2=0) u=p;p=p-next;q-next=p; s-next=u; s=s-next; else p=p-next;q=q-next; int list: bingji(list A,list B,list&C) node*LA; node*LB; node*LC; node*a;node*b; LC=C.get_head(); LA=A.get_head(); LB=B.get_head(); a=LA-next;b=LB-next; while(a!=NULL&b!=NULL) if(a-datadata) a=a-next; else if(a-datab-data) b=b-next; else node*c=new node; c-data=a-data;LC-next=c;LC=c;C.count+; a=a-next;b=b-next; LC-next=NULL; return success;int main()int choice;int i;list A; list B;list C;do/显示主菜单 cout n; cout n; cout 主菜单 n; cout n; cout *endl; cout n;cout 1-创建链表 2-求第i个节点指针 n; cout n;cout 3-在第i个节点前插入一个数 4-删除链表中的第i个节点n;cout
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024-2025学年高中语文 第2单元 置身诗境缘景明情 10 登岳阳楼教学设计 新人教版选修《中国古代诗歌散文欣赏》
- 九年级物理上册 第一章 分子动理论与内能 2 内能和热量教学设计 (新版)教科版
- 九年级化学上册 第七单元 燃料及其利用 课题1 燃烧和灭火示范教学设计 (新版)新人教版
- 6 徽 章(教学设计)苏教版二年级下册综合实践活动
- 2024-2025学年高中生物 专题2 课题3 分解纤维素的微生物的分离教学设计 新人教版选修1
- 16《宇宙的另一边》教学设计-2023-2024学年三年级下册语文统编版
- 2023三年级英语上册 Module 3 Places and activities Unit 9 In my room教学设计 牛津沪教版(三起)
- Unit 5 China and the World. Topic 3 Now it is a symbol of England Section D 教学设计 2024-2025学年仁爱科普版英语九年级下册
- 一年级语文上册 第六单元 课文2 语文园地六教学设计 新人教版
- 《活动6 我的鞋子真干净》(教案)-2024-2025学年三年级上册劳动北师大版
- DL-T5434-2021电力建设工程监理规范
- 2024年上海核工程研究设计院股份有限公司招聘笔试冲刺题(带答案解析)
- 房地产营销毕业论文
- GB/T 43943-2024船舶环境噪声
- 材料力学-第五章弯曲应力
- MOOC 医学心理学-北京大学 中国大学慕课答案
- 2023-2024学年下学期高一思想政治课《心理健康与职业生涯》期中模拟考试卷答案
- 我的人工智能导论职业规划
- 幼儿园沙水区培训活动
- 2024年银行考试-兴业银行笔试参考题库含答案
- 山东省潍坊市2023-2024学年一年级下学期期中质量检测数学试题
评论
0/150
提交评论