单链表实验报告.docx
《单链表实验报告.docx》由会员分享,可在线阅读,更多相关《单链表实验报告.docx(17页珍藏版)》请在冰豆网上搜索。
单链表实验报告
数据结构实验报告
姓名
章杰
学号
2012211496
专业班级
通信工程12-1
指导教师
实验时间
2013/4/22
实验地点
四号机房
(实验名称)
1.实验目标
a)熟练掌握线性表的链式存储结构。
b)熟练掌握单链表的有关算法设计。
c)根据具体问题的需要,设计出合理的表示数据的顺序结构,并设计相关算法。
2.实验内容和要求
本次实验中的链表结构指带头结点的单链表;
单链表结构和运算定义,算法的实现以库文件方式实现,不得在测试主程序中直接实现;
实验程序有较好可读性,各运算和变量的命名直观易懂,符合软件工程要求;
程序有适当的注释。
3.数据结构设计
#include"iostream.h"
typedefintelementtype;
typedefstructsnode{
elementtypedata;
structsnode*next;
}node;
classlist{
public:
list();
//~list();
intlength()const;
intget_element(constinti,elementtype&x)const;
node*locate(constelementtypex)const;
intInsert(constinti,constelementtypex);
voidinsert(constelementtypex);
intdelete_element(constinti);
node*get_head(){returnhead;}
voidcreat_R();
voidcreat_H();
voidprint();
voidlist:
:
interset(lista,listb,list&c);
boollist:
:
delete1();
voidlist:
:
destroy();
voidlist:
:
outnode(list&B,node*&q);
voidlist:
:
combine(list&A,list&B);
private:
intcount;
node*head;
};
list:
:
list(){
head=newnode;
head->next=NULL;
count=0;
}
intlist:
:
length()const{
node*p=head->next;
intn=0;
while(p!
=NULL){
n++;
p=p->next;
}
returnn;
}
intlist:
:
get_element(constinti,elementtype&x)const{
node*p=head->next;intj=1;
while(p!
=NULL&&j!
=i){
p=p->next;j++;
}
if(p==NULL)return0;
x=p->data;return1;
}
node*list:
:
locate(constelementtypex)const{
node*p=head->next;
while(p!
=NULL)
if(p->data==x)returnp;
elsep=p->next;
returnNULL;
}
intlist:
:
Insert(constinti,constelementtypex){
node*p=head;intj=0;
while(j!
=i-1&&p!
=NULL){
p=p->next;j++;
}
if(i<1||i>count+1)return0;
node*s=newnode;
s->data=x;
s->next=p->next;
p->next=s;
count++;
return1;
}
intlist:
:
delete_element(constinti){
node*p=head;intj=0;
while(j!
=i-1&&p!
=NULL){
p=p->next;j++;
}
if(i<1||i>count)return0;
node*u=p->next;
p->next=u->next;
deleteu;
count--;
return1;
}
voidlist:
:
creat_R(){
elementtypex;
cout<<"请输入数据元素:
(以9999结束)"<cin>>x;
node*rear=head;
while(x!
=9999){
count++;
node*s=newnode;
s->data=x;
rear->next=s;
rear=s;
rear->next=NULL;
cin>>x;
}
}
voidlist:
:
creat_H(){
elementtypex;
cout<<"请输入数据元素:
(以9999结束)"<cin>>x;
while(x!
=9999){
count++;
node*s=newnode;
s->data=x;
s->next=head->next;
head->next=s;
cin>>x;
}
}
voidlist:
:
print(){
node*p=head->next;
intj=1;
while(j<=count){
cout<data<<'\t';
p=p->next;j++;
}
cout<}
voidlist:
:
insert(constelementtypex)
{
node*p=newnode;
p=head->next;
while(p->data<=x)
p=p->next;
node*u=newnode;
u->data=p->data;
p->data=x;
u->next=p->next;
p->next=u;
count++;
}
voidlist:
:
interset(lista,listb,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->datadata)pa=pa->next;
elseif(pa->data>pb->data)pb=pb->next;
else{
u=newnode;
u->data=pa->data;
rc->next=u;
rc=u;
c.count++;
pa=pa->next;
pb=pb->next;
}
}
rc->next=NULL;
}
boollist:
:
delete1()
{
node*p=head->next;
if(p==NULL)returnfalse;
while(p->next!
=NULL)
{
if(p->data==p->next->data)
{
node*u=newnode;
u=p->next;
p->next=u->next;
deleteu;
count--;
}
elsep=p->next;
}
returntrue;
}
voidlist:
:
destroy()
{
node*p,*u;
p=head->next;
while(p)
{
u=p->next;
deletep;
p=u;
}
cout<<"单链表已销毁"<}
voidlist:
:
outnode(list&B,node*&q)
{
q=B.head->next;
B.head->next=q->next;
}
voidlist:
:
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->next=p;
}
else
{
while(p->next!
=NULL)
{
if(q->data>p->data&&q->data<=p->next->data)
{
m=p->next;
p->next=q;
q->next=m;
break;
}
elseif(q->data>p->next->data)
{
p=p->next;
}
}
if(p->next==NULL)
{
p->next=q;
break;
}
}
}
B.head=NULL;
}
4.算法设计
(除书上给出的基本运算(这部分不必给出设计思想),其它实验内容要给出算法设计思想)
5.运行和测试
6.总结和心得
本次实验的内容较上次多,难度也明显增加了,所幸的是,自己课下空余时间有提前编写,才得以按时上交,由此可见,实验并不是课上那两三个小时的事情,更多的是课下我们下的功夫!
虽然实验难度加大了,但是通过和一些同学的讨论,还是得以顺利解决了!
看着自己的劳动获得了收获,真的很有成就感!
再接再厉!
[7.附录]
(源代码清单。
纸质报告不做要求。
电子报告,可直接附源文件,删除编译生成的所有文件)
源代码见文件夹:
linkedList