Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #ifndef STACK_H
  2. #define STACK_H
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. #include "Node.h"
  8.  
  9. template <class T>
  10. class Stack {
  11. public:
  12. Stack(); // Constructor
  13. ~Stack(); // Destructor
  14. bool empty() const; // Checks if stack is empty
  15. void push(const T &val); // Pushes data on top of stack
  16. void pop(); // Removes top item from stack
  17. T getTop(); // Accessor for data in top node
  18.  
  19. /*** OVERLOADED OPERATORS ***/
  20. Stack<T> &operator =(const Stack<T> &rhs);
  21. bool operator ==(const Stack<T> &rhs);
  22.  
  23. // Template declaration necessary here because operator
  24. // is technically outside class
  25. template <class U>
  26. friend ostream &operator <<(ostream &out, Stack<U> &aStack);
  27.  
  28. private:
  29. Node<T> *top; // Node at top of stack
  30. };
  31.  
  32. /*** FUNCTION DEFINITIONS ***/
  33.  
  34. // Constructor
  35. template <class T>
  36. Stack<T>::Stack() : top(NULL)
  37. {}
  38.  
  39. // Destructor
  40. // Deletes nodes from stack until it's empty
  41. template <class T>
  42. Stack<T>::~Stack()
  43. {
  44. while (!empty()) {
  45. pop();
  46. }
  47. }
  48.  
  49. // Checks if stack is empty
  50. template <class T>
  51. bool Stack<T>::empty() const {
  52. return (top == NULL);
  53. }
  54.  
  55. // Pushes data on top of stack
  56. template <class T>
  57. void Stack<T>::push(const T &val) {
  58. top = new Node<T>(val, top);
  59. }
  60.  
  61. // Removes top item from stack
  62. template <class T>
  63. void Stack<T>::pop() {
  64. Node<T> *temp;
  65. if (top == NULL)
  66. cout << "Just kidding--stack is empty\n";
  67. else {
  68. temp = top;
  69. top = top->getNext();
  70. delete temp;
  71. }
  72. }
  73.  
  74. template <class T>
  75. T Stack<T>::getTop() {
  76. return top->getVal();
  77. }
  78.  
  79. // Overloaded assignment--performs deep copy
  80. template <class T>
  81. Stack<T> &Stack<T>::operator =(const Stack<T> &rhs) {
  82. Stack<T> temp;
  83. Node<T> *p;
  84.  
  85. if (!(*this == rhs)) {
  86. // Step 0. If there's data in the calling object, empty it
  87. while (!empty())
  88. pop();
  89.  
  90. // Step 1. Copy data from rhs to temp, in reverse order
  91. p = rhs.top;
  92. while (p != NULL) {
  93. temp.push(p->getVal());
  94. p = p->getNext();
  95. }
  96.  
  97. // Step 2. Empty temp stack and copy data to calling object
  98. while (!temp.empty()) {
  99. this->push(temp.getTop());
  100. temp.pop();
  101. }
  102. }
  103. return *this;
  104. }
  105.  
  106. // Overloaded comparison
  107. template <class T>
  108. bool Stack<T>::operator ==(const Stack<T> &rhs) {
  109. Node<T> *tmp1, *tmp2; // Node pointer for each stack
  110.  
  111. tmp1 = top; // Top of calling object
  112. tmp2 = rhs.top; // Top of RHS
  113.  
  114. // Traverse stacks while:
  115. // (1) Haven't hit end of either one (tested in loop condition), and
  116. // (2) Data in all nodes so far match (if statement will exit if false)
  117. while (tmp1 != NULL && tmp2 != NULL) {
  118.  
  119. // If mismatch found, exit function
  120. if (tmp1->getVal() != tmp2->getVal())
  121. return false;
  122.  
  123. // Move both Node pointers ahead to next node
  124. tmp1 = tmp1->getNext();
  125. tmp2 = tmp2->getNext();
  126. }
  127.  
  128. // Stacks are same length & contain same data
  129. // (if they were same length and contained different data,
  130. // if statement in loop would have forced function to
  131. // return false before reaching this point)
  132. if (tmp1 == NULL && tmp2 == NULL)
  133. return true;
  134.  
  135. // Else case: one stack is shorter than the other
  136. else
  137. return false;
  138. }
  139.  
  140. // Overloaded output (friend function)
  141. template <class T>
  142. ostream &operator <<(ostream &out, Stack<T> &aStack) {
  143. Node<T> *temp = aStack.top;
  144. while (temp != NULL) {
  145. cout << temp->getVal() << '\n';
  146. temp = temp->getNext();
  147. }
  148. return out;
  149. }
  150. /*** END FUNCTION DEFINITIONS ***/
  151.  
  152.  
  153. #endif //STACK_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement