Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. /* Header file for abstract data type "STACK" implemented using a linked list */
  2.  
  3. #ifndef STACK_H
  4. #define STACK_H
  5.  
  6. template <class T>
  7. class stack {
  8.  
  9. public:
  10. void push(T); //Function that inserts elements into the stack
  11. bool empty(); //Function to test whether the stack is empty
  12. T top(); //Returns top element of stack
  13. void pop(); //Removes element at the top of the stack
  14. int size(); //Returns size of stack
  15. void print(); //Prints stack contents
  16.  
  17. struct node { //Definition of node structure with constructor and destructor
  18.  
  19. T node_data;
  20. node *next;
  21.  
  22. //default ctor
  23. node() { next = nullptr; }
  24.  
  25. //default dtor
  26. ~node() { delete root_node; }
  27. };
  28.  
  29. private:
  30. node *root_node = nullptr;
  31. int elements = 0;
  32. };
  33.  
  34. #endif //STACK_H
  35.  
  36. /* Definitons of the STACK data structure implemented using a linked list */
  37.  
  38. #include <iostream>
  39. #include "stack.h"
  40.  
  41. using std::cout;
  42. using std::endl;
  43.  
  44.  
  45. /* FUNCTION: Test to check if the stack is empty or has at least one element */
  46.  
  47. template <class T>
  48. bool stack<T>::empty() { return (root_node == nullptr); }
  49.  
  50. /* FUNCTION: Returns current size of the stack */
  51. template <class T>
  52. int stack<T>::size() { return elements; }
  53.  
  54.  
  55. /* FUNCTION: Adds nodes to the stack with one argument which is the data to be inserted */
  56.  
  57. template <class T>
  58. void stack<T>::push(T data) {
  59.  
  60. //Operation to preform if the stack is empty.
  61. //Root element is popped off last (First in, Last out)
  62. if ( empty() ) {
  63. root_node = new node;
  64. root_node->node_data = data;
  65. root_node->next = nullptr;
  66. elements++;
  67. }
  68.  
  69. //Operation to preform if stack is not empty.
  70. //Elements inserted into stack with dynamic allocation.
  71. else {
  72. node *new_node = new node;
  73. *new_node = *root_node;
  74. root_node->next = new_node;
  75. root_node->node_data = data;
  76.  
  77. elements++;
  78. }
  79. }
  80.  
  81. /* FUNCTION: Removes element at the top of the stack */
  82. template <class T>
  83. void stack<T>::pop() {
  84.  
  85. if (size() > 1) {
  86. node *temp_node = new node;
  87. temp_node = root_node->next;
  88.  
  89. root_node = temp_node;
  90. elements--;
  91. }
  92.  
  93. else if (size() == 1) {
  94. root_node = nullptr;
  95. elements--;
  96. }
  97.  
  98. else {cout << "nOperation pop() failed: Stack is empty!" << endl;}
  99. }
  100.  
  101. /* FUNCTION: Retrieves element at the top of the stack */
  102. template <class T>
  103. T stack<T>::top() {
  104. if (!empty()) {return root_node->node_data;}
  105. else {cout << "nOperation top() failed: Stack is empty!" << endl; return -1;}
  106. }
  107.  
  108. /* FUNCTION: Prints the stack contents */
  109. template <class T>
  110. void stack<T>::print() {
  111. int index = size();
  112. for (int i = 1; i < index; i++) {
  113. cout << "Element " << i << ": " << " " << top() << endl;
  114. pop();
  115. }
  116. }
  117.  
  118. void function(const argument& a);
  119.  
  120. void function(argument& a); // a is modified in the function (I/O parameter)
  121.  
  122. void function(argument a); // function "owns" a (gets it's own exclusive copy)
  123.  
  124. template<typename T>
  125. void stack<T>::push(T data) // pass by value here
  126. {
  127. root = new node{ std::move(data), root }; // and move the value here
  128. ++elements;
  129. }
  130.  
  131. template<typename T>
  132. const T& // return a const T&
  133. stack<T>::top() const // and function is const
  134. {
  135. if (!root)
  136. throw std::runtime_error("stack<T>::top: empty stack");
  137. return root->data;
  138. }
  139.  
  140. int size();
  141.  
  142. std::size_t size() const;
  143.  
  144. root_node = new node;
  145. root_node->node_data = data;
  146. root_node->next = nullptr;
  147.  
  148. root_node = new node{ std::move(data), root_node }; // use std::move on the data
  149.  
  150. template <class T>
  151. class stack {
  152. // ...
  153. struct node { // struct, because it is internal and only
  154. // access will be in stack
  155. // (i.e. we can guarantee correct use in client code)
  156.  
  157. T *node_data;
  158. node* next;
  159. };
  160. }
  161.  
  162. template <class T>
  163. void stack<T>::pop() {
  164. if(empty())
  165. throw std::runtime_error{"stack<T>::pop(): empty stack"};
  166.  
  167. node* new_root = root_node->next;
  168. delete root_node;
  169. root_node = new_root;
  170. --elements;
  171. }
  172.  
  173. struct node {
  174.  
  175. node *next;
  176.  
  177. //default ctor
  178. //default dtor
  179.  
  180. ~node() { delete root_node; }
  181.  
  182. #include <iostream>
  183. #include "stack.h"
  184.  
  185. using std::cout;
  186. using std::endl;
  187.  
  188. void stack<T>::push(T data) {
  189.  
  190. root_node = new node; // `node` contains a T and it must be
  191. // be created by default construction.
  192.  
  193. root_node->node_data = data; // Force a copy of the object into node.
  194.  
  195. void stack<T>::push(T data) {
  196. root_node = new node(data, root_node);
  197. elements++;
  198. }
  199.  
  200. node *temp_node = new node; // Create a node we don't need.
  201. temp_node = root_node->next; // Then we leak it the net line.
  202.  
  203. root_node = temp_node; // The old value of root node was just
  204. // lost and no way to delete it.
  205.  
  206. void stack<T>::pop() {
  207. node* tmp = root_node; // Keep track of the old root.
  208. root_node = root_node->next; // Remove the root node from the list.
  209. elements--;
  210. delete tmp; // delete the old root.
  211. }
  212.  
  213. void print(std::ostream& stream = std::cout);
  214.  
  215. else {cout << "nOperation top() failed: Stack is empty!" << endl; return -1;}
  216.  
  217. else { throw std::runtime_error("XXXX");}
  218.  
  219. class Stack
  220. {
  221. public:
  222. bool empty() const;
  223. int size() const;
  224.  
  225. #ifndef STACK_H
  226. #define STACK_H
  227.  
  228. #include <iostream>
  229. #include <stdexcept>
  230.  
  231. template <typename T>
  232. class Stack {
  233.  
  234. struct Node {
  235. T data;
  236. Node* next;
  237.  
  238. Node(T const& data, Node* next)
  239. : data(data)
  240. , next(next)
  241. {}
  242. Node(T&& data, Node* next) // With C++11 allow move construction
  243. : data(std::move(data))
  244. , next(next)
  245. {}
  246. };
  247.  
  248. public:
  249. ~Stack();
  250. void push(T const& data);
  251. void push(T&& data); // WIth C++ 11 allow move construction
  252. bool empty() const;
  253. int size() const;
  254. T top() const;
  255. void pop();
  256. void print(std::ostream& str = std::cout) const;
  257.  
  258.  
  259. private:
  260. Node* root = nullptr;
  261. int elements = 0;
  262. };
  263.  
  264. template<typename T>
  265. Stack<T>::~Stack()
  266. {
  267. Node* next;
  268. for(Node* loop = root; loop != nullptr; loop = next)
  269. {
  270. next = loop->next;
  271. delete loop;
  272. }
  273. }
  274. template<typename T>
  275. void Stack<T>::push(T const& data)
  276. {
  277. root = new Node(data, root);
  278. ++elements;
  279. }
  280. template<typename T>
  281. void Stack<T>::push(T&& data)
  282. {
  283. root = new Node(std::forward(data), root);
  284. ++elements;
  285. }
  286. template<typename T>
  287. bool Stack<T>::empty() const
  288. {
  289. return root == nullptr;
  290. }
  291. template<typename T>
  292. int Stack<T>::size() const
  293. {
  294. return elements;
  295. }
  296. template<typename T>
  297. T Stack<T>::top() const
  298. {
  299. if (root == nullptr)
  300. { throw std::runtime_error("Invalid Action");
  301. }
  302. return root->data;
  303. }
  304. template<typename T>
  305. void Stack<T>::pop()
  306. {
  307. if (root == nullptr)
  308. { throw std::runtime_error("Invalid Action");
  309. }
  310. Node* tmp = root;
  311. root = root->next;
  312. --elements;
  313. delete tmp;
  314. }
  315. template<typename T>
  316. void Stack<T>::print(std::ostream& str) const
  317. {
  318. int id = 0;
  319. for(Node* loop=root; loop != nullptr; loop=loop->next, ++id)
  320. {
  321. str << "Element: " << id << " = " << loop->data << "n";
  322. }
  323. }
  324.  
  325. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement