131 lines
2.1 KiB
C++
131 lines
2.1 KiB
C++
//-------------------------------------------------------------------------------------------------------+
|
|
// Copyright (C), 1998-2007, Beijing Tangent Software Co. Ltd.
|
|
// = FileName : TDListStack 模板类
|
|
// = Version : ver2.0
|
|
// = Author : wlw
|
|
// = CreateDate : 2002-09-09
|
|
// = Description: TDListStack 声明 基于双向链表的堆栈
|
|
// = Maintainers:
|
|
//
|
|
//-------------------------------------------------------------------------------------------------------+
|
|
#ifndef _TDLISTSTACK_H_
|
|
#define _TDLISTSTACK_H_
|
|
|
|
#ifndef _TSTACK_H_
|
|
#include "TStack.h"
|
|
#endif
|
|
|
|
#ifndef _TDUALPTRLIST_H_
|
|
#include "TDualPtrList.h"
|
|
#endif
|
|
|
|
template<class T> class TDListStack : public TStack< T >
|
|
{
|
|
public:
|
|
TDListStack(long depth = -1);
|
|
|
|
virtual ~TDListStack();
|
|
|
|
virtual bool Pop(T&t);
|
|
virtual bool Push(T const&t);
|
|
virtual void Clear();
|
|
|
|
virtual bool GetTopData(T&t);
|
|
virtual long GetDepth();
|
|
virtual T& operator[](long i);
|
|
|
|
virtual void GoBack();
|
|
virtual void GoAhead();
|
|
|
|
protected:
|
|
TDualPtrList<T> list;
|
|
};
|
|
|
|
|
|
template<class T>
|
|
TDListStack<T>::TDListStack(long depth) : TStack<T>(depth)
|
|
{
|
|
}
|
|
|
|
template<class T>
|
|
TDListStack<T>::~TDListStack()
|
|
{
|
|
}
|
|
|
|
template<class T>
|
|
bool TDListStack<T>::Push(T const&t)
|
|
{
|
|
if(this->IsDepthLimited() && this->GetDepth() >= this->maxDepth)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
list[list.GetCur()+1] = t;
|
|
this->oldDepth = this->GetDepth();
|
|
return true;
|
|
}
|
|
|
|
template<class T>
|
|
bool TDListStack<T>::Pop(T&t)
|
|
{
|
|
if(this->IsEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
t = list.GetCurData();
|
|
list.DelCur();
|
|
this->oldDepth = this->GetDepth();
|
|
return true;
|
|
}
|
|
|
|
template<class T>
|
|
bool TDListStack<T>::GetTopData(T&t)
|
|
{
|
|
if(this->IsEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
t = list.GetCurData();
|
|
return true;
|
|
}
|
|
|
|
template<class T>
|
|
void TDListStack<T>::Clear()
|
|
{
|
|
list.Clear();
|
|
this->oldDepth = 0;
|
|
}
|
|
|
|
template<class T>
|
|
long TDListStack<T>::GetDepth()
|
|
{
|
|
return list.GetCur() + 1;
|
|
}
|
|
|
|
template<class T>
|
|
void TDListStack<T>::GoBack()
|
|
{
|
|
if(this->CanGoBack())
|
|
{
|
|
list.MoveToPre();
|
|
}
|
|
}
|
|
|
|
template<class T>
|
|
void TDListStack<T>::GoAhead()
|
|
{
|
|
if(this->CanGoAhead())
|
|
{
|
|
list.MoveToNext();
|
|
}
|
|
}
|
|
|
|
template<class T>
|
|
T& TDListStack<T>::operator[](long i)
|
|
{
|
|
return list[i];
|
|
}
|
|
|
|
#endif
|