143 lines
2.4 KiB
C++
143 lines
2.4 KiB
C++
//-------------------------------------------------------------------------------------------------------+
|
|
// Copyright (C), 1998-2007, Beijing Tangent Software Co. Ltd.
|
|
// = FileName : TPDListStack 模板类
|
|
// = Version : ver2.0
|
|
// = Author : wlw
|
|
// = CreateDate : 2002-09-09
|
|
// = Description: TPDListStack 声明 基于双向指针链表的堆栈,内存外部分配,外部释放,也可自动清理
|
|
// = Maintainers:
|
|
//
|
|
//-------------------------------------------------------------------------------------------------------+
|
|
#ifndef _TPDLISTSTACK_H_
|
|
#define _TPDLISTSTACK_H_
|
|
|
|
#ifndef _TSTACK_H_
|
|
#include "TStack.h"
|
|
#endif
|
|
|
|
#ifndef _TPDLIST_H_
|
|
#include "TPDList.h"
|
|
#endif
|
|
|
|
template<class T> class TPDListStack: public TStack<T*>
|
|
{
|
|
public:
|
|
TPDListStack(long depth=-1);
|
|
virtual ~TPDListStack();
|
|
|
|
virtual bool Pop(T*& t);
|
|
virtual bool Push(T* const& t);
|
|
virtual void Clear();
|
|
|
|
bool GetTopData(T& t);
|
|
virtual bool GetTopData(T*& t);
|
|
virtual long GetDepth();
|
|
virtual T*& operator[](long i);
|
|
|
|
virtual void GoBack();
|
|
virtual void GoAhead();
|
|
|
|
protected:
|
|
TPDList<T> list;
|
|
|
|
};
|
|
|
|
template<class T>
|
|
TPDListStack<T>::~TPDListStack()
|
|
{
|
|
|
|
}
|
|
template<class T>
|
|
T*& TPDListStack<T>::operator[](long i)
|
|
{
|
|
return list[i];
|
|
}
|
|
|
|
template<class T>
|
|
TPDListStack<T>::TPDListStack(long depth):TStack<T*>(depth)
|
|
{
|
|
|
|
}
|
|
|
|
template<class T>
|
|
bool TPDListStack<T>::Push(T* const& t)
|
|
{
|
|
if(this->IsDepthLimited() && this->GetDepth() >= this->maxDepth)
|
|
{
|
|
return false;
|
|
}
|
|
list[list.GetCur()+1] = t;
|
|
this->oldDepth = GetDepth();
|
|
return true;
|
|
}
|
|
|
|
template<class T>
|
|
bool TPDListStack<T>::Pop(T*& t)
|
|
{
|
|
if(this->IsEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
t = list.GetCurData();
|
|
list.SetCurData(NULL);
|
|
list.DelCur();
|
|
this->oldDepth = GetDepth();
|
|
return true;
|
|
}
|
|
|
|
template<class T>
|
|
bool TPDListStack<T>::GetTopData(T*& t)
|
|
{
|
|
if(this->IsEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
t = list.GetCurData();
|
|
return true;
|
|
}
|
|
|
|
template<class T>
|
|
bool TPDListStack<T>::GetTopData(T& t)
|
|
{
|
|
if(this->IsEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
t = *(list.GetCurData());
|
|
return true;
|
|
}
|
|
|
|
template<class T>
|
|
void TPDListStack<T>::Clear()
|
|
{
|
|
list.Clear();
|
|
this->oldDepth = 0;
|
|
}
|
|
|
|
template<class T>
|
|
long TPDListStack<T>::GetDepth()
|
|
{
|
|
return list.GetCur()+1;
|
|
}
|
|
|
|
template<class T>
|
|
void TPDListStack<T>::GoBack()
|
|
{
|
|
if(this->CanGoBack())
|
|
{
|
|
list.MoveToPre();
|
|
}
|
|
}
|
|
|
|
template<class T>
|
|
void TPDListStack<T>::GoAhead()
|
|
{
|
|
if(this->CanGoAhead())
|
|
{
|
|
list.MoveToNext();
|
|
}
|
|
}
|
|
|
|
|
|
#endif
|