Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <time.h>
  5. #include <windows.h>
  6.  
  7.  
  8. //ZAD 2. Zaimplementuj jednokierunkową listę. Dodj do listy 1000 losowych elementów. Zmierz średni czas dostępu do tego samego i losowego elementu.
  9. // Wytłumacz różnicę. Zaimplementuj funkcję megre(lista l1, lista l2) która połączy 2 listy.
  10.  
  11.  
  12. typedef struct Node
  13. {
  14. int el;
  15. struct Node *next;
  16. } Node;
  17.  
  18.  
  19. bool isEmpty(Node **head)
  20. {
  21. return ((*head) == NULL);
  22. }
  23.  
  24.  
  25. void enqueue(int x, Node **head) //wstaw w odpowiednie miejsce do listy
  26. {
  27. if(head == NULL) return;
  28.  
  29. if(isEmpty(head)) //pusta kolejka
  30. {
  31. Node *newNode = (Node*)malloc(sizeof(Node));
  32. newNode->el = x;
  33. newNode->next = NULL;
  34. (*head) = newNode;
  35. }
  36. else //wstawiamy do posortowanej listy
  37. {
  38. Node *newNode = (Node*)malloc(sizeof(Node));
  39. newNode->el = x;
  40. newNode->next = NULL;
  41.  
  42. Node *tmp = (*head);
  43.  
  44. if(x < tmp->el) //wstawiamy przed pierwszym elementem
  45. {
  46. newNode->next = tmp;
  47. (*head) = newNode;
  48. }
  49. else //wstawiamy po którymś z kolejnych elementów
  50. {
  51. while(tmp->next != NULL && tmp->next->el < x)
  52. tmp = tmp->next;
  53.  
  54. Node *nast = tmp->next;
  55.  
  56. tmp->next = newNode;
  57. newNode->next = nast;
  58. }
  59. }
  60. }
  61.  
  62. void showElements(Node **head)
  63. {
  64. printf("Kolejne elementy listy: ");
  65. if(head != NULL && (*head) != NULL)
  66. {
  67. Node *tmp = (*head);
  68. while(tmp != NULL)
  69. {
  70. printf("%d ", tmp->el);
  71. tmp = tmp->next;
  72. }
  73. printf("\n");
  74. }
  75. else
  76. printf("lista pusta.\n");
  77. }
  78.  
  79. void idzDo(int numer, Node **head)
  80. {
  81. if(head != NULL && (*head) != NULL)
  82. {
  83. Node *tmp = (*head);
  84. int i;
  85. for(i=0; i<numer; i++)
  86. {
  87. if(tmp->next != NULL)
  88. {
  89. tmp = tmp->next;
  90. }
  91. }
  92. //printf("Dotarlem do %d elementu\n", i);
  93. }
  94. else
  95. {
  96. printf("Lista pusta\n");
  97. }
  98. }
  99.  
  100. void deleteNode(int key, Node **head)
  101. {
  102. if(head == NULL || (*head) == NULL) return;
  103.  
  104. Node *ptr = (*head);
  105. Node *tmp;
  106. if(ptr->el == key) //jezeli już w głowie jest szukany klucz
  107. {
  108. while(ptr != NULL && ptr->el == key)
  109. {
  110. tmp = ptr;
  111. ptr = ptr->next;
  112. free(tmp);
  113. tmp = NULL;
  114. }
  115. (*head) = ptr; //jezeli pusta lista została to wskazuje na NULL, a jak nie to na kolejny rozny element
  116. }
  117. else // jezeli szukany klucz jest w innym elemencie niż głowa
  118. {
  119. while(ptr->next != NULL && ptr->next->el != key) //zatrzymuje się jeden element przed NULL'em albo przed szukanym elemencie
  120. { //jezeli przed NULLem to znaczy ze nic nie znalazl
  121. ptr = ptr->next;
  122. }
  123.  
  124. if(ptr->next == NULL) return; //nic nie znalezlismy
  125. Node *first = ptr;
  126. ptr = ptr->next;
  127.  
  128.  
  129.  
  130. while(ptr != NULL && (ptr->el) == key)
  131. {
  132. tmp = ptr;
  133. ptr = ptr->next;
  134. free(tmp);
  135. tmp = NULL;
  136. }
  137.  
  138. first->next = ptr;
  139. }
  140. }
  141.  
  142.  
  143.  
  144. void megre(Node **lista1, Node **lista2) //poczatek posortowanej listy jest w (*lista1)
  145. {
  146. if(lista1 == NULL || lista2 == NULL || ((*lista1) == NULL && (*lista2) == NULL) ) return; //łączenie nie ma sensu
  147.  
  148. if((*lista1) == NULL) //druga nie jest NULLem z górnego if'a
  149. {
  150. (*lista1) = (*lista2); //podczepiamy druga liste, jezeli druga jest nullem to zostaje jak jest
  151. (*lista2) = NULL;
  152. free(lista2); //not sure, ale chyba moge zwolnic miejsce na wskazanie na druga liste skoro je polaczylismy
  153. lista2 = NULL;
  154. }
  155. else //obie listy zawieraja posortowane elementy
  156. {
  157. Node *tmp1 = (*lista1);
  158. Node *tmp2 = (*lista2);
  159.  
  160. Node *wynik;
  161.  
  162. //najpierw ustalmy glowe nowej listy
  163. if(tmp1->el < tmp2->el)
  164. {
  165. tmp1 = tmp1->next; //lista1 wskazuje juz na poprawny pierwszy element
  166. wynik = (*lista1);
  167. }
  168. else
  169. {
  170. wynik = (*lista2);
  171. (*lista1) = tmp2;
  172. tmp2 = tmp2->next;
  173. }
  174.  
  175. while(tmp1 != NULL && tmp2 != NULL) //dopoki nie dojdziemy do konca którejś z list
  176. {
  177. if(tmp1->el < tmp2->el)
  178. {
  179. Node *first = tmp1;
  180.  
  181.  
  182. while(tmp1->next != NULL && tmp1->next->el < tmp2->el)
  183. tmp1 = tmp1->next;
  184.  
  185.  
  186. (*lista1)->next = first;
  187.  
  188. (*lista1) = tmp1;
  189. tmp1 = tmp1->next;
  190. }
  191. else
  192. {
  193. Node *first = tmp2;
  194.  
  195. while(tmp2->next != NULL && tmp2->next->el < tmp1->el)
  196. tmp2 = tmp2->next;
  197.  
  198.  
  199. (*lista1)->next = first;
  200. (*lista1) = tmp2;
  201. tmp2 = tmp2->next;
  202. }
  203. }
  204.  
  205. //teraz któraś z poprzednich list jest już pusta wiec wystarczy podczepić resztę drugiej
  206. if(tmp1 == NULL)
  207. (*lista1)->next = tmp2;
  208. else
  209. (*lista1)->next = tmp1;
  210.  
  211. (*lista1) = wynik;
  212.  
  213. (*lista2) = NULL;
  214. free(lista2);
  215. lista2 = NULL;
  216.  
  217. }
  218. }
  219.  
  220.  
  221. int main()
  222. {
  223. Node *lista1 = (Node*)malloc(sizeof(Node));
  224. Node *lista2 = (Node*)malloc(sizeof(Node));
  225. lista1 = NULL;
  226. lista2 = NULL;
  227.  
  228. printf("Czy lista jest pusta?: %s\n", isEmpty(&lista1) ? "tak" : "nie");
  229.  
  230. srand(time(NULL));
  231. int r;
  232. int i;
  233. /* for(i=0; i<10; i++)
  234. {
  235. r = rand();
  236. enqueue(r, &lista1);
  237. } */
  238.  
  239. enqueue(2, &lista1); enqueue(3, &lista1); enqueue(3, &lista1); enqueue(3, &lista1); enqueue(4, &lista1); enqueue(6, &lista1); enqueue(8, &lista1); enqueue(8, &lista1);
  240. for(i=0; i<10; i++)
  241. {
  242. r = rand();
  243. enqueue(r, &lista2);
  244. }
  245.  
  246. printf("Przed.\n");
  247. showElements(&lista1);
  248. //
  249. deleteNode(3, &lista1);
  250. showElements(&lista1);
  251.  
  252. deleteNode(8, &lista1);
  253. showElements(&lista1);
  254.  
  255. deleteNode(6, &lista1);
  256. showElements(&lista1);
  257.  
  258. deleteNode(2, &lista1);
  259. showElements(&lista1);
  260. //
  261. printf("Po.\n");
  262.  
  263. showElements(&lista2);
  264.  
  265. megre(&lista1, &lista2);
  266.  
  267. showElements(&lista1);
  268. showElements(&lista2);
  269.  
  270.  
  271. clock_t start_t, end_t;
  272. double total_t;
  273. int n;
  274. int ile = 10000;
  275. // start_t = GetTickCount();
  276. start_t = clock();
  277.  
  278. for(n=0; n<1000; n++)
  279. {
  280. idzDo(9000, &lista1);
  281. }
  282.  
  283.  
  284. // end_t = GetTickCount();
  285. end_t = clock();
  286.  
  287. total_t = (double)(end_t - start_t);
  288. printf("Sredni czas dostepu: %f\n", total_t);
  289.  
  290.  
  291. return 0;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement