Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #ifndef STACK_LINKED_LIST
  2. #define STACK_LINKED_LIST
  3.  
  4. #include<iostream>
  5.  
  6. template<typename T>
  7. struct StackElement
  8. {
  9. T data;
  10. StackElement<T>* link;
  11. };
  12.  
  13. template<typename T>
  14. class StackList
  15. {
  16. public:
  17. StackList() : topElement(nullptr) {};
  18. ~StackList();
  19. StackList(const StackList<T>& other);
  20. StackList<T>& operator=(const StackList<T>& other);
  21. void push(const T& element);
  22. bool empty() const;
  23. T top() const;
  24. T pop();
  25. //friend std::istream& operator>>(std::istream& in, StackList<T> stack);
  26. friend std::ostream& operator<<(std::ostream& out, StackList<T> stack);
  27.  
  28. private:
  29. StackElement<T>* topElement;
  30. void copy(StackElement<T>* other);
  31. void deleteStack();
  32. void copyStack(const StackList<T>& other);
  33. };
  34.  
  35. template<typename T>
  36. std::ostream& operator<<(std::ostream& out, StackList<T> stack)
  37. {
  38. while (!stack.empty())
  39. {
  40. T x = stack.pop();
  41. out << x << " ";
  42. }
  43. return out;
  44. }
  45.  
  46. template<typename T>
  47. bool StackList<T>::empty() const
  48. {
  49. return topElement == nullptr;
  50. }
  51.  
  52. template<typename T>
  53. void StackList<T>::push(const T& element)
  54. {
  55. StackElement<T>* newElementPtr = new StackElement<T>;
  56. newElementPtr->data = element;
  57. newElementPtr->link = topElement;
  58. topElement = newElementPtr;
  59. }
  60.  
  61. template<typename T>
  62. T StackList<T>::pop()
  63. {
  64. if (empty())
  65. {
  66. std::cerr << "Cannot pop from an empty stack" << std::endl;
  67. return T();
  68. }
  69. StackElement<T>* tempElement = topElement;
  70. topElement = topElement->link;
  71. T x = tempElement->data;
  72. delete tempElement;
  73.  
  74. return x;
  75. }
  76.  
  77. template<typename T>
  78. T StackList<T>::top() const
  79. {
  80. if (empty())
  81. {
  82. std::cerr << "Cannot get an element from an empty stack." << std::endl;
  83. return T();
  84. }
  85. return topElement->data;
  86. }
  87.  
  88. template<typename T>
  89. void StackList<T>::deleteStack()
  90. {
  91. while (!empty())
  92. pop();
  93. }
  94.  
  95. template<typename T>
  96. StackList<T>::~StackList()
  97. {
  98. deleteStack();
  99. }
  100.  
  101. template<typename T>
  102. void StackList<T>::copy(StackElement<T>* element)
  103. {
  104. if (element == nullptr)
  105. return;
  106. copy(element->link);
  107. push(element->data);
  108. }
  109.  
  110. template<typename T>
  111. void StackList<T>::copyStack(const StackList<T>& other)
  112. {
  113. topElement = nullptr;
  114. copy(other.topElement);
  115. }
  116.  
  117. template<typename T>
  118. StackList<T>::StackList(const StackList<T>& other)
  119. {
  120. copyStack(other);
  121. }
  122.  
  123. template<typename T>
  124. StackList<T>& StackList<T>::operator=(const StackList<T>& other)
  125. {
  126. if (this != &other)
  127. {
  128. deleteStack();
  129. copyStack(other);
  130. }
  131. return *this;
  132. }
  133.  
  134.  
  135. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement