MyStack2.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #pragma once
  2. #include <iostream>
  3. template <class T> class MyStack2
  4. {
  5. class Node
  6. {
  7. public:
  8. Node* pNext; //óêàçàòåëü íà ñëåäóþùèé ýëåìåíò
  9. T m_Data;
  10. //Ìåòîäû:
  11. Node(): pNext(nullptr), m_Data() {}
  12. Node(Node *prev, const T& data):m_Data(data) {this->pNext = prev->pNext; prev->pNext = this;}//new Node «ïîäêëþ÷àåòñÿ» â ñïèñîê.
  13. //~Node() { }
  14. };
  15. Node* Head;
  16. size_t exist;
  17. public:
  18. MyStack2():Head(new Node),exist(0) {};// äåôîëòîâûé êîíòðóêòîð
  19. MyStack2(const MyStack2& other);
  20. MyStack2(MyStack2&& other);
  21. MyStack2& operator=(const MyStack2& other);
  22. MyStack2& operator=(MyStack2&& other);
  23. ~MyStack2();
  24. void Push(const T& =T());
  25. T Pop();
  26. void Print();
  27. void rPrint();
  28. };
  29. template<class T>
  30. inline MyStack2<T>::MyStack2(const MyStack2& other)
  31. :Head(new Node), exist(other.exist)
  32. {
  33. //for (size_t it = other.exist; it > 0; it--)
  34. //{
  35. // Node* tmp = other.Head->pNext;
  36. // Node* tmpPrev = other.Head;
  37. // /*for (size_t i = 0; i < it; i++)
  38. // {
  39. // tmpPrev = tmpPrev->pNext;
  40. // tmp = tmp->pNext;
  41. // }
  42. // Push(tmpPrev->m_Data);*/
  43. //}
  44. Node* tmp = other.Head->pNext;
  45. Node* tmpPrev = Head;
  46. for (size_t it = other.exist; it > 0; it--)
  47. {
  48. tmpPrev = new Node(tmpPrev, tmp->m_Data);
  49. tmp = tmp->pNext;
  50. }
  51. }
  52. template<class T>
  53. inline MyStack2<T>::MyStack2(MyStack2&& other)
  54. :Head (other.Head), exist(other.exist)
  55. {
  56. other.Head = new Node;
  57. other.exist = 0;
  58. }
  59. template<class T>
  60. inline MyStack2<T>& MyStack2<T>::operator=(const MyStack2& other)
  61. {
  62. if (&other != this)
  63. {
  64. if (exist >= other.exist)
  65. {
  66. while (exist!= other.exist)
  67. {
  68. Pop();
  69. }
  70. if (!exist) { return *this; }
  71. }
  72. Node* tmpOther = other.Head->pNext;
  73. Node* tmpThis = Head->pNext;
  74. if (exist < other.exist)
  75. {
  76. for (size_t i = 0; i < other.exist - exist; i++)
  77. {
  78. tmpOther = tmpOther->pNext;
  79. }
  80. }
  81. for (size_t it = 0; it < exist; it++)
  82. {
  83. tmpThis->m_Data = tmpOther->m_Data;
  84. tmpThis = tmpThis->pNext;
  85. tmpOther= tmpOther->pNext;
  86. }
  87. for (size_t it = other.exist - exist; it > 0; it--)
  88. {
  89. Node* tmpPrev = other.Head;
  90. tmpOther = tmpPrev->pNext;
  91. for (size_t i = 0; i < it; i++)
  92. {
  93. tmpPrev = tmpPrev->pNext;
  94. tmpOther = tmpOther->pNext;
  95. }
  96. Push(tmpPrev->m_Data);
  97. }
  98. }
  99. return *this;
  100. }
  101. template<class T>
  102. inline MyStack2<T>::~MyStack2()
  103. {
  104. while (exist != 0)
  105. {
  106. Node* del = Head->pNext;
  107. Head->pNext = del->pNext;
  108. delete del;
  109. exist--;
  110. }
  111. delete Head;
  112. }
  113. template<class T>
  114. inline void MyStack2<T>::Push(const T& Data)
  115. {
  116. new Node(Head,Data);
  117. exist++;
  118. }
  119. template<class T>
  120. inline T MyStack2<T>::Pop()
  121. {
  122. if (exist != 0)
  123. {
  124. T tmpT = Head->pNext->m_Data;//åñòü ëè ñìûñë ëè ïåðåìåùàòü â äàííûå ãîëîâó è âîçâðàùàòü ññûëêó íà äàííûå à íå ïî çíà÷åíèþ?
  125. Node* tmpN = Head->pNext;
  126. Head->pNext = tmpN->pNext;
  127. delete tmpN;
  128. exist--;
  129. return tmpT;
  130. }
  131. throw std::out_of_range("No Data");
  132. }
  133. template<class T>
  134. inline void MyStack2<T>::Print()
  135. {
  136. if (exist == 0) { std::cout << "Empty list" << std::endl; return; }
  137. Node* tmp = Head->pNext;
  138. for (size_t i = 0; i < exist; i++)
  139. {
  140. std::cout << tmp->m_Data << ' ';
  141. tmp = tmp->pNext;
  142. }
  143. std::cout << std::endl;
  144. }
  145. template<class T>
  146. inline void MyStack2<T>::rPrint()
  147. {
  148. if (exist==0){ std::cout <<"Empty list" << std::endl; return;}
  149. for (size_t it = exist; it > 0; it--)
  150. {
  151. Node* tmp = Head->pNext;
  152. Node* tmpPrev = Head;
  153. for (size_t i = 0; i < it; i++)
  154. {
  155. tmpPrev = tmpPrev->pNext;
  156. tmp = tmp->pNext;
  157. }
  158. std::cout << tmpPrev->m_Data << ' ';
  159. }
  160. std::cout << std::endl;
  161. }