fork download
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4.  
  5. struct Node
  6. {
  7. int key;
  8. struct Node *prev;
  9. struct Node *next;
  10. };
  11.  
  12. struct List
  13. {
  14. struct Node *head;
  15. struct Node *tail;
  16. };
  17.  
  18. void ListInit(struct List &L)
  19. {
  20. L.head = nullptr;
  21. L.tail = nullptr;
  22. }
  23.  
  24. bool ListIsEmpty(struct List L)
  25. {
  26. return (L.head == nullptr && L.tail == nullptr);
  27. }
  28.  
  29. Node *ListSearch(struct List L,int k)
  30. {
  31. Node *x = L.head;
  32. while(x != nullptr && x->key != k)
  33. x = x->next;
  34. return x;
  35. }
  36.  
  37. void ListInsert(struct List &L,int k)
  38. {
  39. Node *x = new Node;
  40. x->key = k;
  41. x->next = L.head;
  42. if(L.head != nullptr)
  43. L.head->prev = x;
  44. L.head = x;
  45. x->prev = nullptr;
  46. if(x->next == nullptr)
  47. L.tail = x;
  48. }
  49.  
  50. void ListDelete(struct List &L,int k)
  51. {
  52. Node *x = ListSearch(L,k);
  53. if(x != nullptr)
  54. {
  55. if(x->next == nullptr)
  56. L.tail = x->prev;
  57. if(x->prev != nullptr)
  58. x->prev->next = x->next;
  59. else
  60. L.head = x->next;
  61. if(x->next != nullptr)
  62. x->next->prev = x->prev;
  63. delete x;
  64. }
  65. }
  66.  
  67. void ListDispayForward(struct List L)
  68. {
  69. int counter = 0;
  70. struct Node *p = L.head;
  71. while(p != nullptr)
  72. {
  73. std::cout<<p->key<<" -> ";
  74. p = p->next;
  75. counter++;
  76. }
  77. std::cout<<"NULL \n";
  78. std::cout<<"Liczba wezlow listy : "<< counter <<'\n';
  79. }
  80.  
  81. void ListDispayBackward(struct List L)
  82. {
  83. int counter = 0;
  84. struct Node *p = L.tail;
  85. while(p != nullptr)
  86. {
  87. std::cout<<p->key<<" -> ";
  88. p = p->prev;
  89. counter++;
  90. }
  91. std::cout<<"NULL \n";
  92. std::cout<<"Liczba wezlow listy : "<< counter <<'\n';
  93. }
  94.  
  95.  
  96. void ListSplit(struct List L, struct List &L1,struct List &L2)
  97. {
  98. struct Node *p,*q;
  99. //Wyszukiwanie srodkowego wezła
  100. //Iterujemy wskaźniki z obu stron i gdy się spotkaja to oznacza ze znaleźlismy węzeł
  101. //Pętla while sprawdza dwa warunki dla list o parzystej i nieparzystej liczbie węzłów
  102. p = L.head;
  103. q = L.tail;
  104. while(p != q && p->next != q)
  105. {
  106. p = p->next;
  107. q = q->prev;
  108. }
  109. //Po znalezieniu węzła rozdzielamy listę za znalezionym węzłem
  110. if(p == nullptr && p->next == nullptr)
  111. {
  112. L1.head = L.head;
  113. L1.tail = L.tail;
  114. L2.head = nullptr;
  115. L2.tail = nullptr;
  116. }
  117. else
  118. {
  119. q = p->next;
  120. p->next = nullptr;
  121. q->prev = nullptr;
  122. L1.head = L.head;
  123. L1.tail = p;
  124. L2.head = q;
  125. L2.tail = L.tail;
  126. }
  127. }
  128.  
  129. void ListMerge(struct List &L, struct List L1,struct List L2)
  130. {
  131. struct Node *curr;
  132. //Sprawdzamy czy któraś z list jest pusta i zwracamy tę niepustą
  133. if(ListIsEmpty(L1))
  134. L.head = L2.head;
  135. else if(ListIsEmpty(L2))
  136. L.head = L1.head;
  137. else
  138. {
  139. // Ustawiamy głowę dla listy wynikowej
  140.  
  141. if(L2.head->key < L1.head->key)
  142. {
  143. L.head = L2.head;
  144. L.tail = L2.head;
  145. L2.head = L2.head->next;
  146. }
  147. else
  148. {
  149. L.head = L1.head;
  150. L.tail = L1.tail;
  151. L1.head = L1.head->next;
  152. }
  153. curr = L.head;
  154. L.head->prev = nullptr;
  155. // Iterujemy wskaźniki dopóki nie osiągniemy końca co najmniej jednej z list
  156. // dołączając do listy wynikowej węzeł o mniejszym lub równym kluczu
  157. while(L1.head != nullptr && L2.head != nullptr)
  158. {
  159. if(L2.head->key < L1.head->key)
  160. {
  161. curr->next = L2.head;
  162. L2.head->prev = curr;
  163. L2.head = L2.head->next;
  164. }
  165. else
  166. {
  167. curr->next = L1.head;
  168. L1.head->prev = curr;
  169. L1.head = L1.head->next;
  170. }
  171. curr = curr->next;
  172. }
  173. //Dołączenie reszty listy do listy wynikowej
  174. if(L1.head != nullptr)
  175. {
  176. curr->next = L1.head;
  177. L1.head->prev = curr;
  178. L.tail = L1.tail;
  179. }
  180. else
  181. {
  182. curr->next = L2.head;
  183. L2.head->prev = curr;
  184. L.tail = L2.tail;
  185. }
  186. }
  187. }
  188.  
  189. void ListMergeSort(struct List &L)
  190. {
  191. struct List h1,h2;
  192. if(L.head != nullptr && L.head->next != nullptr)
  193. {
  194. ListSplit(L,h1,h2);
  195. ListMergeSort(h1);
  196. ListMergeSort(h2);
  197. ListMerge(L,h1,h2);
  198. }
  199. }
  200.  
  201. int main()
  202. {
  203. int k,n,m,p;
  204. List L;
  205. ListInit(L);
  206. srand(time(nullptr));
  207. std::cout<<"Ile liczb wylosowac \n";
  208. std::cin>>n;
  209. std::cout<<"Podaj gorny zakres przedzialu dla losowanych liczb \n";
  210. std::cin>>m;
  211. for(k = 1;k <= n;k++)
  212. ListInsert(L,rand()%m);
  213. std::cout<<"Lista L \n";
  214. ListDispayForward(L);
  215. ListDispayBackward(L);
  216. ListMergeSort(L);
  217. std::cout<<"Lista L \n";
  218. ListDispayForward(L);
  219. ListDispayBackward(L);
  220. while(!ListIsEmpty(L))
  221. ListDelete(L,L.head->key);
  222. //system("PAUSE");
  223. return 0;
  224. }
  225.  
