• [c++ stack source](https://www.cprogramming.com/tutorial/computersciencetheory/stackcode. ```

    /*

    | | | Stack Class | | =========================================================== | | This Stack has been implemented with templates to allow it | | to accomodate virtually any data type, and the size of the | | Stack is determined dynamically at runtime. | | | | There is also a new function: peek(), which, given a whole | | number ‘Depth’, returns the Stack element which is ‘Depth’ | | levels from the top. |

    | |

    */

ifndef StackClassH

define StackClassH

include // For error-checking purposes

//————————————————————————- // Main structure of Stack Class: //————————————————————————-

template class Stack { public: Stack(int MaxSize=500); Stack(const Stack &OtherStack); ~Stack(void);

  1. inline void Push(const Elem &Item); // Adds Item to the top
  2. inline Elem Pop(void); // Returns Item from the top
  3. inline const Elem &Peek(int Depth) const; // Peek a depth downwards

protected: Elem *Data; // The actual Data array int CurrElemNum; // The current number of elements const int MAX_NUM; // Maximum number of elements };

//————————————————————————- // Implementation of Stack Class: //————————————————————————-

// Stack Constructor function template Stack::Stack(int MaxSize) : MAX_NUM( MaxSize ) // Initialize the constant { Data = new Elem[MAX_NUM]; CurrElemNum = 0; }

// Stack Destructor function template Stack::~Stack(void) { delete[] Data; }

// Push() function template inline void Stack::Push(const Elem &Item) { // Error Check: Make sure we aren’t exceeding the maximum storage space assert(CurrElemNum < MAX_NUM);

Data[CurrElemNum++] = Item; }

// Pop() function template inline Elem Stack::Pop(void) { // Error Check: Make sure we aren’t popping from an empty Stack assert(CurrElemNum > 0);

return Data[—CurrElemNum]; }

// Peek() function template inline const Elem &Stack::Peek(int Depth) const { // Error Check: Make sure the depth doesn’t exceed the number of elements assert(Depth < CurrElemNum);

return Data[ CurrElemNum - (Depth + 1) ]; }

endif /StackClassH/

```