Advertisement
Phr0zen_Penguin

StackClass.h - Generic (LIFO) Stack class.

Mar 4th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1.  
  2. /**
  3.  * [StackClass.h]
  4.  *
  5.  * Generic (LIFO) Stack class.
  6.  *
  7.  * This Stack has been implemented with templates to allow it to accommodate virtually any data type,
  8.  * and the size of the Stack is determined dynamically at runtime.
  9.  *
  10.  * There is also a new function: peek(), which, given a whole number 'Depth', returns the Stack
  11.  * element which is 'Depth' levels from the top.
  12.  */
  13.  
  14. #ifndef __StackClassH__     // Code include guard...
  15. #define __StackClassH__     // ...
  16.  
  17. #include <assert.h>         // For error-checking purposes.
  18.  
  19. /**
  20.  * Main Structure of Stack Class:
  21.  */
  22. template <class Elem>
  23. class Stack
  24. {
  25.     public:
  26.         Stack(int MaxSize=500);
  27.         Stack(const Stack<Elem> &OtherStack);
  28.         ~Stack(void);
  29.  
  30.         inline void Push(const Elem &Item);                 // Adds Item to the top.
  31.         inline Elem Pop(void);                              // Returns Item from the top.
  32.         inline const Elem &Peek(int Depth) const;           // Peek a depth downwards.
  33.  
  34.     protected:
  35.         Elem        *Data;                                  // The actual Data array.
  36.         int         CurrElemNum;                            // The current number of elements.
  37.         const   int MAX_NUM;                                // Maximum number of elements.
  38. };
  39.  
  40.  
  41. /**
  42.  * Implementation of Stack Class:
  43.  */
  44.  
  45. // Stack Constructor Function:
  46. template <class Elem>
  47. Stack<Elem>::Stack(int MaxSize) :
  48.     MAX_NUM(MaxSize)                                        // Initialize the constant.
  49. {
  50.     Data = new Elem[MAX_NUM];                               // Create Stack array.
  51.     CurrElemNum = 0;
  52. }
  53.  
  54. // Stack Destructor Function:
  55. template <class Elem>
  56. Stack<Elem>::~Stack(void)
  57. {
  58.     delete[] Data;                                          // Destroy Stack array.
  59. }
  60.  
  61. // Push() Function:
  62. template <class Elem>
  63. inline void Stack<Elem>::Push(const Elem &Item)
  64. {
  65.     // Error Check: Make sure we are not exceeding the maximum storage space:
  66.     assert(CurrElemNum < MAX_NUM);
  67.  
  68.     Data[CurrElemNum++] = Item;
  69. }
  70.  
  71. // Pop() Function:
  72. template <class Elem>
  73. inline Elem Stack<Elem>::Pop(void)
  74. {
  75.     // Error Check: Make sure we are not popping from an empty Stack:
  76.     assert(CurrElemNum > 0);
  77.  
  78.     return Data[--CurrElemNum];
  79. }
  80.  
  81. // Peek() Function:
  82. template <class Elem>
  83. inline const Elem &Stack<Elem>::Peek(int Depth) const
  84. {
  85.     // Error Check: Make sure the depth does not exceed the number of elements:
  86.     assert(Depth < CurrElemNum);
  87.  
  88.     return Data[CurrElemNum - (Depth + 1)];
  89. }
  90. #endif // __StackClassH__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement