Advertisement
jeanleopoldo

Untitled

Mar 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #ifndef STRUCTURES_ARRAY_STACK_H
  2. #define STRUCTURES_ARRAY_STACK_H
  3.  
  4. #include <cstdint>  //  std::size_t
  5. #include <stdexcept>  //  C++ exceptions
  6.  
  7. namespace structures {
  8.  
  9. template<typename T>
  10. /*
  11. *  class stack
  12. */
  13. class ArrayStack {
  14.  private:
  15. /*
  16. *  atributes of class stack
  17. */
  18.     T* contents;
  19.     int top_;
  20.     std::size_t max_size_;
  21.     const static auto DEFAULT_SIZE = 10u;
  22.  public:
  23. /*
  24. * standard constructor
  25. */
  26.     ArrayStack() {
  27.         max_size_ = DEFAULT_SIZE;
  28.         contents = new T[max_size_];
  29.         top_ = -1;
  30.     }
  31. /*
  32. *  constructor with size of array as parameter
  33. */
  34.     explicit ArrayStack(std::size_t max) {
  35.         contents = new T[max];
  36.         max_size_ = max;
  37.         top_ = -1;
  38.     }
  39. /*
  40. *  destructor
  41. */
  42.     ~ArrayStack() {
  43.         delete contents;
  44.     }
  45. /*
  46. * inserts data on top_ of the stack
  47. */
  48.     void push(const T& data) {
  49.         if (full())
  50.             throw 1;
  51.         contents[++top_] = data;
  52.     }
  53. /*
  54. *   remove data from the stack
  55. */
  56.     T pop() {
  57.         if (empty())
  58.             throw 2;
  59.         return contents[top_--];
  60.     }
  61. /*
  62. *  points to the top_ of the stack
  63. */
  64.     T& top() {
  65.         return contents[top_];
  66.     }
  67. /*
  68. *  points to the beggining of the stack
  69. */
  70.     void clear() {
  71.         top_ = -1;
  72.     }
  73. /*
  74. * returns actual size of the stack
  75. */
  76.     std::size_t size() {
  77.         return top_ + 1;
  78.     }
  79. /*
  80. * returns max_size_ of the stack
  81. */
  82.     std::size_t max_size() {
  83.         return max_size_;
  84.     }
  85. /*
  86. *   verifies if stack is empty
  87. */
  88.     bool empty() {
  89.         if (top_ == -1)
  90.             return true;
  91.         return false;
  92.     }
  93. /*
  94. *   verifies if stack is full
  95. */
  96.     bool full() {
  97.         if (top_ == (max_size_-1))
  98.             return true;
  99.         return false;
  100.     }
  101. };
  102.  
  103. }  //  namespace structures
  104.  
  105. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement