链表(单链表双向循环)实验报告讲解_第1页
链表(单链表双向循环)实验报告讲解_第2页
链表(单链表双向循环)实验报告讲解_第3页
免费预览已结束,剩余15页可下载查看

下载本文档

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

文档简介

1、数据结构 实验报告T1223-3-21余帅实验实验题目:仅仅做链表部分难度从上到下1. 双向链表,带表头,线性表常规操作。2. 循环表,带表头,线性表常规操作。3. 单链表,带表头,线性表常规操作。 实验目的:了解和掌握线性表的逻辑结构和链式存储结构,掌握单链表 的基本算法及相关的时间性能分析。实验要求:常规操作至少有:1. 数据输入或建立2. 遍历3. 插入4. 删除必须能多次反复运行实验主要步骤:1、分析、理解给出的示例程序。2、调试程序,并设计输入数据,测试程序的如下功能:1.数据输入或建立2.遍历3. 插入4. 删除单链表示意图:创建headdatadataNULLhead删除data

2、datadatadataNULLheadNULL双向循环链表示意图:创建删除程序代码 :/单链表#include#includeconst MAX=5;enum returninfosuccess,fail,overflow,underflow,range_error;int defaultdataMAX=11,22,33,44,55;class nodepublic:int data;node *next;class linklistprivate:node *headp;protected:int count;public:linklist();linklist();bool empty(

3、);void clearlist();returninfo create(void);returninfo insert(int position,const int &item);returninfo remove(int position) ;returninfo traverse(void);linklist:linklist()headp = new node;headp-next = NULL;count=0;linklist:linklist()clearlist();delete headp;bool linklist:empty()if(headp-next=NULL)retu

