198 lines
3.5 KiB
C++
198 lines
3.5 KiB
C++
//-----------------------------------------------------------------------------+
|
|
// Copyright (C), 1998-2007, Beijing Tangent Software Co. Ltd.
|
|
// = FileName : TPGeGDList.h
|
|
// = Version : ver2.0
|
|
// = Author : xlc
|
|
// = CreateDate : 2002-09-09
|
|
// = Description: 类的声明 双向递归链
|
|
|
|
// = Maintainers:
|
|
//
|
|
//-----------------------------------------------------------------------------+
|
|
#ifndef _TPGEGDLIST_H_
|
|
#define _TPGEGDLIST_H_
|
|
|
|
#ifndef _TPGEGLIST_H_
|
|
#include "TPGeGList.h"
|
|
#endif
|
|
|
|
template<class T> class TPGeGDList : public TPGeGList<T>
|
|
{
|
|
public:
|
|
TPGeGDList();
|
|
|
|
//pList!=NULL!!!
|
|
virtual void CNNext(TPGeGDList*pList);
|
|
|
|
virtual void DCNNext();
|
|
|
|
//释放所有前面结点,本结点成为起始结点
|
|
void ReleaseAllPre();
|
|
|
|
TPGeGDList<T>*Next() const;
|
|
|
|
TPGeGDList<T>*Pre() const;
|
|
|
|
TPGeGDList<T>* GetTail() const;
|
|
|
|
//如果是环路GetTail与GetHead结果相同
|
|
TPGeGDList<T>* GetHead() const;
|
|
|
|
TPGeGDList<T>* GetAt(long i) const;
|
|
|
|
const T& GetData(long i) const;
|
|
|
|
void DCNPre();
|
|
|
|
void SetPre(TPGeGDList<T>* pList) const;
|
|
|
|
//安全转接,自动断开所有原相关链接,pList!=NULL!!!
|
|
void CNPre(TPGeGDList*pList);
|
|
|
|
TPGeGDList<T>* FetchNext(long num = 1);
|
|
|
|
//返回新链首,如果本结点非链首,将在本结点处形成分叉!如果是环路将保持环路
|
|
TPGeGDList<T>* Reverse();
|
|
|
|
protected:
|
|
|
|
TPGeGDList* pre;
|
|
};
|
|
|
|
template<class T>
|
|
TPGeGDList<T>::TPGeGDList() : pre(NULL)
|
|
{
|
|
|
|
}
|
|
|
|
template<class T>
|
|
TPGeGDList<T>* TPGeGDList<T>::Next() const
|
|
{
|
|
return (TPGeGDList<T>*)this->next;
|
|
}
|
|
|
|
template<class T>
|
|
TPGeGDList<T>* TPGeGDList<T>::Pre() const
|
|
{
|
|
return pre;
|
|
}
|
|
|
|
template<class T>
|
|
TPGeGDList<T>* TPGeGDList<T>::GetTail() const
|
|
{
|
|
return (TPGeGDList<T>*)(TPGeGList<T>::GetTail());
|
|
}
|
|
|
|
template<class T>
|
|
const T& TPGeGDList<T>::GetData(long i) const
|
|
{
|
|
return GetAt(i)->GetData();
|
|
}
|
|
|
|
template<class T>
|
|
void TPGeGDList<T>::SetPre(TPGeGDList<T>* pList) const
|
|
{//一般不使用!
|
|
pre = pList;
|
|
}
|
|
|
|
template<class T>
|
|
void TPGeGDList<T>::DCNNext()
|
|
{
|
|
if (this->next != NULL)
|
|
{
|
|
this->next->SetPre(NULL);
|
|
this->next = NULL;
|
|
}
|
|
}
|
|
|
|
template<class T>
|
|
void TPGeGDList<T>::DCNPre()
|
|
{
|
|
if(pre != NULL)
|
|
{
|
|
pre->SetNext(NULL);
|
|
pre = NULL;
|
|
}
|
|
}
|
|
|
|
template<class T>
|
|
TPGeGDList<T>* TPGeGDList<T>::FetchNext(long num/* = 1*/)
|
|
{
|
|
return (TPGeGDList<T>*)(TPGeGList<T>::FetchNext(num));
|
|
}
|
|
|
|
template<class T>
|
|
void TPGeGDList<T>::CNNext(TPGeGDList*pList)
|
|
{
|
|
DCNNext();
|
|
pList->DCNPre();
|
|
this->SetNext(pList);
|
|
pList->SetPre(this);
|
|
}
|
|
|
|
template<class T>
|
|
void TPGeGDList<T>::CNPre(TPGeGDList*pList)
|
|
{
|
|
DCNPre();
|
|
pList->DCNNext();
|
|
SetPre(pList);
|
|
pList->SetNext(this);
|
|
}
|
|
|
|
template<class T>
|
|
TPGeGDList<T>* TPGeGDList<T>::GetHead() const
|
|
{
|
|
for(const TPGeGDList<T>*pCur = this; pCur->Pre() != NULL && pCur->Pre() != this; pCur = pCur->Pre());
|
|
return (TPGeGDList<T>*)this->pCur;
|
|
}
|
|
|
|
template<class T>
|
|
TPGeGDList<T>* TPGeGDList<T>::GetAt(long i) const
|
|
{
|
|
if(i >= 0)
|
|
{
|
|
return (TPGeGDList<T>*)(TPGeGList<T>::GetAt(i));
|
|
}
|
|
else
|
|
{
|
|
long count = 1;
|
|
for(TPGeGDList<T>*pCur = Pre(); count< -i && pCur != NULL; count++, pCur = pCur->Pre());
|
|
return this->pCur;
|
|
}
|
|
}
|
|
|
|
template<class T>
|
|
void TPGeGDList<T>::ReleaseAllPre()
|
|
{
|
|
TPGeGDList<T>* pHead = GetHead();
|
|
DCNPre();
|
|
delete pHead;
|
|
}
|
|
|
|
template<class T>
|
|
TPGeGDList<T>* TPGeGDList<T>::Reverse()
|
|
{
|
|
TPGeGDList<T> *pPre = this,*pCur = Next();
|
|
while(pCur != NULL && pCur != this)
|
|
{
|
|
TPGeGDList<T>* temp = pCur->Next();
|
|
pCur->SetNext(pPre);
|
|
pPre->SetPre(pCur);
|
|
pPre = pCur;
|
|
pCur = temp;
|
|
}
|
|
if(pCur == NULL)
|
|
{
|
|
this->SetNext(NULL);
|
|
pPre->SetPre(NULL);
|
|
return pPre;
|
|
}
|
|
else
|
|
{
|
|
this->SetNext(pPre);
|
|
pPre->SetPre(this);
|
|
return this;
|
|
}
|
|
}
|
|
|
|
#endif |