vimix

stack.h

May 23rd, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. // Author: venix cador
  2. // File: stack.cpp
  3. // Date: 3/18/2013
  4.  
  5. #ifndef VENIX_STACK_H_
  6. #define VENIX_STACK_H_
  7.  
  8. #include <iostream>
  9. #include <cassert>
  10.  
  11.  
  12. template <typename T>
  13. class node
  14. {
  15. public:
  16.  
  17. //default constructor for node
  18. node(): data(), next(0) {};
  19.  
  20. //Node constructor for element wiht data
  21. node(const T& item): data(item), next(0) {};
  22.  
  23. T data;
  24.  
  25. node<T> *next;
  26. };
  27.  
  28. template <typename T>
  29. class Stack{
  30. public:
  31.  
  32. Stack(): Tos(0) {};
  33.  
  34. ~Stack();
  35.  
  36. Stack(const Stack<T>&);
  37.  
  38. void swap(Stack<T>&);
  39.  
  40. Stack<T>& operator= (Stack<T>);
  41.  
  42. void push(const T&);
  43.  
  44. T pop();
  45.  
  46. T top()const;
  47.  
  48. bool isEmpty(){return Tos == 0;};
  49.  
  50. bool isFull() const;
  51.  
  52. void printAll()const;
  53.  
  54. private:
  55.  
  56. //pointer to top of stack
  57. node<T> *Tos;
  58.  
  59. };
  60.  
  61. //Outputs all the elements of the Stack
  62. // Ex: out << example_stack;
  63.  
  64. template <typename T>
  65. std::ostream& operator << (std::ostream& out, const Stack<T>& rhs)
  66. {
  67. for (Stack<T> temp(rhs); !temp.isEmpty();) {// making a copy of the rhs of the stack and puting it in the ostream and return it back to out because it was call by out
  68. out << temp.pop();
  69. }
  70.  
  71. return out;
  72. }
  73.  
  74. //determine the current stack is full
  75. // Ex: full_stack.isfull()
  76. template<typename T>
  77. bool Stack<T>::isFull() const
  78. {
  79.  
  80. //try to create a new node in memory
  81. node<T> *temp = new(std::nothrow) node<T>();
  82.  
  83. //if we cannot do that, we are out of memory
  84. if ( temp == 0){
  85. return true;
  86. }
  87. delete temp;
  88. return false;
  89. }
  90.  
  91. //destructor
  92. //
  93. template <typename T>
  94. Stack<T>::~Stack()
  95. {
  96. while (Tos != 0) {
  97. node<T> *temp = Tos;
  98. Tos = Tos->next;
  99. delete temp;
  100. }
  101. }
  102. //Prints all items in the stack
  103. //EX: example_stack.printAll();
  104. template<typename T>
  105. void Stack<T>::printAll()const{
  106.  
  107. //make copy of temp for output
  108. Stack<T> temp(*this);
  109.  
  110. //Pop element one at a time for output
  111. while (!temp.isEmpty()){
  112. std::cout << temp.pop() <<' ' << std::endl;
  113. }
  114. std::cout << std::endl;
  115. }
  116. //swap
  117. template<typename T>
  118. void Stack<T>::swap(Stack<T>& rhs){
  119.  
  120. //create temp node and copy tos into it
  121. node<T> *temp = Tos;
  122.  
  123. //set tos to what you pass in
  124. Tos = rhs.Tos;
  125.  
  126. //set what you passed in to temp
  127. rhs.Tos = temp;
  128. }
  129. // the operator equal
  130. template<typename T>
  131. Stack<T>& Stack<T>::operator =(Stack<T>rhs){
  132. swap(rhs);
  133. return *this;
  134. }
  135.  
  136. // stack's copy constructor
  137. //ex: stack<int> new_stack(old_stack);
  138. //
  139. template <typename T>
  140. Stack<T>::Stack(const Stack<T>& actual){
  141. node<T> *bottom, *tempTos, *tempnode;
  142.  
  143. tempTos = actual.Tos;
  144.  
  145. Tos = 0;
  146.  
  147.  
  148. while(tempTos != 0){
  149. tempnode = new node<T>(tempTos->data);
  150.  
  151. if(Tos == 0){
  152. Tos = tempnode;
  153.  
  154. }else{
  155. bottom->next = tempnode;
  156. }
  157. bottom = tempnode;
  158. tempTos = tempTos->next;
  159. }
  160. }
  161.  
  162. //pushes an item to the top of the stack
  163. //ex: example_stack.push(1);
  164. template <typename T>
  165. void Stack<T>::push(const T& item)
  166. {
  167. //if stack is full, do nothing
  168. assert(!isFull());
  169.  
  170. //create temporary node with passed in data
  171. node<T> *temp = new node<T>(item);
  172.  
  173. //take existing data and appen it to temp
  174. temp->next = Tos; //Can be (*temp).next, and it is now NULL.
  175. Tos = temp;
  176. }
  177.  
  178. template <typename T>
  179. T Stack<T>::pop()
  180. {
  181. assert(Tos != 0);
  182. node<T> *temp = Tos;
  183. T result = Tos->data;
  184. Tos = Tos->next;
  185. delete temp; //Deallocating temp.
  186. return result;
  187. } //Now we have a clean stack.
  188.  
  189. #endif
Advertisement
Add Comment
Please, Sign In to add comment