4、rn true;elsereturn false;void linklist:clearlist()node *searchp=headp-next,*followp=headp;while(searchp-next!=NULL) followp=searchp; searchp=searchp-next; delete followp;headp-next = NULL;count = 0;returninfo linklist:create()node *searchp=headp,*newnodep;for(int i=0;idata = defaultdatai;newnodep-ne

5、xt = NULL; searchp-next = newnodep; searchp = searchp-next; count+;searchp-next = NULL; traverse();return success;插入一returninfo linklist:insert(int position,const int &item) / 个结点if(position=count)return range_error;node *newnodep=new node,*searchp=headp-next,*followp=headp;for(int i=1; inext;给数据赋值

6、注意此处的次序相关性计数器加一newnodep-data=item; / newnodep-next=followp-next; / followp-next=newnodep;count+; /return success;returninfo linklist:remove(int position) / 删除一个结点if(empty()return underflow;if(position=count+1)return range_error;node *searchp=headp-next,*followp=headp; / 这里两个指针的初始 值设计一前一后for(int i=1;

7、 inext;删除结点的实际语句释放该结点计数器减一 followp-next=searchp-next; / delete searchp; / count-;/return success;returninfo linklist:traverse(void)node *searchp;if(empty()return underflow;searchp = headp-next;cout 连表中的数据为: endl; while(searchp!=NULL)coutdatanext;coutendl;return success;class interfacebasepublic:link

8、list listface; / 定义一个对象 Cskillstudyonface void clearscreen(void);void showmenu(void);void processmenu(void);void interfacebase:clearscreen(void)system(cls);void interfacebase:showmenu(void)cout=endl;cout功能菜单endl;cout1.创建链表endl;cout2.增加结点endl;cout3.删除结点endl;cout4.遍历链表endl;cout0.结束程序endl;cout=endl; co

9、utmenuchoice;switch(menuchoice) / 根据用户的选择进行相应的操作case 1:returnvalue=listface.create();if(returnvalue=success)cout 链表创建已完成 endl;break;case 2:cout 请输入插入位置: position;cout 请输入插入数据: item;returnvalue = listface.insert(position,item);if(returnvalue=range_error)cout 数据个数超出范围 endl;elsecout 操作成功! endl;break;ca

10、se 3:cout 输入你要删除的位置: position;returnvalue = listface.remove(position); if(returnvalue=underflow) cout 链表已空 endl;else if(returnvalue=range_error) cout 删除的数据位置超区范围 endl;elsecout 操作成功! endl; break;case 4:listface.traverse();break;case 0:coutendlendl 您已经成功退出本系统,欢迎再次使 用!endl;system(pause);exit(1);default

11、:cout 对不起,您输入的功能编号有错 !请重新输入 !endl; break; void main() interfacebase interfacenow; linklist listnow; system(color f0); interfacenow.clearscreen(); while(1) interfacenow.showmenu(); cessmenu(); system(pause); interfacenow.clearscreen(); /* 功能:用双向循环链表存储数据1. 创建链表2. 增加结点3. 删除结点4. 遍历链表制作人:

12、余帅内容: 239行*/#include#includeconst MAX=5;enum returninfosuccess,fail,overflow,underflow,range_error;int defaultdataMAX=11,22,33,44,55;class nodepublic:int data;node * next; /指向后续节点node * pre; /指向前面的节点;class linklistprivate:node *headp;protected:int count;public:linklist();linklist();bool empty();void

13、 clearlist();returninfo create(void);returninfo insert(int position,const int &item);returninfo remove(int position) ;returninfo traverse(void);linklist:linklist()headp = new node;headp-next = NULL;headp-pre = NULL;count=0;linklist:linklist()clearlist();delete headp;bool linklist:empty()if(headp-nex

14、t=NULL)return true;elsereturn false;void linklist:clearlist()node *searchp=headp-next,*followp=headp;while(searchp-next!=NULL) followp=searchp; searchp=searchp-next; delete followp;headp-next = NULL;headp-pre = NULL;count = 0;returninfo linklist:create()node *searchp=headp,*newnodep;for(int i=0;idat

15、a = defaultdatai; newnodep-next = NULL;searchp-next = newnodep; newnodep-pre = searchp;searchp = searchp-next;count+;searchp-next = headp;headp-pre = searchp;traverse();return success;插入一returninfo linklist:insert(int position,const int &item) / 个结点if(positioncount+1)return range_error;node *newnode

16、p=new node;node *searchp=headp-next,*followp=headp;for(int i=1; inext;newnodep-data=item; / newnodep-next = searchp;searchp-pre = newnodep;followp-next = newnodep;newnodep-pre = followp;count+; /return success;returninfo linklist:remove(int position) /if(empty() return underflow;if(position=count+1)

17、return range_error;node *searchp=headp-next,*followp=headp; 值设计一前一后for(int i=1; inext;followp-next=searchp-next; / searchp-next-pre = followp;delete searchp; / count-;/return success;returninfo linklist:traverse(void)给数据赋值计数器加一删除一个结点/ 这里两个指针的初始删除结点的实际语句释放该结点计数器减一node *searchp1,*searchp2; if(empty()

18、return underflow;searchp1 = headp;searchp2 = headp;cout 连表中的数据为: endl;coutnext!=headp ) searchp1 = searchp1 -next; cout data ;coutendl;coutpre!=headp ) searchp2 = searchp2 -pre; cout data ;coutendl;return success;class interfacebasepublic:linklist listface; /定义一个对象 Cskillstudyonfacevoid clearscreen(

19、void);void showmenu(void);void processmenu(void);void interfacebase:clearscreen(void)system(cls);void interfacebase:showmenu(void)cout=endl;cout功能菜单endl;cout1.创建链表endl;cout2.增加结点endl;cout3.删除结点endl;cout4.遍历链表endl;cout0.结束程序endl;cout=endl; coutmenuchoice;switch(menuchoice) / 根据用户的选择进行相应的操作case 1:retu

20、rnvalue=listface.create();if(returnvalue=success)cout 链表创建已完成 endl;break;case 2:cout 请输入插入位置: position;cout 请输入插入数据: item;returnvalue = listface.insert(position,item);if(returnvalue=range_error)cout 数据个数超出范围 endl;elsecout 操作成功! endl;break;case 3:cout 输入你要删除的位置: position;returnvalue = listface.remove(position); if(returnvalue=underflow) cout 链表已空 endl;else if(returnvalue=range_error)cout 删除的数据位置超区范围 endl;elsecout 操作成功! endl;break;case 4:listface.traverse();break;case 0:co

温馨提示

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

评论

0/150

提交评论