Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2021
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #include <algorithm>
  2. #include "func.hpp"
  3.  
  4.  
  5. template<typename T>
  6. List<T>::List()
  7. {
  8. Size = 0;
  9. head = nullptr;
  10. }
  11.  
  12. template<typename T>
  13. List<T>::~List()
  14. {
  15. clear();
  16. }
  17.  
  18. template<typename T>
  19. void List<T>::push_back(T data)
  20. {
  21. if(head == nullptr)
  22. {
  23. head = tail = new Node(data);
  24. }
  25. else
  26. {
  27. tail->_pNext = new Node(data);
  28. tail = tail->_pNext;
  29. }
  30. ++Size;
  31. }
  32.  
  33. template<typename T>
  34. void List<T>::push_front(T data)
  35. {
  36. if(head == nullptr)
  37. {
  38. head = tail = new Node(data);
  39. }
  40. else
  41. {
  42. Node* new_node = new Node();
  43.  
  44. new_node->_pNext = head;
  45.  
  46. new_node->_data = data;
  47.  
  48. head = new_node;
  49.  
  50. ++Size;
  51. }
  52. }
  53.  
  54. template<typename T>
  55. T& List<T>::operator[](const int index)
  56. {
  57. Node* current = head;
  58.  
  59. int counter = 0;
  60. while (counter != index)
  61. {
  62. current = current->_pNext;
  63.  
  64. counter++;
  65. }
  66. return current->_data;
  67. }
  68.  
  69. template<typename T>
  70. void List<T>::pop_front()
  71. {
  72. Node* temp = head;
  73. head = head->_pNext;
  74.  
  75. delete temp;
  76.  
  77. --Size;
  78. }
  79.  
  80. template<typename T>
  81. void List<T>::pop_back()
  82. {
  83. Node* cur_node = head;
  84. while(cur_node->_pNext != tail)
  85. {
  86. cur_node = cur_node->_pNext;
  87. }
  88.  
  89. delete tail;
  90.  
  91. cur_node->_pNext = nullptr;
  92. tail = cur_node;
  93.  
  94. --Size;
  95. }
  96.  
  97. template<typename T>
  98. void List<T>::insert(T data, int index)
  99. {
  100. if(index == 0)
  101. {
  102. push_front(data);
  103. }
  104. else
  105. {
  106. Node* previous = head;
  107. for (int i = 0; i < index - 1; i++)
  108. {
  109. previous = previous->_pNext;
  110. }
  111.  
  112. Node* new_elem = new Node(data);
  113.  
  114. new_elem->_pNext = previous->_pNext;
  115.  
  116. previous->_pNext = new_elem;
  117.  
  118. ++Size;
  119. }
  120. }
  121.  
  122. template<typename T>
  123. void List<T>::removeAt(int index)
  124. {
  125. if(index == 0)
  126. {
  127. pop_front();
  128. }
  129. else
  130. {
  131. Node* previous = head;
  132.  
  133. int count = 0;
  134. while(count < index - 1)
  135. {
  136. previous = previous->_pNext;
  137.  
  138. count++;
  139. }
  140. Node* toDelete = previous->_pNext;
  141.  
  142. previous->_pNext = toDelete->_pNext;
  143.  
  144. delete toDelete;
  145.  
  146. --Size;
  147. }
  148. }
  149.  
  150. template<typename T>
  151. void List<T>::clear()
  152. {
  153. while (Size != 0)
  154. {
  155. pop_front();
  156. }
  157. }
  158. template<typename T>
  159. void List<T>::ShiftRight()
  160. {
  161. Node* current = head;
  162. while(current->_pNext != tail)
  163. {
  164. current = current->_pNext;
  165. }
  166. current->_pNext = nullptr;
  167.  
  168. Node* new_curr = head;
  169.  
  170. head = tail;
  171.  
  172. head->_pNext = new_curr;
  173.  
  174. tail = current;
  175. }
  176.  
  177. template<typename T>
  178. void List<T>::Sort()
  179. {
  180. Node* new_curr = head;
  181. for(int i = 0; i < Size; i++)
  182. {
  183. for(int j = 0; j < Size - i - 1; j++)
  184. {
  185. if(new_curr->_data > new_curr->_pNext->_data)
  186. {
  187. std::swap(new_curr->_data, new_curr->_pNext->_data);
  188. }
  189.  
  190. new_curr = new_curr->_pNext;
  191. }
  192. new_curr = head;
  193. }
  194. }
  195.  
  196. template<typename T>
  197. void List<T>::Reverse()
  198. {
  199. Node* p1 = head;
  200. Node* p2 = head->_pNext;
  201. Node* p3 = head->_pNext->_pNext;
  202.  
  203. p1->_pNext = nullptr;
  204. while(p3 != nullptr)
  205. {
  206. p2->_pNext = p1;
  207.  
  208. p1 = p2;
  209. p2 = p3;
  210. p3 = p3->_pNext;
  211. }
  212. p2->_pNext = p1;
  213.  
  214. tail = head;
  215.  
  216. head = p2;
  217. }
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement