//-------------------------------------------------------------------------------------------------------+ // Copyright (C), 1998-2007, SH Software Co. Ltd. // = FileName : SqStack 模板类 // = Version : ver2.0 // = Author : zjq // = CreateDate : 2002-09-09 // = Description: SqStack 声明 基于动态数组的堆栈 // = Maintainers: // //-------------------------------------------------------------------------------------------------------+ #ifndef _SQSTACK_H_ #define _SQSTACK_H_ #ifndef _STACK_H_ #include "Stack.h" #endif #ifndef _SQTABLE_H_ #include "SqTable.h" #endif template class SqStack: public Stack { public: SqStack(long autoAllocUnit = 100,long depth = -1); virtual ~SqStack(); virtual void Clear(); virtual void GoBack(); virtual void GoAhead(); virtual bool Pop(T&t); virtual bool Push(T const&t); virtual bool GetTopData(T&t); virtual long GetDepth(); virtual T& operator[](long i); protected: SqTable table; }; template SqStack::~SqStack() { } template virtual T& SqStack::operator[](long i) { return table[i]; } template SqStack::SqStack(long autoAllocUnit,long depth) :Stack(depth),table(autoAllocUnit,autoAllocUnit) { } template bool SqStack::Push(T const&t) { if(this->IsDepthLimited() && GetDepth() >= this->maxDepth) { return false; } table[table.GetCur()+1] = t; this->oldDepth = GetDepth(); return true; } template bool SqStack::Pop(T&t) { if(this->IsEmpty()) { return false; } t = table.GetCurData(); table.MoveToPre(); return true; } template bool SqStack::GetTopData(T&t) { if(this->IsEmpty()) { return false; } t = table.GetCurData(); return true; } template void SqStack::Clear() { table.Clear(); this->oldDepth = 0; } template long SqStack::GetDepth() { return table.GetCur()+1; } template void SqStack::GoBack() { if(this->CanGoBack()) { table.MoveToPre(); } } template void SqStack::GoAhead() { if(this->CanGoAhead()) { table.MoveToNext(); } } #endif