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