#ifndef STL_LINK_DOUBLELIST_H
#define STL_LINK_DOUBLELIST_H
/************************************************************************/
/* 以下为C++实现双链表 (STL模板)
/************************************************************************/
/************************************************************************/
/* 双链表--节点定义
/************************************************************************/
template <class Elemplent>
class DoubleNode
{
protected:
Elemplent data;
DoubleNode<Elemplent> *next;
DoubleNode<Elemplent> *prior;
public:
DoubleNode();
DoubleNode(Elemplent tempData,DoubleNode<Elemplent> *tempNext,DoubleNode<Elemplent> *tempPrior);
};
//节点--构造函数
template<class Elemplent>
DoubleNode<Elemplent>::DoubleNode()
{
next = NULL;
prior =NULL;
}
//节点--创建一个节点构造函数
template<class Elemplent>
DoubleNode<Elemplent>::DoubleNode(Elemplent tempData,
DoubleNode<Elemplent> *tempNext,DoubleNode<Elemplent> *tempPrior)
{
data = tempData;
next = tempNext;
prior = tempPrior;
}
/************************************************************************/
/* 双链表
/************************************************************************/
template<class Elemplent>
class DoubleList
{
public:
DoubleNode<Elemplent> *head;
mutable DoubleNode<Elemplent> *CursorNodePtr;
mutable int CursorNodePosition;
int CountTotal;
public:
DoubleList();
DoubleList(const DoubleList<Elemplent>& tempDoublelist);
DoubleList<Elemplent>& operator = (const DoubleList<Elemplent> &tempDoublelist);
~DoubleList();
void InitDoubleList();
DoubleNode<Elemplent>* GetPTRWithPostion(int position = 0);
public:
int GetLength();
bool IsEmpty();
bool Clear();
bool SetElemplentWithPosition(const Elemplent &tempElemplent,int position = 0);
bool GetElemplentWihtPosition(Elemplent & tempElemplent,int position = 0)const;
bool Insert(const Elemplent &tempElemplent,int position = 0);
bool Delete(int position = 0);
};
//双链表构造函数
template<class Elemplent>
DoubleList<Elemplent>::DoubleList()
{
InitDoubleList();
}
//双链表析构函数
template <class Elemplent>
DoubleList<Elemplent>::~DoubleList()
{
Clear();
delete head;
}
//双链表初始化函数
template<class Elemplent>
void DoubleList<Elemplent>::InitDoubleList()
{
head = new DoubleNode();
head->next = head;
head->prior =head;
CursorNodePosition =0;
CursorNodePtr =NULL;
CountTotal = 0;
}
//双链表--返回节点位置的指针--方法有些笨,但是易看易懂
template <class Elemplent>
DoubleNode<Elemplent>* DoubleList<Elemplent>::GetPTRWithPostion(int position /* = 0 */)
{
if (position>CursorNodePosition)
{
while(position>CursorNodePosition || CursorNodePtr != head)
{
CursorNodePosition--;
CursorNodePtr = CursorNodePtr->prior;
}
if (position == CursorNodePosition)
{
return CursorNodePtr;
}
else
{
return NULL;
}
}
else if (position<CursorNodePosition)
{
while(position<CursorNodePosition || CursorNodePtr !=NULL)
{
CursorNodePosition++;
CursorNodePtr = CursorNodePtr->next;
}
if (position == CursorNodePosition)
{
return CursorNodePtr;
}
else
{
return NULL;
}
}
}
//双链表--得到长度
template<class Elemplent>
int DoubleList<Elemplent>::GetLength()
{
return CountTotal;
}
template<class Elemplent>
bool DoubleList<Elemplent>::IsEmpty()
{
return head->next == head;
}
//双链表--清空表
template<class Elemplent>
bool DoubleList<Elemplent>::Clear()
{
while(GetLength()>0)
{
delete(1);
}
return IsEmpty() == true;
}
//双链表--用位置得到该节点的元素值
template <class Elemplent>
bool DoubleList<Elemplent>::GetElemplentWihtPosition(Elemplent & tempElemplent,int position /* = 0 */)const
{
DoubleNode<Elemplent> *tempPtr;
if ((tempPtr=GetPTRWithPostion(position) ==NULL))
{
return false;
}
tempElemplent = tempPtr->data;
return true;
}
//双链表--用位置设置该节点的元素值
template <class Elemplent>
bool DoubleList<Elemplent>::SetElemplentWithPosition(const Elemplent &tempElemplent,int position /* = 0 */)
{
DoubleNode<Elemplent>* tempPtr;
if ((tempPtr=GetPTRWithPostion(position)==NULL))
{
return false;
}
tempPtr->data = tempElemplent;
return true;
}
//双链表--插入一个元素
template <class Elemplent>
bool DoubleList<Elemplent>::Insert(const Elemplent &tempElemplent,int position /* = 0 */)
{
DoubleNode<Elemplent> *tempPtr;
if ((tempPtr = GetPTRWithPostion(position-1)==NULL))
{
return false;
}
DoubleNode<Elemplent> *newPtr =new DoubleNode<Elemplent>(tempElemplent,tempPtr->next);
newPtr->next->prior=tempPtr;
tempPtr->next=newPtr;
newPtr->prior =tempPtr;
CursorNodePtr = newPtr;
CursorNodePosition = position;
CountTotal++;
return true;
}
//双链表--删除一个元素
template<class Elemplent>
bool DoubleList<Elemplent>::Delete(int position)
{
DoubleNode<Elemplent> *tempPtr;
if ((tempPtr = GetPTRWithPostion(position-1) == NULL))
{
return false;
}
tempPtr = tempPtr->next;
tempPtr->prior->next = tempPtr->next;
tempPtr->next->prior = tempPtr->prior;
if (position == CountTotal)
{
CursorNodePosition = 0;
CursorNodePtr = head;
}
else
{
CursorNodePtr = tempPtr->next;
CursorNodePosition = position ;
}
delete tempPtr;
CountTotal --;
return true;
}
//双链表--复制构造函数
template <class Elemplent>
DoubleList<Elemplent>::DoubleList(const DoubleList<Elemplent>& tempDoublelist)
{
Elemplent tempElemplent;
int length = tempDoublelist.GetLength();
InitDoubleList();
for (int position = 1;position <=length;position++)
{
tempDoublelist.GetElemplentWihtPosition(tempElemplent,position);
Insert(tempElemplent,position);
}
return *this;
}
//双链表--赋值操作
template<class Elemplent>
DoubleList<Elemplent>& DoubleList<Elemplent>::operator=(const DoubleList<Elemplent>& tempDoublelist)
{
if (this == tempDoublelist)
{
return *this;
}
Clear();
Elemplent tempElemplent;
int length = tempDoublelist.GetLength();
for (int position =1;position<=length;position++)
{
tempDoublelist.GetElemplentWihtPosition(tempElemplent,position);
Insert(tempElemplent,position);
}
return *this;
}
#endif