Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct element {
  5. int k;
  6. struct element *next;
  7. struct element *prev;
  8. };
  9.  
  10. void lista_wyswietl(struct element *head);
  11. struct element *insert(struct element *head, int key, int new);
  12. struct element *swap(struct element *head, int k,int n);
  13. struct element *lista_zwolnij_pamiec(struct element *head);
  14. struct element *lista_odwroc(struct element *head);
  15. struct element *lista_szukaj(struct element *head, int klucz);
  16. struct element *lista_usun1(struct element *head, int klucz);
  17. struct element *lista_usun2(struct element *head,struct element *x);
  18. struct element *lista_usun3(struct element *head);
  19. struct element *lista_dodaj1(struct element *head, struct element *nowy);
  20. struct element *lista_dodaj2(struct element *head, struct element *nowy);
  21. struct element *lista_sortuj(struct element *head);
  22. int lista_policz(struct element*head);
  23. int lista_pusta(struct element*head);
  24. int iitl(struct element *head, int key);
  25.  
  26. main()
  27. {
  28. struct element *head = NULL, *nowy = NULL;
  29. char z;
  30. int liczba,liczba2,i=0;
  31. while (1)
  32. {
  33. if (i == 0) {
  34. printf("\nco chcesz zrobic?");
  35. printf("\nd - dodac");
  36. printf("\ns - szukac");
  37. printf("\nu - usunac");
  38. printf("\no – odwrocic liste");
  39. printf("\nw - wyswietlic");
  40. printf("\nk - dodaj na koniec");
  41. printf("\nq - wyjsc");
  42. printf("\ni - zamienic elementy");
  43. printf("\nz - zwolnic pamiec");
  44. printf("\nl - usun glowe");
  45. printf("\np - dlugosc listy");
  46. printf("\nc - sprawdzic czy element nalezy do listy");
  47. printf("\nb - zamienic element");
  48. printf("\nn - posortowac liste");
  49. printf("\n");
  50. }
  51. fflush(stdin);
  52. z = getchar();
  53. switch (z)
  54. {
  55. case 'd':
  56. nowy = (struct element*)malloc(sizeof(struct element));
  57. //printf("\npodaj wartosc elementu do wstawienia: ");
  58. scanf_s("%d", &liczba);
  59. nowy->k = liczba;
  60. head = lista_dodaj1(head, nowy);
  61. break;
  62. case 's':
  63. printf("jaki element szukac?\n");
  64. fflush(stdin);
  65. scanf_s("%d", &liczba);
  66. //(lista_szukaj(head,liczba)!=0) ? printf("%d", lista_szukaj(head,liczba)) : printf("nie ma takiego elementu\n");
  67. printf("%p", lista_szukaj(head, liczba));
  68. break;
  69. case 'w':
  70. if (!lista_pusta(head)) {
  71. lista_wyswietl(head);
  72. printf("\n");
  73. }
  74. else
  75. printf("lista pusta");
  76. break;
  77. case 'i':
  78. printf("\njakie elementy zamienic?\n");
  79. scanf_s("%d", &liczba);
  80. scanf_s("%d", &liczba2);
  81. head = swap(head, liczba,liczba2);
  82. lista_wyswietl(head);
  83. break;
  84. case'k':
  85. printf("Jaki element chcesz dodac?\n");
  86. fflush(stdin);
  87. scanf_s("%d", &liczba);
  88. nowy = (struct element*)malloc(sizeof(struct element));
  89. nowy->k = liczba;
  90. lista_dodaj2(head, nowy);
  91. break;
  92. case 'u':
  93. printf("Jaki element chcesz usunac?\n");
  94. fflush(stdin);
  95. scanf_s("%d", &liczba);
  96. lista_usun1(head, liczba);
  97. break;
  98. case'l':
  99. head=lista_usun3(head);
  100. break;
  101. case 'o':
  102. head = lista_odwroc(head);
  103. lista_wyswietl(head);
  104. break;
  105. case 'z':
  106. head=lista_zwolnij_pamiec(head);
  107. break;
  108. case 'p':
  109. printf("\n liczba elementow w liscie: %d \n", lista_policz(head));
  110. break;
  111. case 'c':
  112. printf("\nJaki element sprawdzic?");
  113. scanf_s("%d", &liczba);
  114. printf("\n%d\n", iitl(head, liczba));
  115. break;
  116. case'b':
  117. printf("\nJaki element zamienic? ");
  118. scanf_s("%d", &liczba);
  119. printf("Na jaki podmienic? ");
  120. fflush(stdin);
  121. scanf_s("%d", &liczba2);
  122. head = insert(head, liczba, liczba2);
  123. lista_wyswietl(head);
  124. break;
  125. case'n':
  126. head = lista_sortuj(head);
  127. lista_wyswietl(head);
  128. break;
  129. case 'q': return 0;
  130. }
  131. i++;
  132.  
  133. }
  134. }
  135.  
  136. void lista_wyswietl(struct element *head)
  137. {
  138. struct element*x = head;
  139. while (x != NULL)
  140. {
  141. printf("%d ", x->k);
  142. x = x->next;
  143. }
  144. }
  145.  
  146. struct element *lista_szukaj(struct element *head, int klucz) {
  147. struct element *x = head;
  148. while (x != NULL && x->k != klucz) {
  149. x = x->next;
  150. }
  151. return x;
  152. }
  153. struct element *lista_usun1(struct element *head, int klucz) {
  154. struct element *x = lista_szukaj(head, klucz);
  155. if (x->prev != NULL)
  156. x->prev->next = x->next;
  157. else
  158. head = x->next;
  159.  
  160. if (x->next != NULL)
  161. x->next->prev = x->prev;
  162.  
  163. free(x);
  164. return head;
  165. }
  166.  
  167. struct element *lista_usun2(struct element *head, struct element *x) {
  168. if (x->prev != NULL)
  169. x->prev->next = x->next;
  170. else
  171. head = x->next;
  172.  
  173. if (x->next != NULL)
  174. x->next->prev = x->prev;
  175.  
  176. free(x);
  177.  
  178. return head;
  179. }
  180.  
  181.  
  182. struct element *lista_dodaj1(struct element *head, struct element *nowy) {
  183. nowy->next = head;
  184. if (head != NULL) {
  185. head->prev = nowy;
  186. }
  187. head = nowy;
  188. nowy->prev = NULL;
  189. return head;
  190. }
  191.  
  192. struct element *lista_dodaj2(struct element *head, struct element *nowy) {
  193. struct element *x = head;
  194. while (x->next != NULL)
  195. x = x->next;
  196. x->next = nowy;
  197. nowy->next = NULL;
  198. nowy->prev = x;
  199. return head;
  200. }
  201.  
  202. struct element *lista_odwroc(struct element *head){
  203. struct element *x = head;
  204. struct element *temp = NULL;
  205. while (x->next != NULL) {
  206. temp = x->prev;
  207. x->prev = x->next;
  208. x->next = temp;
  209. x = x->prev;
  210. }
  211. x->next = x->prev;
  212. x->prev = NULL;
  213. return x;
  214. }
  215.  
  216. int lista_policz(struct element*head) {
  217. int i = 0;
  218. struct element *x = head;
  219. while (x != NULL) {
  220. x = x->next;
  221. i++;
  222. }
  223. return i;
  224. }
  225.  
  226. struct element *lista_usun3(struct element *head) {
  227. struct element*x = head;
  228. if (x->next == NULL){
  229. free(head);
  230. return NULL;
  231. }
  232. x->next->prev = NULL;
  233. x = x->next;
  234. free(head);
  235. head = x;
  236. return head;
  237.  
  238. }
  239.  
  240. struct element* lista_zwolnij_pamiec(struct element *head){
  241.  
  242. while (!lista_pusta(head)) {
  243.  
  244. head=lista_usun3(head);
  245. }
  246. return head;
  247. }
  248.  
  249.  
  250. int lista_pusta(struct element *head) {
  251. if (head == NULL)
  252. return 1;
  253. return 0;
  254. }
  255. struct element *swap(struct element *head,int k,int n) {
  256. struct element *w1 = lista_szukaj(head, k);
  257. struct element *w2 = lista_szukaj(head, n);
  258. int temp;
  259. temp = w1->k;
  260. w1->k = w2->k;
  261. w2->k = temp;
  262. return head;
  263. }
  264.  
  265. int iitl(struct element *head, int key) {
  266. struct element *x = head;
  267. int bol=0;
  268. while (x != NULL) {
  269. if (key == x->k) bol = 1;
  270. x = x->next;
  271. }
  272. return bol;
  273. }
  274. struct element *insert(struct element *head, int key, int new) {
  275. struct element *x = head;
  276. if (iitl(head, key)) {
  277. while (x != NULL && key!=x->k) {
  278. x = x->next;
  279. }
  280. x->k = new;
  281. }
  282. return head;
  283. }
  284.  
  285. struct element *lista_sortuj(struct element *head) {
  286. int i = lista_policz(head);
  287. int temp;
  288. struct element *x = head;
  289.  
  290. do{
  291. while (x->next!= NULL) {
  292. if (x->k > x->next->k) {
  293. temp = x->k;
  294. x->k = x->next->k;
  295. x->next->k = temp;
  296. }
  297. x = x->next;
  298. }
  299. i = i - 1;
  300. x = head;
  301. } while (i > 1);
  302. return head;
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement