双向循环链表中结点类,链表类的定义与实现。
2010-10-15 15:46
477 查看
template<class T> class DouListNode { T data; DouListNode<T>* link; DouListNode<T>* prior; public: DouListNode():link(NULL),prior(NULL){} DouListNode(T value):link(NULL),prior(NULL),data(value){} ~DouListNode(){}; void SetLink(DouListNode<T>* next); void SetPrior(DouListNode<T>* pre); DouListNode<T>* GetLink(); DouListNode<T>* GetPrior(); T& GetData(); }; template<class T> void DouListNode<T>::SetLink(DouListNode<T>* next) { link=next; } template<class T> void DouListNode<T>::SetPrior(DouListNode<T>* pre) { prior=pre; } template<class T> DouListNode<T>* DouListNode<T>::GetLink() { return link; } template<class T> DouListNode<T>* DouListNode<T>::GetPrior() { return prior; } template<class T> T& DouListNode<T>::GetData() { return data; } template<class T> class DouList { DouListNode<T>* head; DouListNode<T>* tail; DouListNode<T>* cur; public: DouList(); ~DouList(){}; bool AddTail(T value); bool AddHead(T value); void RemoveThis(bool direction); void RemoveAll(); void SetBegin(); int GetCount(); void TowardCur(); void BackCur(); DouListNode<T>* GetCur(); DouListNode<T>* GetHead(); DouListNode<T>* GetTail(); void InsertAfter(T value); bool IsEmpty(); T GetNext(); T GetPrior(); }; template<class T> DouList<T>::DouList() { head=tail=new DouListNode<T>; cur=NULL; head->SetLink(head); head->SetPrior(tail); } template<class T> bool DouList<T>::AddTail(T value) { DouListNode<T>* add=new DouListNode<T>(value); tail->SetLink(add); add->SetPrior(tail); tail=tail->GetLink(); tail->SetLink(head); head->SetPrior(add); if(tail!=NULL) { return true; } else return false; } template<class T> bool DouList<T>::AddHead(T value) { DouListNode<T>* add=new DouListNode<T>(value); add->SetPrior(head); add->SetLink(head->GetLink()); head->GetLink()->SetPrior(add); head->SetLink(add); if(tail==head) { tail=head->GetLink(); } if(add!=NULL) { return true; } else return false; } template<class T> void DouList<T>::RemoveThis(bool direction) { if(cur==head) { if(direction==0) cur=cur->GetLink(); if(direction==1) cur=cur->GetPrior(); } DouListNode<T>* preCur=NULL; DouListNode<T>* nextCur=NULL; preCur=cur->GetPrior(); nextCur=cur->GetLink(); preCur->SetLink(cur->GetLink()); nextCur->SetPrior(cur->GetPrior()); if(direction==0) cur=nextCur; if(direction==1) cur=preCur; } template<class T> void DouList<T>::RemoveAll() { SetBegin(); int length=GetCount(); for(int i=0;i<length;i++) { RemoveThis(0); } cur=head; } template<class T> void DouList<T>::SetBegin() { cur=head; } template<class T> int DouList<T>::GetCount() { int num=0; DouListNode<T>* here=cur; while(cur->GetLink()!=here) { cur=cur->GetLink(); ++num; } cur=cur->GetLink(); return num; } template<class T> void DouList<T>::TowardCur() { cur=cur->GetLink(); } template<class T> void DouList<T>::BackCur() { cur=cur->GetPrior(); } template<class T> DouListNode<T>* DouList<T>::GetCur() { return cur; } template<class T> DouListNode<T>* DouList<T>::GetHead() { return head; } template<class T> DouListNode<T>* DouList<T>::GetTail() { return tai; } template<class T> bool DouList<T>::IsEmpty() { return head->GetLink()==head; } template<class T> void DouList<T>::InsertAfter(T value) { DouListNode<T>* add=new DouListNode<T>(value); DouListNode<T>* nextCur=cur->GetLink(); cur->SetLink(add); add->SetLink(nextCur); nextCur->SetPrior(add); add->SetPrior(cur); if(cur==tail) { tail=cur->GetLink(); } } template<class T> T DouList<T>::GetNext() { if(cur==head) cur=cur->GetLink(); T num=cur->GetData(); cur=cur->GetLink(); return num; } template<class T> T DouList<T>::GetPrior() { if(cur==head) cur=cur->GetPrior(); T num=cur->GetData(); cur=cur->GetPrior(); return num; }
相关文章推荐
- 双向链表、双向循环链表的JS实现
- 双向循环链表的实现(粗糙版)
- 数据结构的C实现_双向循环链表
- C语言 双向循环链表的实现和结构 链表 线性表的链式实现
- 数据结构--双向循环链表C实现
- 数据结构学习之双向循环链表的基本操作(非递归实现)
- 双向循环链表的头插、中插、尾插、删除、逆序顺序显示(C++实现)
- C++实现有哨兵的双向循环链表
- 双向循环链表基本操作的实现(C语言)
- 双向循环链表--java实现
- Linux双向循环链表的实现
- (C++版)链表(四)——实现双向循环链表创建、插入、删除等简单操作
- 数据结构(三)---双向循环链表的实现---java版
- c++实现双向循环链表
- Linux双向循环链表的实现
- 数据结构之循环双向链表java实现
- C语言实现双向链表之ADT定义
- C语言实现双向循环链表
- 数据结构基础(12) --双向循环链表的设计与实现
- C语言实现双向循环链表