Guest User

Untitled

a guest
Jan 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. #ifndef TEST_STACK_H
  2. #define TEST_STACK_H
  3.  
  4. #include <stdexcept>
  5.  
  6. template <class T>
  7. class stack {
  8.  
  9. struct node {
  10. T data;
  11. node* previous;
  12.  
  13. node(T data, node *previous) : data(data), previous(previous) {}
  14. };
  15.  
  16. node* head = nullptr;
  17.  
  18. int size = 0;
  19. int max = -1; // -1 so isFull() == false when default constructor used
  20.  
  21. public:
  22. stack() = default;
  23.  
  24. stack(int max) {
  25. if (max <= 0) throw std::out_of_range("stack size must be > 0");
  26. this->max = max;
  27. }
  28.  
  29. // copy constructor
  30.  
  31. stack(stack const& rhs) :
  32. head(copyList(rhs.head)),
  33. size(rhs.size),
  34. max(rhs.size) {}
  35.  
  36. // assignment operator
  37.  
  38. stack& operator = (stack const& rhs)
  39. {
  40. stack tmp(rhs);
  41. swap(tmp);
  42.  
  43. return *this;
  44. }
  45.  
  46. ~stack() {
  47. node* n = head;
  48.  
  49. while (n != nullptr) {
  50. node* previous = n->previous;
  51. delete n;
  52.  
  53. n = previous;
  54. }
  55. }
  56.  
  57. void push(const T &object) {
  58. if (isFull()) throw std::overflow_error("cannot push to a full stack");
  59.  
  60. head = new node(object, head);
  61. ++size;
  62. }
  63.  
  64. const void pop() {
  65. if (head == nullptr) throw std::underflow_error("cannot get item from empty stack");
  66.  
  67. node* old = head;
  68. head = head->previous;
  69.  
  70. --size;
  71. delete old;
  72. }
  73.  
  74. T peek() {
  75. if (head == nullptr) throw std::underflow_error("cannot get item from empty stack");
  76. return head->data;
  77. }
  78.  
  79. int getSize() {
  80. return size;
  81. }
  82.  
  83. bool isFull() {
  84. return size == max;
  85. }
  86.  
  87. bool isEmpty() {
  88. return head == nullptr;
  89. }
  90.  
  91. void swap(stack& other) noexcept
  92. {
  93. using std::swap;
  94. swap(head, other.head);
  95. swap(size, other.size);
  96. swap(max, other.max);
  97. }
  98.  
  99. private:
  100. node* copyList(node* l)
  101. {
  102. if (l == nullptr) {
  103. return nullptr;
  104. }
  105. return new node{l->data, copyList(l->previous)};
  106. }
  107. };
  108.  
  109. #endif
  110.  
  111. #include <stdexcept>
  112.  
  113. stack(int max) {
  114.  
  115. void foo(stack<int> s);
  116. void bar() {
  117. foo(42); // compiles and calls foo with stack<int>(42)
  118. }
  119.  
  120. int max = -1; // -1 so isFull() == false when default constructor used
  121.  
  122. if (max <= 0) throw std::out_of_range("stack size must be > 0");
  123.  
  124. this->max = max;
  125.  
  126. max_ = max;
  127.  
  128. stack(stack const& rhs) :
  129. head(copyList(rhs.head)),
  130. size(rhs.size),
  131. max(rhs.size) {}
  132.  
  133. stack& operator = (stack const& rhs)
  134. {
  135. stack tmp(rhs);
  136. swap(tmp);
  137.  
  138. return *this;
  139. }
  140.  
  141. stack& operator=(stack const& rhs)
  142. {
  143. stack(rhs).swap(*this);
  144. return *this;
  145. }
  146.  
  147. const void pop() {
  148.  
  149. T peek() {
  150.  
  151. T peek() const {
  152. if (head == nullptr) throw std::underflow_error("cannot get item from empty stack");
  153. return std::as_const(head->data);
  154. }
  155.  
  156. void swap(stack& other) noexcept
  157.  
  158. friend void swap(stack& a, stack& b) noexcept {
  159. a.swap(b);
  160. }
  161.  
  162. using std::swap;
  163. swap(redstack, bluestack);
  164.  
  165. node* copyList(node* l)
  166.  
  167. static node *copyList(const node *l)
Add Comment
Please, Sign In to add comment