Advertisement
Andrey_ZoZ

Untitled

Jul 29th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template <typename ElemT>
  4. class ListFw
  5. {
  6. class Node
  7. {
  8. public:
  9. ElemT data;
  10. Node* next;
  11. Node(const ElemT& dataP, Node* nextP = nullptr)
  12. :data{ dataP }, next{ nextP }{};
  13. };
  14.  
  15. Node* head;
  16. Node* tail;
  17. size_t size;
  18. public:
  19. ListFw() : head{ nullptr }, tail{ nullptr }, size{ 0 } {}
  20. ElemT& front() { return head->data; };
  21. const ElemT& front()const { return head->data; };
  22. ElemT& back() { return tail->data; };
  23. const ElemT& back()const { return tail->data; };
  24. const size_t getSize() const { return size; };
  25. bool empty() const { return size == 0; };
  26. void push_back(const ElemT& data);
  27. void push_front(const ElemT& data);
  28. bool pop_back();
  29. bool pop_front();
  30. void clear();
  31. ListFw clone();
  32. ListFw operator+(ListFw& object);
  33. ListFw operator*(ListFw& object);
  34. void operator=(ListFw& object)
  35. {
  36. clear();
  37. for (Node* curr{object.head}; curr!=nullptr; curr=curr->next) {
  38. push_front(curr->data);
  39. }
  40. }
  41. void operator=(ListFw&& object)
  42. {
  43. clear();
  44. size=object.size;
  45. head=object.head;
  46. tail=object.tail;
  47. object.head=nullptr;
  48. object.tail=nullptr;
  49. }
  50. ListFw(ListFw& object)
  51. {
  52. size=object.size;
  53. for (Node* curr{object.head}; curr!=nullptr; curr=curr->next) {
  54. push_front(curr->data);
  55. }
  56. }
  57. ~ListFw();
  58. };
  59.  
  60. template <typename ElemT>
  61. void ListFw<ElemT>::push_back(const ElemT& data)
  62. {
  63. if (!head)
  64. {
  65. head = new Node{ data };
  66. tail = head;
  67. }
  68. else
  69. {
  70. tail->next = new Node{ data };
  71. tail = tail->next;
  72. }
  73. ++size;
  74. }
  75.  
  76. template <typename ElemT>
  77. void ListFw<ElemT>::push_front(const ElemT& data)
  78. {
  79. head = new Node{ data, head };
  80. ++size;
  81.  
  82. if (!tail)
  83. {
  84. tail = head;
  85. }
  86. }
  87.  
  88. template <typename ElemT>
  89. bool ListFw<ElemT>::pop_front()
  90. {
  91. if (head)
  92. {
  93. Node* tmp{ head };
  94. head = head->next;
  95. delete tmp;
  96. --size;
  97. return true;
  98. }
  99. return false;
  100. }
  101.  
  102. template <typename ElemT>
  103. bool ListFw<ElemT>::pop_back()
  104. {
  105. if (tail)
  106. {
  107. if (tail == head)
  108. {
  109. delete tail;
  110. size = 0;
  111. tail = nullptr;
  112. head = nullptr;
  113. return true;
  114. }
  115.  
  116. //Node* curr{ head };
  117.  
  118. //while (curr)
  119. for (Node* curr{ head }; curr; curr = curr->next)
  120. {
  121. if (curr->next == tail)
  122. {
  123. delete tail;
  124. curr->next = nullptr;
  125. tail = curr;
  126. --size;
  127. return true;
  128. }
  129. //curr = curr->next;
  130. }
  131. }
  132. return false;
  133. }
  134.  
  135. template <typename ElemT>
  136. void ListFw<ElemT>::clear()
  137. {
  138. while (tail)
  139. {
  140. std::cout << "Clear for: " << back() << '\n';
  141. pop_back();
  142. }
  143. }
  144.  
  145. template <typename ElemT>
  146. ListFw<ElemT>::~ListFw()
  147. {
  148. while (head)
  149. {
  150. std::cout << "Destruct for: " << front() << '\n';
  151. pop_front();
  152. }
  153. }
  154.  
  155. template<class ElemT>
  156. ListFw<ElemT> ListFw<ElemT>::clone()
  157. {
  158. ListFw objectT;
  159. objectT.size = size;
  160. for (Node* curr{ head }; curr!= nullptr; curr = curr->next) {
  161. objectT.push_back(curr->data);
  162. }
  163. objectT.size = size;
  164. return objectT;
  165. }
  166.  
  167. template<class ElemT>
  168. ListFw<ElemT> ListFw<ElemT>::operator+(ListFw<ElemT>& object)
  169. {
  170. ListFw objectTest;
  171. objectTest = clone();
  172. for (Node* curr{object.head}; curr!=nullptr; curr=curr->next) {
  173. objectTest.push_back(curr->data);
  174. }
  175. size+=object.size;
  176. return objectTest;
  177.  
  178. }
  179.  
  180. template<class ElemT>
  181. ListFw<ElemT> ListFw<ElemT>::operator*(ListFw<ElemT>& object)
  182. {
  183. ListFw objectNew;
  184. for (Node* currObject{object.head},curr{head}; currObject!=nullptr and curr!=nullptr; currObject=currObject->next,curr=curr->next) {
  185. if (curr->data == currObject->data)
  186. {
  187. objectNew.push_front(curr->data);
  188. }
  189. }
  190.  
  191.  
  192. return objectNew;
  193. }
  194.  
  195. int main()
  196. {
  197. /*ListFw<int> l;
  198. std::cout << "Size: " << l.getSize() << '\n';
  199.  
  200. l.push_back(42);
  201. l.push_back(33);
  202.  
  203. std::cout << "Size: " << l.getSize() << '\n';
  204. std::cout << "Front: " << l.front() << '\n';
  205. std::cout << "Back: " << l.back() << '\n';
  206. l.pop_front();
  207. l.pop_back();
  208. std::cout << "Size: " << l.getSize() << '\n';
  209.  
  210. l.push_back(1);
  211. l.push_back(2);
  212. l.push_back(3);
  213. l.push_back(4);
  214. std::cout << "Size: " << l.getSize() << '\n';
  215. std::cout << "Front: " << l.front() << '\n';
  216. std::cout << "Back: " << l.back() << '\n';
  217.  
  218. l.clear();
  219. std::cout << "Size: " << l.getSize() << '\n';
  220.  
  221. l.push_front(1);
  222. l.push_front(2);
  223. l.push_front(3);
  224. l.push_front(4);
  225. std::cout << "Size: " << l.getSize() << '\n';
  226. std::cout << "Front: " << l.front() << '\n';
  227. std::cout << "Back: " << l.back() << '\n';*/
  228.  
  229. ListFw<int> test1;
  230. test1.push_front(1);
  231. test1.push_back(2);
  232. ListFw<int> test2;
  233. test2.push_front(3);
  234. test2.push_back(4);
  235. ListFw<int> test3;
  236. test3 = test2.clone(); //If you want to check code, uncomment from 235 to 237
  237. //std::cout << "Size: " << test3.getSize() << '\n';
  238. //std::cout << "Front: " << test3.front() << '\n';
  239. //std::cout << "Back: " << test3.back() << '\n';
  240. ListFw<int> test4; //If you want to check code, uncomment from 235 to 237
  241. test4=test1+test2;
  242. //std::cout << "Size: " << test4.getSize() << '\n';
  243. //std::cout << "Front: " << test4.front() << '\n';
  244. //std::cout << "Back: " << test4.back() << '\n';
  245. ListFw<int> test5; //Empty be after actions
  246. test5=test1*test2;
  247. std::cout << "Size: " << test5.getSize() << '\n';
  248. std::cout << "Front: " << test5.front() << '\n';
  249. std::cout << "Back: " << test5.back() << '\n';
  250. }
  251.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement