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#ifnde
3、f LINEARLIST_H_INCLUDED#define LINEARLIST_H_INCLUDEDtemplateclass LinearListNode;templateclass LinearList;templateclass LinearListIteratorpublic:T* Initialize(const LinearListT* Next();private:LinearListNode *location;templateclass LinearListNodefriend LinearList;friend LinearListIterator;private:T
4、data;LinearListNode *link;templateclass LinearListfriend LinearListIterator;public:LinearList()first = 0;LinearList();LinearList/自表首插入元素LinearList/删除指定元素int Search(const T/搜索表中是否有指定元素void Output();/输出链表LinearList/创建链表void Reverse();/反序输出void Merge(LinearList that);/链表合并private:LinearListNode *first;
5、#endif / LINEARLIST_H_INCLUDEDLinearList.cpp#include#include “LinearList.h“using namespace std;templateT* LinearListIterator:Initialize(const LinearListif(location)return return 0;templateT* LinearListIterator:Next()if(!location)return 0;location = location-link;if(location)return return 0;templateL
6、inearList:LinearList()LinearListNode *next;while(first)next = first-link;delete first;first = next;templateLinearListy-data = x;y-link = first;first = y;return *this;templateLinearListif(k=1)first = first-link;elseLinearListNode *q = first;for(int index=1;indexlink;if(!q|!q-link)cout link;q-link = p
7、-link;x = p-data;delete p;return *this;templateint LinearList:Search(const Tint index = 1;while(currentindex+;if(current)return index;return 0;templatevoid LinearList:Output()LinearListNode *current;for(current=first;current;current=current-link)if(current-link)cout data “;elsecout data void LinearL
8、ist: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;x = i.Initialize(*this);while(x)l.Insert(*x);x = i.Next();l.Output();te
9、mplateLinearListivoid LinearList:Merge(LinearList that)LinearListIterator i1,i2;LinearList l;int *x1,*x2;x1 = i1.Initialize(*this);x2 = i2.Initialize(that);while(x1x1 = 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();#
10、include 五 测试用例main.cpp#include #include “LinearList.h“#include “LinearList.cpp“using namespace std;int main()int num,z;cout num;int *a = new intnum;cout ai;cout l,j,k;l.Create(a,num);l.Output();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,5);j.Output();cout “创建有序链表 2:“;k.Create(c,5);k.Output();cout “合并两表:“;j.Merge(k);return 0;六 实验心得通过此次实验,我对链表有了更加深刻的理解和认识,对链表的基本操作也更加熟练,同时,学会了使用链表遍历器。在实验中我也意识到,在遇到问题时,还要多请教老师和同学,积极交流,主动提高自身编程能力。