Guest User

Untitled

a guest
Dec 8th, 2019
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. template<typename T>
  8. struct Node
  9. {
  10. T data;
  11. Node<T> *next;
  12. Node<T> *prev;
  13. };
  14.  
  15. template<typename T>
  16. class List
  17. {
  18. protected:
  19. Node<T> *head;
  20. Node<T> *tail;
  21. long amount;
  22. public:
  23. List()
  24. {
  25. head = nullptr;
  26. tail = nullptr;
  27. amount = 0;
  28. }
  29. ~List()
  30. {
  31. while (head)
  32. {
  33. this->pop_head();
  34. }
  35. }
  36.  
  37. //Длина списка
  38. long size()
  39. {
  40. return this->amount;
  41. }
  42.  
  43. //Добавление с головы
  44. void push_head(T input_object)
  45. {
  46. if (head == nullptr)
  47. {
  48. head = new Node<T>;
  49. head->data = input_object;
  50. head->next = nullptr;
  51. head->prev = nullptr;
  52. tail = head;
  53. amount++;
  54. return;
  55. }
  56. Node<T> *node = new Node<T>;
  57. node->data = input_object;
  58. node->next = head;
  59. node->prev = nullptr;
  60. head->prev = node;
  61. head = node;
  62. amount++;
  63. return;
  64. }
  65.  
  66. //Добавление в хвост
  67. void push_tail(T input_object)
  68. {
  69. if (head == nullptr)
  70. {
  71. head = new Node<T>;
  72. head->data = input_object;
  73. head->next = nullptr;
  74. head->prev = nullptr;
  75. tail = head;
  76. amount++;
  77. return;
  78. }
  79. Node<T> *node = new Node<T>;
  80. node->data = input_object;
  81. node->next = nullptr;
  82. node->prev = tail;
  83. tail->next = node;
  84. tail = node;
  85. amount++;
  86. return;
  87. }
  88.  
  89. //Удаление от головы
  90. T pop_head()
  91. {
  92. if (!(head)) return T();
  93. T data = head->data;
  94. Node<T> *node = head;
  95. head = head->next;
  96. if (head)
  97. head->prev = nullptr;
  98. delete node;
  99. amount--;
  100. return data;
  101. }
  102.  
  103. //Удаление из хвоста
  104. T pop_tail()
  105. {
  106. if (!(head)) return T();
  107. T data = tail->data;
  108. Node<T> *node = tail;
  109. tail = tail->prev;
  110. if (tail)
  111. tail->next = nullptr;
  112. delete node;
  113. amount--;
  114. return data;
  115. }
  116.  
  117. // произвольный доступ к объектам
  118. T &operator[](long num)
  119. {
  120. Node<T> *curr = head;
  121. if (num < 0 || num >= amount) return curr->data;
  122. for (long i = 0; i < num; i++)
  123. curr = curr->next;
  124. return curr->data;
  125. }
  126.  
  127. // удаление элемента из списка
  128. void delete_element(Node<T> *node)
  129. {
  130. if (head == node) head = head->next;
  131. if (tail == node) tail = tail->prev;
  132. if (node->next != nullptr) node->next->prev = node->prev;
  133. if (node->prev != nullptr) node->prev->next = node->next;
  134. delete node;
  135. node = nullptr;
  136. this->amount--;
  137. }
  138.  
  139. // вывод на экран содержимого списка
  140. void List<T>::output()
  141. {
  142. int i = 0;
  143. if (!head) cout << "Список пуст!";
  144. else for (Node<T> *node = head; node != nullptr; node = node->next)
  145. {
  146. cout << node->data;
  147. i++;
  148. }
  149. }
  150.  
  151. void sort()
  152. {
  153. Node<T> *cur = head;
  154. while (cur->next)
  155. {
  156. if ((cur->data.getAge()) > (cur->next->data.getAge()))
  157. {
  158. cTourist temp = cur->next->data;
  159. cur->next->data = cur->data;
  160. cur->data = temp;
  161.  
  162. cur = cur->next;
  163. sort();
  164. }
  165. else { cur = cur->next; }
  166. }
  167. }
  168. };
RAW Paste Data