//-------------------------------------------------------------------------------------------------------+ // 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 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 list; }; template TDListStack::TDListStack(long depth) : TStack(depth) { } template TDListStack::~TDListStack() { } template bool TDListStack::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 bool TDListStack::Pop(T&t) { if(this->IsEmpty()) { return false; } t = list.GetCurData(); list.DelCur(); this->oldDepth = this->GetDepth(); return true; } template bool TDListStack::GetTopData(T&t) { if(this->IsEmpty()) { return false; } t = list.GetCurData(); return true; } template void TDListStack::Clear() { list.Clear(); this->oldDepth = 0; } template long TDListStack::GetDepth() { return list.GetCur() + 1; } template void TDListStack::GoBack() { if(this->CanGoBack()) { list.MoveToPre(); } } template void TDListStack::GoAhead() { if(this->CanGoAhead()) { list.MoveToNext(); } } template T& TDListStack::operator[](long i) { return list[i]; } #endif