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 Node *x,struct List L,struct List &L1,struct List &L2)
  97. {
  98. struct Node *inputHead;
  99. struct Node *nextX;
  100. if(x == nullptr || x->next == nullptr)
  101. {
  102. L1.head = L.head;
  103. L1.tail = L.tail;
  104. L2.head = nullptr;
  105. L2.tail = nullptr;
  106. }
  107. else
  108. {
  109. inputHead = L.head;
  110. nextX = x->next;
  111. x->next = nullptr;
  112. nextX->prev = nullptr;
  113. L1.head = inputHead;
  114. L1.tail = x;
  115. L2.head = nextX;
  116. L2.tail = L.tail;
  117. }
  118. }
  119.  
  120.  
  121. int main()
  122. {
  123. int k,n,m,p;
  124. List L,L1,L2;
  125. Node *x;
  126. ListInit(L);
  127. ListInit(L1);
  128. ListInit(L2);
  129. srand(time(nullptr));
  130. std::cout<<"Ile liczb wylosowac \n";
  131. std::cin>>n;
  132. std::cout<<"Podaj gorny zakres przedzialu dla losowanych liczb \n";
  133. std::cin>>m;
  134. for(k = 1;k <= n;k++)
  135. ListInsert(L,rand()%m);
  136. std::cout<<"Lista L \n";
  137. ListDispayForward(L);
  138. ListDispayBackward(L);
  139. std::cout<<"Podaj element za ktorym chcesz rozdzielic liste \n";
  140. std::cin>>p;
  141. x = ListSearch(L,p);
  142. ListSplit(x,L,L1,L2);
  143. std::cout<<"Lista L1 \n";
  144. ListDispayForward(L1);
  145. ListDispayBackward(L1);
  146. std::cout<<"Lista L2 \n";
  147. ListDispayForward(L2);
  148. ListDispayBackward(L2);
  149. std::cout<<"Lista L \n";
  150. ListDispayForward(L);
  151. ListDispayBackward(L);
  152. while(!ListIsEmpty(L1))
  153. ListDelete(L1,L1.head->key);
  154. while(!ListIsEmpty(L2))
  155. ListDelete(L2,L2.head->key);
  156. std::cout<<"Lista L1 \n";
  157. ListDispayForward(L1);
  158. ListDispayBackward(L1);
  159. std::cout<<"Lista L2 \n";
  160. ListDispayForward(L2);
  161. ListDispayBackward(L2);
  162. std::cout<<"Lista L \n";
  163. ListDispayForward(L);
  164. ListDispayBackward(L);
  165. system("PAUSE");
  166. return 0;
  167. }
  168.  
Success #stdin #stdout #stderr 0s 15240KB
stdin
20 100
50
stdout
Ile liczb wylosowac 
Podaj gorny zakres przedzialu dla losowanych liczb 
Lista L 
64 -> 43 -> 7 -> 9 -> 70 -> 53 -> 54 -> 87 -> 30 -> 38 -> 45 -> 62 -> 72 -> 79 -> 83 -> 12 -> 21 -> 75 -> 38 -> 2 -> NULL 
Liczba wezlow listy : 20
2 -> 38 -> 75 -> 21 -> 12 -> 83 -> 79 -> 72 -> 62 -> 45 -> 38 -> 30 -> 87 -> 54 -> 53 -> 70 -> 9 -> 7 -> 43 -> 64 -> NULL 
Liczba wezlow listy : 20
Podaj element za ktorym chcesz rozdzielic liste 
Lista L1 
64 -> 43 -> 7 -> 9 -> 70 -> 53 -> 54 -> 87 -> 30 -> 38 -> 45 -> 62 -> 72 -> 79 -> 83 -> 12 -> 21 -> 75 -> 38 -> 2 -> NULL 
Liczba wezlow listy : 20
2 -> 38 -> 75 -> 21 -> 12 -> 83 -> 79 -> 72 -> 62 -> 45 -> 38 -> 30 -> 87 -> 54 -> 53 -> 70 -> 9 -> 7 -> 43 -> 64 -> NULL 
Liczba wezlow listy : 20
Lista L2 
NULL 
Liczba wezlow listy : 0
NULL 
Liczba wezlow listy : 0
Lista L 
64 -> 43 -> 7 -> 9 -> 70 -> 53 -> 54 -> 87 -> 30 -> 38 -> 45 -> 62 -> 72 -> 79 -> 83 -> 12 -> 21 -> 75 -> 38 -> 2 -> NULL 
Liczba wezlow listy : 20
2 -> 38 -> 75 -> 21 -> 12 -> 83 -> 79 -> 72 -> 62 -> 45 -> 38 -> 30 -> 87 -> 54 -> 53 -> 70 -> 9 -> 7 -> 43 -> 64 -> NULL 
Liczba wezlow listy : 20
Lista L1 
NULL 
Liczba wezlow listy : 0
NULL 
Liczba wezlow listy : 0
Lista L2 
NULL 
Liczba wezlow listy : 0
NULL 
Liczba wezlow listy : 0
Lista L 
0 -> 1928289936 -> 1928289904 -> 1928289872 -> 1928289840 -> 1928289808 -> 1928289776 -> 1928289744 -> 1928289712 -> 1928289680 -> 1928289648 -> 1928289616 -> 1928289584 -> 1928289552 -> 1928289520 -> 1928289488 -> 1928289456 -> 1928289424 -> 1928289392 -> 1928289360 -> NULL 
Liczba wezlow listy : 20
1928289360 -> NULL 
Liczba wezlow listy : 1
stderr
sh: 1: PAUSE: not found