ImageVerifierCode 换一换
格式:DOCX , 页数:11 ,大小:22.63KB ,
资源ID:23443560      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/23443560.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(数据结构实验三.docx)为本站会员(b****2)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

数据结构实验三.docx

1、数据结构实验三 实验三 线性表操作一 实验目的1 掌握线性表的基本操作:插入、删除、查找。2 掌握链表遍历器的使用方法。二 实验内容1创建线性表类。线性表的存储结构使用链表。2提供操作:自表首插入元素、删除指定元素、搜索表中是否有指定元素、输出链表。3接收键盘录入的一系列整数(例10,25,8,33,60)作为节点的元素值,创建链表。输出链表内容。4输入一个整数(例33),在链表中进行搜索,输出其在链表中的位置。如果不存在输出0。5使用链表遍历器实现链表的反序输出。6创建两个有序链表,使用链表遍历器实现链表的合并。三 知识点介绍 1 线性表(亦作顺序表)是最基本、最简单、也是最常用的一种数据结

2、构。线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。线性表的逻辑结构简单,便于实现和操作。因此,线性表这种数据结构在实际应用中是广泛采用的一种数据结构。 2 链表遍历器有两个共享成员Initialize和Next。Initialize返回一个指针,该指针指向第一个链表节点中所包含的数据,同时把私有变量location设置为指向链表的第一个节点,该变量用来跟踪我们在链表中所处的为位置。成员Next用来调整location,使其指向链表中的下一个节点,并返回指向该数据域的指针。四 源码LinearList.h#ifndef LINEARLI

3、ST_H_INCLUDED#define LINEARLIST_H_INCLUDEDtemplateclass LinearListNode;templateclass LinearList;templateclass LinearListIteratorpublic: T* Initialize(const LinearList& c); T* Next();private: LinearListNode *location;templateclass LinearListNodefriend LinearList;friend LinearListIterator;private: T d

4、ata; LinearListNode *link;templateclass LinearListfriend LinearListIterator;public: LinearList()first = 0; LinearList(); LinearList& Insert(T &x);/自表首插入元素 LinearList& Delete(int k, T& x);/删除指定元素 int Search(const T& x);/搜索表中是否有指定元素 void Output();/输出链表 LinearList& Create(int *a, int n);/创建链表 void Reve

5、rse();/反序输出 void Merge(LinearList that);/链表合并private: LinearListNode *first;#endif / LINEARLIST_H_INCLUDEDLinearList.cpp#include#include LinearList.husing namespace std;templateT* LinearListIterator:Initialize(const LinearList& c) location = c.first; if(location) return &location-data; return 0;temp

6、lateT* LinearListIterator:Next() if(!location) return 0; location = location-link; if(location) return &location-data; return 0;templateLinearList:LinearList() LinearListNode *next; while(first) next = first-link; delete first; first = next; templateLinearList& LinearList:Insert(T &x) LinearListNode

7、 *y = new LinearListNode; y-data = x; y-link = first; first = y; return *this;templateLinearList& LinearList:Delete(int k, T& x) if(k1|!first) cout OUT OF BOUNDS!; LinearListNode *p = first; if(k=1) first = first-link; else LinearListNode *q = first; for(int index=1;indexlink; if(!q|!q-link) cout li

8、nk; q-link = p-link; x = p-data; delete p; return *this;templateint LinearList:Search(const T& x) LinearListNode *current = first; int index = 1; while(current¤t-data!=x) current = current-link; index+; if(current) return index; return 0;templatevoid LinearList:Output() LinearListNode *current

9、; for(current=first;current;current=current-link) if(current-link) cout data ; else cout data endl; /*templatevoid LinearList: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;*/templatevoid

10、LinearList:Reverse() LinearListIterator i; LinearList l; int* x; x = i.Initialize(*this); while(x) l.Insert(*x); x = i.Next(); l.Output();templateLinearList& LinearList:Create(int *a, int n) for(int i=0;in;i+) Insert(ai); return *this;templatevoid LinearList:Merge(LinearList that) LinearListIterator

11、 i1,i2; LinearList l; 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#incl

12、ude #include LinearList.h#include LinearList.cppusing namespace std;int main() int num,z; cout num; int *a = new intnum; cout 请输入数据:; for(int i=0;i ai; cout 创建链表:; LinearList l,j,k; l.Create(a,num); l.Output(); cout 该链表反序输出:; l.Reverse(); cout z; cout 它在表中的位置(不存在为0): l.Search(z) endl; int *b = new i

13、nt5; b0 = 1; b1 = 3; b2 = 5; b3 = 7; b4 = 9; int *c = new int5; c0 = 0; c1 = 2; c2 = 4; c3 = 6; c4 = 8; cout 创建有序链表1:; j.Create(b,5); j.Output(); cout 创建有序链表2:; k.Create(c,5); k.Output(); cout 合并两表:; j.Merge(k); return 0;六 实验心得通过此次实验,我对链表有了更加深刻的理解和认识,对链表的基本操作也更加熟练,同时,学会了使用链表遍历器。在实验中我也意识到,在遇到问题时,还要多请教老师和同学,积极交流,主动提高自身编程能力。

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

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