雙向循環鏈表類的C++實現

至於DouListNode的實現請見:http://my.oschina.net/u/181847/blog/42959node

#pragma once
#include"DouListNode.h"
#include<iostream>
//-------------------------------------------------------------------------
template<class T>
class DouList
{
private:
 DouListNode<T>* head;
 DouListNode<T>* tail;
 DouListNode<T>* cur;
public:
 DouList(void);
 ~DouList(void){};
 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 Is Empty();
 T GetNext();
 T GetPrior();ios

};
//-------------------------------------------------------------------------
template<class T>
T DouList<T>::GetPrior()
{//get the value in DouListNode where pointer cur point to prior
 if(cur==head)
 {
  cur=cur->GetPrior();
 }
 T num=cur->GetData();
 cur=cur->GetPrior();
 return num;
}
//-------------------------------------------------------------------------
template<class T>
T DouList<T>::GetNext()
{//get the value in DouListNode where pointer cur point to next
 if(cur==head)
 {
  cur=cur->GetLink();
 }
 T num=cur->GetData();
 cur=cur->GetLink();
 return num;
}
//-------------------------------------------------------------------------
template<class T>
void DouList<T>::InsertAfter(T value)
{//pointer cur may be the head,tail,or between them
 if(cur==tail)
 {
  AddTail(value);
 }
 else if(cur==head)
 {
  AddHead(value);
 }
 else
 {
 DouListNode<T> temp=new DouListNode<T>(value);
 temp->SetLink(cur->GetLink());
 cur->GetLink()->SetPrior(temp);
 cur->SetLink(temp);
 temp->SetPrior(cur);
 }
}
//-------------------------------------------------------------------------
template<class T>
bool DouList<T>::IsEmpty()
{
 return head->GetLink()==head;
}
//-------------------------------------------------------------------------
template<class T>
DouListNode<T>* DouList<T>::GetTail()
{
 return tail;
}
//-------------------------------------------------------------------------
template<class T>
DouListNode<T>* DouList<T>::GetHead()
{
 return head;
}
//-------------------------------------------------------------------------
template<class T>
DouListNode<T>* DouList<T>::GetCur()
{
 return cur;
}
//-------------------------------------------------------------------------
template<class T>
void DouList<T>::BackCur()
{
 cur=cur->GetPrior();
}
//-------------------------------------------------------------------------
template<class T>
void DouList<T>::TowardCur()
{
 cur=cur->GetLink();
}
//-------------------------------------------------------------------------
template<class T>
int  DouList<T>::GetCount()
{//count the numbers of DouListNode in DouList
 DouListNode<T>* temp=cur;
 int i=0;
 while(temp->GetLink()!=cur)
 {
  i++;
  temp=temp->GetLink();
 }
 return i;
}
//-------------------------------------------------------------------------
template<class T>
void DouList<T>::SetBegin()
{
 cur=head;
}
//-------------------------------------------------------------------------
template<class T>
void DouList<T>::RemoveAll()
{//delete all nodes in DouList
 SetBegin();
 int length=GetCount();
 for(int i=0;i<length;i++)
 {
  RemoveThis(0);
 }
 cur=head;
}
//-------------------------------------------------------------------------
template<class T>
void DouList<T>::RemoveThis(bool direction)
{
 //pointer head can not be deleted
 if(cur==head)
 {
  //move direction:default lift to right
  if(direction==0)
  {
   cur=cur->GetLink();
  }
  else
  {
   cur=cur->GetPrior();
  }
 }.net

 DouListNode<T>* preCur=NULL;
 DouListNode<T>* nextCur=NULL;
 preCur=cur->GetPrior();
 nextCur=cur->GetLink();
 preCur->SetLink(cur->GetLink());
 nextCur->SetPrior(cur->GetPrior());
 //move direction:default lift to right
  if(direction==0)
  {
   cur=nextCur();
  }
  else
  {
   cur=PreCur();
  }
 }
//-------------------------------------------------------------------------
template<class T>
bool DouList<T>::AddHead(T value)
{//create a DouListNode with value, then insert into DouList at the behind of pointer head.
 DouListNode<T>* add=new DouListNode<T>(value);
 add->SetLink(head->GetLink());
 head->GetLink()->SetPrior(add);
 head->SetLink(add);
 add->SetPrior(head);blog

 //if tail equal to head, reset pointer tail.
 if(tail==head)
 {
  tail=head->GetLink();
 }
 if(add!=NULL)
 {
  return true;
 }
 else
 {
  return false;
 }
}
//-------------------------------------------------------------------------
template<class T>
bool DouList<T>::AddTail(T value)
{//add to the end of DouList
 DouListNode<T>* add=new DouListNode<T>(value);
 Add->SetPrior(tail);
 tail->SetLink(add);
 tail=tail->GetLink();
 add->SetLink(head);
 head->SetPrior(add);
 if(tail!=NULL)//judge the value of pointer tail: NULL or something else
 {
  return true;
 }
 else
 {
  return false;
 }
}
//-------------------------------------------------------------------------
template<class T>
DouList<T>::DouList(void)
{//create DouList
 head=tail=new DouListNode<T>;
 cur=NULL;
 head->SetLink(head);
 head->SetPrior(tail);
}
//-------------------------------------------------------------------------get

相關文章
相關標籤/搜索