数据结构之单链表C++(模板)

来源:互联网 发布:艾默生网络被收购 编辑:程序博客网 时间:2024/05/20 18:01
#ifndef STL_LINK_LIST#define STL_LINK_LIST/************************************************************************//* 以下为C++实现单链表(STL模板)/************************************************************************//************************************************************************//* //结点定义/************************************************************************/template <class Elemplent>class CNode{public:Elemplent data;CNode<Elemplent> *next;public:CNode<Elemplent> ();CNode(Elemplent tempElemplent,CNode<Elemplent>* tempNext = NULL); ~CNode();};//结点构造函数template<class Elemplent>CNode<Elemplent>::CNode(){next = NULL;}//结点析构函数template<class Elemplent>CNode<Elemplent>::~CNode(){}//创建新节点函数template<class Elemplent>CNode<Elemplent>::CNode(Elemplent tempElemplent,CNode<Elemplent>* tempNext ){data = tempElemplent;next = tempNext;}/************************************************************************//* //单链表/************************************************************************/template<class Elemplent>class CList{protected:CNode<Elemplent> *head;int CountTotal;public:CList();CList(const CList<Elemplent> & temp);~CList();void initClist();CNode<Elemplent>* GetPtrWithPosition(int position  = 0) const;public:bool IsEmpty();int GetLength();bool Insert(const Elemplent &tempElemplent,int position = 0);bool Delete(int position  = 0);bool SetElemplentWithPosition(const Elemplent &tempElemplent,int position = 0);bool GetElemplentForElemplent(Elemplent &tempElemplent,int position = 0) const;bool Clear();public:CList<Elemplent>&  operator = (const CList<Elemplent> & temp);};//单链表构造函数template<class Elemplent>CList<Elemplent>::CList(){initClist();}//单链表析构函数template<class Elemplent>CList<Elemplent>::~CList(){Clear();delete head;}//单链表赋值构造函数template<class Elemplent>CList<Elemplent>::CList(const CList<Elemplent> & temp){int lenth = temp.GetLength;Elemplent tempplent;initClist();//刚刚忘记初始化了!for (int cocypostion = 1;cocypostion<lenth;cocypostion++){temp.GetElemplentForElemplent(tempplent,cocypostion);Insert(tempplent,cocypostion);}}//单链表赋值操作重载template<class Elemplent>CList<Elemplent>& CList<Elemplent>::operator=(const CList<Elemplent> & temp){if (&temp == this){return NULL;}int length=temp.GetLength;Elemplent tempElemplent;Clear();for (int copyposition = 1 ;copyposition<length;copyposition++){temp.GetElemplentForElemplent(tempElemplent,copyposition);Insert(tempElemplent,copyposition);}return *this;}//单链表初始化单链表template<class Elemplent>void CList<Elemplent>::initClist(){head = new CNode();CountTotal = 0;}//单链表判断是否为空表template<class Elemplent>bool CList<Elemplent>::IsEmpty(){return NULL==head->next;}//单链表用位置返回元素的指针template <class Elemplent>CNode<Elemplent>* CList<Elemplent>::GetPtrWithPosition(int position /* = 0 */)const{int temPositionCursor = 0;CNode<Elemplent> *tempCursorPtr;tempCursorPtr = head;if (position>0||position<CountTotal){return NULL;}else{while(temPositionCursor<=CountTotal && tempCursorPtr !=NULL){if (temPositionCursor == position){break;}temPositionCursor++;tempCursorPtr= tempCursorPtr->next;}return tempCursorPtr;}}//单链表得到链表长度template <class Elemplent>int CList<Elemplent>::GetLength(){return CountTotal;}//单链表插入一个元素template <class Elemplent>bool CList<Elemplent>::Insert(const Elemplent &tempElemplent,int position /* = 0 */){CNode<Elemplent> *tempCursorPtr;if ( position<0||position>CountTotal){return false;}tempCursorPtr = GetPtrWithPosition(position-1);CNode<Elemplent> *newtempNode = new CNode(tempElemplent,tempCursorPtr->next);tempCursorPtr->next = tempElemplent;CountTotal ++;return true;}//单链表用位置设这某个元素的值template <class Elemplent>bool CList<Elemplent>::SetElemplentWithPosition(const Elemplent &tempElemplent,int position /* = 0 */){CNode<Elemplent> *tempCursorPtr;if ( position<0||position>CountTotal){return false;}tempCursorPtr = GetPtrWithPosition(position);tempCursorPtr->data = tempElemplent;return true;}//单链表得到某个位置的元素的值template<class Elemplent>bool CList<Elemplent>::GetElemplentForElemplent(Elemplent &tempElemplent,int position /* = 0 */)const{CNode<Elemplent> *tempCursorPtr;if ( position<0||position>CountTotal){return false;}tempCursorPtr = GetPtrWithPosition(position);tempElemplent = tempCursorPtr->data;return true;}//单链表删除一个元素template<class Elemplent>bool CList<Elemplent>::Delete(int position /* = 0 */){if (position<0||position>CountTotal){return false;}CNode<Elemplent> *tempElemplent;tempElemplent = GetPtrWithPosition(position-1);CNode<Elemplent> *deltePtr = tempElemplent->next;tempElemplent->next = deltePtr->next;delete deltePtr;CountTotal--;return true;}//单链表清空链表template <class Elemplent>bool CList<Elemplent>::Clear(){while(GetLength()>0){Delete(1);}return IsEmpty()== true;}#endif

原创粉丝点击