///////////////////Software Control Header/////////////////////////////////// // TStack.h - Library to support stack operation // // $LogThisFile$ \Projects\UtilCode\Include\TStack.h // // V01.00 26/10/2000 2213 // Initial revision. // // This code is freely distributed to help the development of complex // software in any field using C++. The author is not held responsible // for any damage caused by the usage of the code. // The author should be acknowledged for the use of this piece of code in // commercial software. // // Bug reports and suggestions are welcomed to report to // // tzzstan@hotmail.com ///////////////////////////////////////////////////////////////////////////// #ifndef __TS_STACK__ #define __TS_STACK__ #include "TSingLst.h" ///////////////////////////////////////////////////////////////////////////// // CStack -- class declaration // // Description: Use CSinglyList to implement stack operation. // // TSTan 12 Aug 2000 ///////////////////////////////////////////////////////////////////////////// template class CStack { int m_StackPtr, m_MaxElement; T m_ElementCopy; CSinglyList m_Stack; public: CStack(CStack &); // Copy constructor CStack(int MaxElement = -1); ~CStack(); /////////////////// // Modifiers /////////////////// bool Push(T&); T &Pop(); void Reset(); CStack &operator=(CStack &); // Assignment operator /////////////////// // Data retrieval /////////////////// int Size() const {return m_StackPtr;} }; ///////////////////////////////////////////////////////////////////////////// // CStack(CStack &original) // // Desctiption: Constructor to create copy of original. // // TSTan 12 Aug 2000 ///////////////////////////////////////////////////////////////////////////// template CStack::CStack(CStack &original) { m_StackPtr = original.m_StackPtr; m_MaxElement = original.m_MaxElement; m_Stack = original.m_Stack; } ///////////////////////////////////////////////////////////////////////////// // operator=(CStack &original) // // Desctiption: Create copy of original before assign to this. // // TSTan 12 Aug 2000 ///////////////////////////////////////////////////////////////////////////// template CStack &CStack::operator=(CStack &original) { m_StackPtr = original.m_StackPtr; m_MaxElement = original.m_MaxElement; m_Stack = original.m_Stack; return *this; } ///////////////////////////////////////////////////////////////////////////// // CStack(int MaxElement) // // Desctiption: Constructor to create stack frame. // // MaxElement - Stack size, -1 = no limit. // // TSTan 12 Aug 2000 ///////////////////////////////////////////////////////////////////////////// template CStack::CStack(int MaxElement) { m_MaxElement = MaxElement; m_StackPtr = 0; } ///////////////////////////////////////////////////////////////////////////// // Reset() // // Desctiption: Reset to empty stack. // // TSTan 12 Aug 2000 ///////////////////////////////////////////////////////////////////////////// template void CStack::Reset() { m_Stack.RemoveAll(); m_StackPtr = 0; } ///////////////////////////////////////////////////////////////////////////// // ~CStack() // // Desctiption: Destructor to clean up memory. // // TSTan 12 Aug 2000 ///////////////////////////////////////////////////////////////////////////// template CStack::~CStack() { } ///////////////////////////////////////////////////////////////////////////// // Push(T &element) // // Desctiption: Push element on top of stack. // // TSTan 12 Aug 2000 ///////////////////////////////////////////////////////////////////////////// template bool CStack::Push(T &element) { if(m_MaxElement == -1 || m_StackPtr < m_MaxElement) { m_Stack.Insert(element); m_StackPtr++; return true; } return false; } ///////////////////////////////////////////////////////////////////////////// // Pop() // // Desctiption: Remove topmost element. // // TSTan 12 Aug 2000 ///////////////////////////////////////////////////////////////////////////// template T &CStack::Pop() { if(m_StackPtr) { m_Stack.ResetPtr(); m_ElementCopy = *m_Stack.Current(); m_Stack.Remove(); m_StackPtr--; } return m_ElementCopy; } #endif