Success #stdin #stdout 0s 15240KB
stdin
50 1000
stdout
Ile liczb wylosowac 
Podaj gorny zakres przedzialu dla losowanych liczb 
Lista L 
911 -> 723 -> 546 -> 68 -> 215 -> 346 -> 969 -> 759 -> 41 -> 990 -> 620 -> 564 -> 837 -> 796 -> 950 -> 509 -> 465 -> 463 -> 80 -> 89 -> 49 -> 678 -> 330 -> 190 -> 775 -> 211 -> 656 -> 280 -> 887 -> 61 -> 218 -> 490 -> 508 -> 200 -> 747 -> 104 -> 953 -> 979 -> 138 -> 125 -> 800 -> 472 -> 261 -> 328 -> 331 -> 487 -> 429 -> 23 -> 61 -> 49 -> NULL 
Liczba wezlow listy : 50
49 -> 61 -> 23 -> 429 -> 487 -> 331 -> 328 -> 261 -> 472 -> 800 -> 125 -> 138 -> 979 -> 953 -> 104 -> 747 -> 200 -> 508 -> 490 -> 218 -> 61 -> 887 -> 280 -> 656 -> 211 -> 775 -> 190 -> 330 -> 678 -> 49 -> 89 -> 80 -> 463 -> 465 -> 509 -> 950 -> 796 -> 837 -> 564 -> 620 -> 990 -> 41 -> 759 -> 969 -> 346 -> 215 -> 68 -> 546 -> 723 -> 911 -> NULL 
Liczba wezlow listy : 50
Lista L 
23 -> 41 -> 49 -> 49 -> 61 -> 61 -> 68 -> 80 -> 89 -> 104 -> 125 -> 138 -> 190 -> 200 -> 211 -> 215 -> 218 -> 261 -> 280 -> 328 -> 330 -> 331 -> 346 -> 429 -> 463 -> 465 -> 472 -> 487 -> 490 -> 508 -> 509 -> 546 -> 564 -> 620 -> 656 -> 678 -> 723 -> 747 -> 759 -> 775 -> 796 -> 800 -> 837 -> 887 -> 911 -> 950 -> 953 -> 969 -> 979 -> 990 -> NULL 
Liczba wezlow listy : 50
990 -> 979 -> 969 -> 953 -> 950 -> 911 -> 887 -> 837 -> 800 -> 796 -> 775 -> 759 -> 747 -> 723 -> 678 -> 656 -> 620 -> 564 -> 546 -> 509 -> 508 -> 490 -> 487 -> 472 -> 465 -> 463 -> 429 -> 346 -> 331 -> 330 -> 328 -> 280 -> 261 -> 218 -> 215 -> 211 -> 200 -> 190 -> 138 -> 125 -> 104 -> 89 -> 80 -> 68 -> 61 -> 61 -> 49 -> 49 -> 41 -> 23 -> NULL 
Liczba wezlow listy : 50