Advertisement
Guest User

Untitled

a guest
May 27th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. //
  2. // Created by alex on 04.05.19.
  3. //
  4. #pragma once
  5.  
  6. //#ifndef LAB_12_MYLIST_H
  7. //#define LAB_12_MYLIST_H
  8.  
  9. template <typename T>
  10. class MyList{
  11. public:
  12.  
  13. struct Node{
  14. T data;
  15. Node *next, *prev;
  16.  
  17. public:
  18. Node()
  19. {
  20. this->next = nullptr;
  21. this->prev = nullptr;
  22. }
  23.  
  24. Node(T data, Node* next, Node *prev)
  25. {
  26. this->data = data;
  27. this->next = next;
  28. this->prev = prev;
  29. }
  30. };
  31.  
  32. int amount;
  33. Node *head, *tail;
  34.  
  35. MyList();
  36. MyList(const MyList<T> &);
  37. ~MyList();
  38. int GetLength();
  39. MyList<T>& PushBack(T data);
  40. MyList<T>& PopBack();
  41. MyList<T>& PushFront(T data);
  42. MyList<T>& PopFront();
  43. MyList<T>& Sort();
  44. MyList<T>& Map(T map_func(T obj));
  45. MyList<T>& Where(bool where_func(T obj));
  46. void Swap(Node *first_node, Node *second_node);
  47. void PrintList();
  48. };
  49.  
  50. template <typename T>
  51. MyList<T>::MyList()
  52. {
  53. this->head = nullptr;
  54. this->tail = nullptr;
  55. this->amount = 0;
  56. };
  57.  
  58. template <typename T>
  59. MyList<T>::MyList(const MyList<T> &my_list) // TODO переделать конструктор копирования
  60. {
  61. this->head = nullptr;
  62. this->tail = nullptr;
  63. this->amount = 0;
  64.  
  65. Node *node = my_list.head;
  66.  
  67. while(node != nullptr)
  68. {
  69. Node *new_node = new Node();
  70.  
  71. new_node->data = node->data;
  72. new_node->next = nullptr;
  73. new_node->prev = tail;
  74.  
  75. node = node->next;
  76. }
  77.  
  78. amount = my_list.amount;
  79. };
  80.  
  81. template <typename T>
  82. MyList<T>::~MyList() = default;
  83.  
  84. template <typename T>
  85. MyList<T>& MyList<T>::PushBack(T data)
  86. {
  87. Node *new_node = new Node();
  88.  
  89. new_node->data = data;
  90. new_node->next = nullptr;
  91. new_node->prev = tail;
  92.  
  93. if (tail != nullptr) {
  94. tail->next = nullptr;
  95. tail->next = new_node;
  96. }
  97. else{
  98. head = new_node;
  99. }
  100.  
  101. tail = new_node;
  102. amount++;
  103.  
  104. return *this;
  105. };
  106.  
  107. template <typename T>
  108. MyList<T>& MyList<T>::PopBack()
  109. {
  110. if (amount == 1)
  111. {
  112. head = nullptr;
  113. tail = nullptr;
  114. }
  115. else if (amount == 0)
  116. {
  117. return *this;
  118. }
  119. else
  120. {
  121. tail = tail->prev;
  122.  
  123. tail->next = nullptr;
  124. }
  125.  
  126. amount--;
  127. return *this;
  128. };
  129.  
  130. template <typename T>
  131. MyList<T>& MyList<T>::PushFront(T data)
  132. {
  133. Node *new_node = new Node();
  134.  
  135. new_node->data = data;
  136. new_node->next = head;
  137.  
  138. if (head != nullptr) {
  139.  
  140. head->prev = new_node;
  141. }
  142. else
  143. {
  144. tail = new_node;
  145. }
  146.  
  147. head = new_node;
  148. amount++;
  149.  
  150. return *this;
  151. };
  152.  
  153. template <typename T>
  154. void MyList<T>::PrintList()
  155. {
  156. Node *printing_node = this->head;
  157.  
  158. std::cout << '{';
  159. for (int i = 0; i < amount; i++)
  160. {
  161. std::cout << printing_node->data;
  162. if (i == amount -1) break;
  163. std::cout << ", ";
  164. printing_node = printing_node->next;
  165. }
  166. std::cout << "}\n";
  167. }
  168.  
  169. template <typename T>
  170. MyList<T>& MyList<T>::PopFront()
  171. {
  172. if (amount == 1)
  173. {
  174. head = nullptr;
  175. tail = nullptr;
  176. }
  177. else if (amount == 0)
  178. {
  179. return *this;
  180. }
  181. else
  182. {
  183. head = head->next;
  184.  
  185. head->prev = nullptr;
  186. }
  187.  
  188. amount--;
  189. return *this;
  190. };
  191.  
  192. template <typename T>
  193. MyList<T>& MyList<T>::Sort()
  194. {
  195. Node *start_node = head;
  196.  
  197. for (int i = 0; i < amount; i++)
  198. {
  199. Node* tmp_node = start_node->next;
  200. Node* min_elem_node = start_node;
  201.  
  202. for (int j = i + 1; j < amount; j++)
  203. {
  204. if (tmp_node->data < min_elem_node->data) min_elem_node = tmp_node;
  205. if (j == amount - 1) break;
  206. tmp_node = tmp_node->next;
  207. }
  208.  
  209. this->Swap(start_node, min_elem_node);
  210. if (i == amount - 1) break;
  211. start_node = start_node->next;
  212. }
  213.  
  214. return *this;
  215. };
  216.  
  217. template <typename T>
  218. int MyList<T>::GetLength()
  219. {
  220. return amount;
  221. }
  222.  
  223. template<typename T>
  224. MyList<T>& MyList<T>::Map(T map_func(T obj))
  225. {
  226. Node *map_node = this->head;
  227.  
  228. for (int i = 0; i < amount; i++)
  229. {
  230. map_node->data = map_func(map_node->data);
  231. if (i == amount -1) break;
  232. map_node = map_node->next;
  233. }
  234.  
  235. return *this;
  236. }
  237.  
  238. template<typename T>
  239. MyList<T>& MyList<T>::Where(bool where_func(T obj))
  240. {
  241. MyList<T> new_list = *new MyList<T>();
  242. Node *where_node = this->head;
  243.  
  244. for(int i = 0; i < amount; i++)
  245. {
  246. if (where_func(where_node->data))
  247. {
  248. new_list.PushBack(where_node->data);
  249. }
  250.  
  251. if (i == amount -1) break;
  252. where_node = where_node->next;
  253. }
  254.  
  255. return new_list;
  256. }
  257.  
  258. template<typename T>
  259. void MyList<T>::Swap(MyList::Node *first_node, MyList::Node *second_node)
  260. {
  261. if (first_node == second_node) return;
  262.  
  263. T tmp_data = first_node->data;
  264. first_node->data = second_node->data;
  265. second_node->data = tmp_data;
  266. }
  267.  
  268.  
  269. //#endif //LAB_12_MYLIST_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement