数据结构实验三

上传人:hs****ma 文档编号:489945808 上传时间:2023-07-21 格式:DOC 页数:12 大小:118KB
返回 下载 相关 举报
数据结构实验三_第1页
第1页 / 共12页
数据结构实验三_第2页
第2页 / 共12页
数据结构实验三_第3页
第3页 / 共12页
数据结构实验三_第4页
第4页 / 共12页
数据结构实验三_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《数据结构实验三》由会员分享,可在线阅读,更多相关《数据结构实验三(12页珍藏版)》请在金锄头文库上搜索。

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;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;

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 演讲稿/致辞

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号