C++ 模版类的单向链式线性表
来源:互联网 发布:师洋淘宝店骂人 编辑:程序博客网 时间:2024/06/05 09:43
先上代码!以后再仔细编辑!
头文件
<span style="font-size:18px;">#pragma oncetemplate<typename EleType>class ChainList{public:struct Node{EleType _data;Node* _next; Node(){ _next = nullptr; }Node(EleType data){ _data = data; _next = nullptr; }};ChainList();~ChainList();bool GetElem(EleType& e, int index=1)const;//得到List中第index个元素,把元素的_data赋给e;bool InsertElem(const EleType e, int index=1);//在List的第index个位置插入一个节点,节点的数据为ebool DeleteElem(EleType& e, int index = 1);//在List的第index个位置删除一个节点,删除节点的数据赋给ebool InsertHead(const EleType& e);//在头部插入数据bool InsertTail(const EleType& e);//在尾部插入数据bool Clear();//清空Listvoid ShowList()const;//显示List的所有元素的数据private:bool Empty()const;//判断List是否为空//在List中查找第index个位置的节点,把该节点的地址赋给n,此处需传入指针的引用,才能保证n可以被修改,不然只能保证*n可以被修改,也就是n指向的节点可以被修改bool Find(int index,Node*& n)const;bool CheckIndex(int index)const;//检查List是否为空,index是否合法Node* Head;//头节点Node* Tail;//尾节点int Length;</span><pre name="code" class="cpp">
实现文件.cpp
<span style="font-size:18px;"></span><pre name="code" class="cpp"><span style="font-size:18px;">#include "ChainList.h"#include <iostream>using namespace std;template<typename EleType>bool ChainList<EleType>::CheckIndex(int index) const{if (Empty()){cout << "Find: the List is Empty!\n";return false;}if (index<1 || index>Length){cout << "the index is invalid!\n";return false;}return true;}template<typename EleType>bool ChainList<EleType>::Find(int index,Node*& n)const//index [1,Length];{ if (CheckIndex(index)) {int i = 1;Node * temp = Head;while (i < index){temp = temp->_next;++i;}n = temp;return true;}return false;}template<typename EleType>void ChainList<EleType>::ShowList() const{Node * temp=nullptr ;if (Empty()){cout << "The list is empty!\n";}for (int i=1;i<=Length;++i){Find(i, temp);cout << temp->_data << " ";}cout << endl; }template<typename EleType>bool ChainList<EleType>::Clear(){if (Empty()){return true;}else{while (Length){EleType m;DeleteElem(m);}return true;}}template<typename EleType>bool ChainList<EleType>::DeleteElem(EleType& e,int index=1 ){if (CheckIndex(index)){Node* temp = nullptr;Node* pre_temp = nullptr;if (Find(index, temp)){if (index == 1){Find(index + 1, Head);}else{if (index == Length){Find(index - 1, Tail);}else{Find(index - 1, pre_temp);pre_temp->_next = temp->_next;}}e = temp->_data;delete temp;--Length;return true;}return false;}return false;}template<typename EleType>bool ChainList<EleType>::InsertElem(const EleType e,int index ){Node *insertNode=new Node(e); if (Empty()) { if (index < 1) return false; Head = Tail = insertNode; insertNode->_next = nullptr; ++Length; return true; } if (index==1) { insertNode->_next = Head; Head = insertNode; ++Length; return true; } if (index==Length+1) { Tail->_next = insertNode; insertNode->_next = nullptr; Tail = insertNode; ++Length; return true; }Node *temp=nullptr; if (Find(index-1,temp)) { insertNode->_next = temp->_next; temp->_next = insertNode; ++Length; return true; } return false;}template<typename EleType>bool ChainList<EleType>::GetElem(EleType& e,int index) const{if (CheckIndex(index)){Node* temp = nullptr;if (Find(index, temp))e = temp->_data;return true;}return false;}template<typename EleType>ChainList<EleType>::~ChainList(){Clear();}template<typename EleType>ChainList<EleType>::ChainList() :Length(0), Head(nullptr), Tail(nullptr){ } template<typename EleType>bool ChainList<EleType>::Empty() const{return(Length==0);}template<typename EleType>bool ChainList<EleType>::InsertTail(const EleType& e){return InsertElem(e, Length + 1);}template<typename EleType>bool ChainList<EleType>::InsertHead(const EleType& e){return InsertElem(e);}</span>
};
0 0
- C++ 模版类的单向链式线性表
- C++ 模版类的单向循环链式线性表
- 数据结构----纯C语言实现线性表的单向链式存储结构
- 二.单向链式存储线性表
- 线性表的链式存储C语言版
- 大话数据结构四:线性表的链式存储结构(单向循环链表)
- 数据结构:线性表的链式存储(单向链表)--Java实现
- 线性表的Java实现--链式存储(单向链表)
- 数据结构_线性表_链式存储_单向循环链表的基本操作
- 数据结构复习——线性表的链式存储实现(单向链表)
- 大话数据结构四:线性表的链式存储结构(单向循环链表)
- 线性表(List)---链式存储结构(单向链表)
- 用c写的线性表的链式实现
- 线性表的链式存储及相关操作 C语言版
- 线性表的链式存储(单链表)C语言实现
- 数据结构 C语言实现 线性表的链式实现
- c语言线性表的链式表示和实现
- C语言实现线性表的链式存储结构
- ubuntu jdk环境配置
- Objective-C Runtime 运行时之四:Method Swizzling
- Linux下php安装
- IE8下无法打开Internet站点,已终止操作问题解析
- hihoCoder - 1175 - 拓扑排序·二 (拓扑排序的应用)
- C++ 模版类的单向链式线性表
- spring四种依赖注入方式
- Volley全解析
- android 实现代码混淆
- 深入理解HTTP Session
- WebKit的一些笔记3(基础篇)
- tabbarcontroller切换
- Listview 去除顶部阴影
- MathType在word中的使用方法