数据结构实验三.docx

上传人:b****7 文档编号:9564705 上传时间:2023-02-05 格式:DOCX 页数:11 大小:22.63KB
下载 相关 举报
数据结构实验三.docx_第1页
第1页 / 共11页
数据结构实验三.docx_第2页
第2页 / 共11页
数据结构实验三.docx_第3页
第3页 / 共11页
数据结构实验三.docx_第4页
第4页 / 共11页
数据结构实验三.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

数据结构实验三.docx

《数据结构实验三.docx》由会员分享,可在线阅读,更多相关《数据结构实验三.docx(11页珍藏版)》请在冰豆网上搜索。

数据结构实验三.docx

数据结构实验三

实验三线性表操作

一实验目的

1掌握线性表的基本操作:

插入、删除、查找。

2掌握链表遍历器的使用方法。

二实验内容

1创建线性表类。

线性表的存储结构使用链表。

2提供操作:

自表首插入元素、删除指定元素、搜索表中是否有指定元素、输出链表。

3接收键盘录入的一系列整数(例10,25,8,33,60)作为节点的元素值,创建链表。

输出链表内容。

4输入一个整数(例33),在链表中进行搜索,输出其在链表中的位置。

如果不存在输出0。

5使用链表遍历器实现链表的反序输出。

6创建两个有序链表,使用链表遍历器实现链表的合并。

三知识点介绍

1线性表(亦作顺序表)是最基本、最简单、也是最常用的一种数据结构。

线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。

线性表的逻辑结构简单,便于实现和操作。

因此,线性表这种数据结构在实际应用中是广泛采用的一种数据结构。

2链表遍历器有两个共享成员Initialize和Next。

Initialize返回一个指针,该指针指向第一个链表节点中所包含的数据,同时把私有变量location设置为指向链表的第一个节点,该变量用来跟踪我们在链表中所处的为位置。

成员Next用来调整location,使其指向链表中的下一个节点,并返回指向该数据域的指针。

四源码

LinearList.h

#ifndefLINEARLIST_H_INCLUDED

#defineLINEARLIST_H_INCLUDED

template

classLinearListNode;

template

classLinearList;

template

classLinearListIterator{

public:

T*Initialize(constLinearList&c);

T*Next();

private:

LinearListNode*location;

};

template

classLinearListNode{

friendLinearList;

friendLinearListIterator;

private:

Tdata;

LinearListNode*link;

};

template

classLinearList{

friendLinearListIterator;

public:

LinearList(){first=0;}

~LinearList();

LinearList&Insert(T&x);//自表首插入元素

LinearList&Delete(intk,T&x);//删除指定元素

intSearch(constT&x);//搜索表中是否有指定元素

voidOutput();//输出链表

LinearList&Create(int*a,intn);//创建链表

voidReverse();//反序输出

voidMerge(LinearListthat);//链表合并

private:

LinearListNode*first;

};

 

#endif//LINEARLIST_H_INCLUDED

LinearList.cpp

#include

#include"LinearList.h"

usingnamespacestd;

template

T*LinearListIterator:

:

Initialize(constLinearList&c){

location=c.first;

if(location)

return&location->data;

return0;

}

template

T*LinearListIterator:

:

Next(){

if(!

location)

return0;

location=location->link;

if(location)

return&location->data;

return0;

}

template

LinearList:

:

~LinearList(){

LinearListNode*next;

while(first){

next=first->link;

deletefirst;

first=next;

}

}

template

LinearList&LinearList:

:

Insert(T&x){

LinearListNode*y=newLinearListNode;

y->data=x;

y->link=first;

first=y;

return*this;

}

template

LinearList&LinearList:

:

Delete(intk,T&x){

if(k<1||!

first)

cout<<"OUTOFBOUNDS!

";

LinearListNode*p=first;

if(k==1)

first=first->link;

else{

LinearListNode*q=first;

for(intindex=1;index

q=q->link;

if(!

q||!

q->link)

cout<<"OUTOFBOUNDS!

";

p=q->link;

q->link=p->link;

}

x=p->data;

deletep;

return*this;

}

template

intLinearList:

:

Search(constT&x){

LinearListNode*current=first;

intindex=1;

while(current&¤t->data!

=x){

current=current->link;

index++;

}

if(current)

returnindex;

return0;

}

template

voidLinearList:

:

Output(){

LinearListNode*current;

for(current=first;current;current=current->link){

if(current->link)

cout<data<<"->";

else

cout<data<

}

}

/*

template

voidLinearList:

:

Reverse(){

LinearListNode*p1,*p2,*p3,*current;

p1=first;

p2=first->link;

while(p2){

p3=p2->link;

p2->link=p1;

p1=p2;

p2=p3;

}

first->link=0;

first=p1;

}

*/

template

voidLinearList:

:

Reverse(){

LinearListIteratori;

LinearListl;

int*x;

x=i.Initialize(*this);

while(x){

l.Insert(*x);

x=i.Next();

}

l.Output();

}

template

LinearList&LinearList:

:

Create(int*a,intn){

for(inti=0;i

Insert(a[i]);

return*this;

}

template

voidLinearList:

:

Merge(LinearListthat){

LinearListIteratori1,i2;

LinearListl;

int*x1,*x2;

x1=i1.Initialize(*this);

x2=i2.Initialize(that);

while(x1&&x2){

if(*x1>*x2){

l.Insert(*x1);

x1=i1.Next();

}

else{

l.Insert(*x2);

x2=i2.Next();

}

}

while(x1){

l.Insert(*x1);

x1=i1.Next();

}

while(x2){

l.Insert(*x2);

x2=i2.Next();

}

l.Output();

}

#include

五测试用例

main.cpp

#include

#include"LinearList.h"

#include"LinearList.cpp"

 

usingnamespacestd;

intmain()

{

intnum,z;

cout<<"请输入数据个数:

";

cin>>num;

int*a=newint[num];

cout<<"请输入数据:

";

for(inti=0;i

cin>>a[i];

cout<<"创建链表:

";

LinearListl,j,k;

l.Create(a,num);

l.Output();

cout<<"该链表反序输出:

";

l.Reverse();

cout<<"输入一个整数:

";

cin>>z;

cout<<"它在表中的位置(不存在为0):

"<

<

int*b=newint[5];

b[0]=1;

b[1]=3;

b[2]=5;

b[3]=7;

b[4]=9;

int*c=newint[5];

c[0]=0;

c[1]=2;

c[2]=4;

c[3]=6;

c[4]=8;

cout<<"创建有序链表1:

";

j.Create(b,5);

j.Output();

cout<<"创建有序链表2:

";

k.Create(c,5);

k.Output();

cout<<"合并两表:

";

j.Merge(k);

return0;

}

六实验心得

通过此次实验,我对链表有了更加深刻的理解和认识,对链表的基本操作也更加熟练,同时,学会了使用链表遍历器。

在实验中我也意识到,在遇到问题时,还要多请教老师和同学,积极交流,主动提高自身编程能力。

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 文学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1