数据结构之单链表实现队列C++

来源:互联网 发布:电脑程序编程入门 编辑:程序博客网 时间:2024/05/23 01:57
#ifndef STL_SDFLSJ_H#define STL_SDFLSJ_H/************************************************************************//* 单链表实现队列C++/************************************************************************//************************************************************************//* 节点类/************************************************************************///结点定义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 ListSequence{protected:CNode<Elemplent> *front,*rear;int CountTotal;public:ListSequence();~ListSequence();ListSequence(const ListSequence& tempptr);ListSequence<Elemplent>& operator = (const ListSequence& tempptr);void InitListSequence();public:bool IsEmpty();void Clear();int GetLength()const;bool Insert(const Elemplent & tempElemplent);bool Delete();}template<class Elemplent>ListSequence<Elemplent>::ListSequence(){InitListSequence();}template<class Elemplent>ListSequence<Elemplent>::~ListSequence(){delete front;};template <class Elemplent>void ListSequence<Elemplent>::Clear(){while(IsEmpty()!){Delete();}};template <class Elemplent>void ListSequence<Elemplent>::InitListSequence(){front = new CNode<Elemplent>;rear = front;CountTotal = 0;}template <class Elemplent>int ListSequence<Elemplent>::GetLength(){return CountTotal;}template <class Elemplent>bool ListSequence<Elemplent>::IsEmpty(){return front == rear;}template <class Elemplent>bool ListSequence<Elemplent>::Delete(){if (IsEmpty()){return false;}CNode<Elemplent> *temptr = front->next;front->next = temptr->next;delete temptr;CountTotal--;return true;}template <class Elemplent>template <class Elemplent>bool ListSequence<Elemplent>::Insert(const Elemplent & tempElemplent){CNode<Elemplent> *newptr = new CNode<Elemplent>(tempElemplent,NULL);rear->next = newptr;CountTotal++;return true;}template<class Elemplent>ListSequence<Elemplent>::ListSequence(const ListSequence& tempptr){if (tempptr.IsEmpty()){return *this;}InitListSequence();CNode<Elemplent> * tempcopy = tempptr.front;CNode<Elemplent> * tempGet;for (tempcopy->next;tempcopy!=NULL;tempcopy = tempcopy->next ){tempGet =new CNode<Elemplent>;tempGet->data = tempcopy->data;Insert(tempGet);}return *this;}template<class Elemplent>ListSequence<Elemplent>& ListSequence<Elemplent>::operator=(const ListSequence& tempptr){if (tempptr.IsEmpty()){return *this;}if (this == tempptr){return *this;}Clear();InitListSequence();CNode<Elemplent> * tempcopy = tempptr.front;CNode<Elemplent> * tempGet;for (tempcopy->next;tempcopy!=NULL;tempcopy = tempcopy->next ){tempGet =new CNode<Elemplent>;tempGet->data = tempcopy->data;Insert(tempGet);}return *this;}#endif