1、单链表实验报告数据结构实验报告姓名章杰学号2012211496专业班级通信工程12-1指导教师实验时间2013/4/22实验地点四号机房(实验名称)1. 实验目标a) 熟练掌握线性表的链式存储结构。b) 熟练掌握单链表的有关算法设计。c) 根据具体问题的需要,设计出合理的表示数据的顺序结构,并设计相关算法。2. 实验内容和要求本次实验中的链表结构指带头结点的单链表;单链表结构和运算定义,算法的实现以库文件方式实现,不得在测试主程序中直接实现;实验程序有较好可读性,各运算和变量的命名直观易懂,符合软件工程要求;程序有适当的注释。3. 数据结构设计#includeiostream.htypedef
2、 int elementtype;typedef struct snode elementtype data; struct snode* next;node; class listpublic: list(); / list(); int length() const; int get_element(const int i,elementtype &x)const; node* locate(const elementtype x)const; int Insert(const int i,const elementtype x); void insert(const elementtyp
3、e x); int delete_element(const int i); node* get_head()return head; void creat_R(); void creat_H(); void print(); void list:interset (list a,list b,list& c); bool list:delete1(); void list:destroy(); void list:outnode(list &B,node* &q); void list:combine (list &A,list &B);private: int count; node* h
4、ead;list:list() head=new node; head-next=NULL; count=0;int list:length()const node* p=head-next; int n=0; while(p!=NULL) n+; p=p-next; return n;int list:get_element(const int i,elementtype &x)const node* p=head-next; int j=1; while(p!=NULL&j!=i) p=p-next;j+; if(p=NULL) return 0; x=p-data; return 1;n
5、ode* list:locate(const elementtype x)const node* p=head-next; while(p!=NULL) if(p-data=x) return p; else p=p-next; return NULL;int list:Insert(const int i,const elementtype x) node* p=head; int j=0; while(j!=i-1&p!=NULL) p=p-next;j+; if(icount+1) return 0; node* s=new node; s-data=x; s-next=p-next;
6、p-next=s; count+; return 1;int 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 0; node* u=p-next; p-next=u-next; delete u; count-; return 1;void list:creat_R() elementtype x; cout请输入数据元素:(以9999结束)x; node* rear=head; while(x!=9999) count+;
7、node* s=new node; s-data=x; rear-next=s; rear=s; rear-next=NULL; cinx; void list:creat_H() elementtype x; cout请输入数据元素:(以9999结束)x; while(x!=9999) count+; node* s=new node; s-data=x; s-next=head-next; head-next=s; cinx; void list:print() node* p=head-next; int j=1; while(j=count) coutdatanext;j+; cout
8、next; while(p-data next; node*u=new node; u-data=p-data; p-data =x; u-next=p-next ; p-next =u; count+;void list:interset (list a,list b,list& c) node*pa,*pb,*rc,*u; rc=c.get_head (); pa=a.get_head ()-next ; pb=b.get_head ()-next ; while( pa!=NULL& pb!=NULL) if(pa-data data ) pa=pa-next ; else if ( p
9、a-data pb-data ) pb=pb-next ; else u=new node; u-data =pa-data ; rc-next=u ; rc=u; c.count +; pa=pa-next ; pb=pb-next ; rc-next =NULL;bool list:delete1() node*p=head-next; if(p=NULL) return false; while (p-next !=NULL) if(p-data =p-next-data ) node*u=new node; u=p-next ; p-next =u-next ; delete u; c
10、ount-; else p=p-next ; return true; void list:destroy() node *p, *u; p=head-next; while(p) u=p-next; delete p; p=u; cout单链表已销毁next; B.head-next=q-next;void list:combine (list &A,list &B) node *p,*q,*m; p=A.head-next; while(B.length()!=0) outnode(B,q); if(A.head-next-data =q-data) A.head -next =q; q-
11、next =p; else while(p-next!=NULL) if(q-data p-data & q-data next-data) m=p-next; p-next=q; q-next=m; break; else if(q-datap-next-data) p=p-next; if(p-next=NULL) p-next=q; break; B.head=NULL;4. 算法设计(除书上给出的基本运算(这部分不必给出设计思想),其它实验内容要给出算法设计思想)5. 运行和测试6. 总结和心得 本次实验的内容较上次多,难度也明显增加了,所幸的是,自己课下空余时间有提前编写,才得以按时上交,由此可见,实验并不是课上那两三个小时的事情,更多的是课下我们下的功夫!虽然实验难度加大了,但是通过和一些同学的讨论,还是得以顺利解决了!看着自己的劳动获得了收获,真的很有成就感!再接再厉!7. 附录(源代码清单。纸质报告不做要求。电子报告,可直接附源文件,删除编译生成的所有文件) 源代码见文件夹:linkedList
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1