付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 LI
3、NEARLIST_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
4、 data; 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 Reverse()
5、;/ 反序输出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;templateT* Li
6、nearListIterator: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 *y = new LinearListN
7、ode;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;elseLinearListNode *q = first;for(int index=1;indexlink;if(!q|!q-link).cout link;q-link = p-link;x = p-data;dele
8、te 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;.for(current=first;current;current=current-lin
9、k)if(current-link)cout data ;elsecout 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 LinearList:Reverse()LinearListIterator i;LinearList l;int* x;
10、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 i1,i2;LinearList l;int *x1,*x2;.x1 = i1.Initialize(*this);x2 = i2.Initia
11、lize(that);while(x1&x2)if(*x1*x2)l.Insert(*x1);x1 = i1.Next();elsel.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.cppusing namespace std;int main()int num,z;c
12、out 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 int5;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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026北京学校招聘3人笔试题库及参考答案详解【基础题】
- 2026西安市浐灞实验小学教师招聘参考题库含答案详解【考试直接用】
- 2026北京大学电子学院招聘劳动合同制工作人员2人模拟试卷及答案详解(真题汇编)
- 2026江苏盐城市大丰区大中街道招聘公益性岗位3人笔试题库及完整答案详解【名师系列】
- 浙江省宁波市慈溪市2023-2024学年四年级下学期语文期末试卷(含答案)
- 破坏文物面试题目及答案
- 菏泽语文面试试题及答案
- 地生会考的试题及答案
- 农村光伏扶贫电站后期运维对发电效率的影响研究报告
- 一级市场环境变化中的耐心资本培育机制
- 2025年中国铁道科学研究院集团有限公司招聘(178人)笔试历年参考题库附带答案详解
- 2026年成人高考政治备考练习试题及答案
- 2026年一年级升二年级语文暑假衔接作业(纯作业打印版)
- en10346:2009连续热浸镀钢带产品标准
- 2026年健康评估期末复习过关检测附答案详解【黄金题型】
- 医院疼痛科管理制度(2026版)
- 冠梁拆除施工方案
- 2026年精神病医用药情况试题及答案
- 东方财富社招测评题库
- GB 46768-2025有限空间作业安全技术规范
- (已压缩)广东省工程勘察设计服务成本取费导则(2024版)
评论
0/150
提交评论