双向循环链表类的C++实现

至于DouListNode的实现请见:http://my.oschina.net/u/181847/blog/42959

#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();

};
//-------------------------------------------------------------------------
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();
   }
  }

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);

//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);
}
//-------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值