先上代码!以后再仔细编辑!
头文件
<span style="font-size:18px;">#pragma once template<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个位置插入一个节点,节点的数据为e bool DeleteElem(EleType& e, int index = 1);//在List的第index个位置删除一个节点,删除节点的数据赋给e bool InsertHead(const EleType& e);//在头部插入数据 bool InsertTail(const EleType& e);//在尾部插入数据 bool Clear();//清空List void 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>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/shiwazone/article/details/47000191