Advertisement
Guest User

Untitled

a guest
May 16th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #pragma once
  2. #include <string>
  3. #include <iostream>
  4. #include "Node.h"
  5. #include "List.h"
  6.  
  7.  
  8. template <class T>
  9. class Stack : public List<T>
  10. {
  11. private:
  12. //const int SIZEMAX = 5;
  13. int size; //size of stack
  14. Node<T> *baseNode; //a base dummy node to attach other nodes to.
  15. List<T> *list; //base list to put nodes into.
  16. public:
  17. Stack() : list(new List<T>), size(0), baseNode(new Node<T>) //initializeing size to 0, and makeing list and basenode in the heap
  18. {
  19. list->setFirst(baseNode); //setting the very first node in the list to be the base node of the class
  20. };
  21. void push(Node <T> *ptr);
  22. void pop();
  23. void emptystack();
  24. bool isempty();
  25. void deleteStack();
  26. T getTop();
  27. };
  28. template <class T>
  29. T Stack<T>::getTop()
  30. {
  31. return list->getData(1); //gets the head nodes data
  32. }
  33. template<class T>
  34. inline void Stack<T>::push(Node <T> *ptr)
  35. {
  36. list->insertFirst(ptr);
  37. size++;
  38. }
  39.  
  40. template<class T>
  41. inline void Stack<T>::pop()
  42. {
  43. size--;
  44. if (size >= 0)
  45. {
  46. list->deleteFirst(); //pops out node at the head of the list
  47.  
  48. }
  49. if (size < 0)
  50. {
  51. size++; //if the size is less than zero and would overflow, throw a signal
  52. throw (size - 1);
  53. }
  54. }
  55.  
  56. template<class T>
  57. inline void Stack<T>::emptystack()
  58. {
  59. try {
  60. while (size > 0)
  61. {
  62. pop(); //calls pop over and over till the stack is empty
  63. }
  64. }
  65.  
  66. catch (int er)
  67. {
  68. cout << "MAJOR UNDERFLOW ERROR, STACK SIZE: " << size << endl; //if the empty function emptys more than it should that it underflows throws a signal and gets caught
  69. }
  70.  
  71. }
  72.  
  73. template<class T>
  74. inline bool Stack<T>::isempty()
  75. {
  76. if (size == 0)
  77. {
  78. return true; //checks if the stack is now empty
  79. }
  80. /*if (size > SIZEMAX)
  81. {
  82. cout << "WARNING OVERFLOWING STACK" << endl; //if the stack is overflowing sends a signal
  83. throw size;
  84. }*/
  85. else if (size < 0)
  86. {
  87. cout << "WARNING UNDERFLOWING STACK" << endl; //if the stack is underflowing sends a signal
  88. throw size;
  89. }
  90. else if (size > 0 )
  91. {
  92. return false; //if size is not overflowing and not underflowing but still has nodes inside it will return false
  93. }
  94.  
  95. }
  96. template<class T>
  97. void Stack<T>::deleteStack()
  98. {
  99. list->emptyList(); //deletes all nodes in the list
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement