Advertisement
Guest User

Untitled

a guest
May 20th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4. struct element
  5. {
  6. int k;
  7. struct element *prev;
  8. struct element *next;
  9. };
  10.  
  11. struct element *lista_dodaj(struct element *head,struct element *nowy);
  12. void lista_wyswietl(struct element*head);
  13. struct element *lista_szukaj(struct element *head,int x);
  14. struct element *lista_usun(struct element *head,struct element *x);
  15. struct element *lista_odwroc(struct element*head);
  16. struct element *lista_zwolnij(struct element *head);
  17. struct element *lista_min(struct element *head);
  18. struct element *lista_sort(struct element*head);
  19.  
  20. main()
  21. {
  22. struct element *head=NULL,*nowy=NULL,*e=NULL;
  23. char z;
  24. int liczba;
  25.  
  26. while(1)
  27. {
  28. printf("\nCo chcesz zrobic?");
  29. printf("\nd - dodac");
  30. printf("\ns - szukac");
  31. printf("\nu - usunac");
  32. printf("\no - odwrocic");
  33. printf("\nw - wyswietlic");
  34. printf("\nm - minimum");
  35. printf("\nv - sort");
  36. printf("\nq - wyjsc\n");
  37. fflush(stdin);
  38.  
  39. z = getch();
  40.  
  41. switch(z)
  42. {
  43.  
  44. case 'd':
  45. nowy=(struct element*)malloc(sizeof(struct element));
  46. printf("\nPodaj wartosc elementu:");
  47. scanf("%d",&liczba);
  48. nowy->k=liczba;
  49. head=lista_dodaj(head,nowy);
  50. break;
  51.  
  52. case 'w':
  53. printf("\n");
  54. lista_wyswietl(head);
  55. printf("\n");
  56. break;
  57. case 's':
  58. printf("Podaj wartosc szukana:");
  59. scanf("%d",&liczba);
  60. e=lista_szukaj(head,liczba);
  61. if(e!=NULL) printf("\n Szukany element znaleziony pod adresem 0x%x",e);
  62. else printf("\nBrak szukanego elementu\n");
  63. break;
  64. case 'u':
  65. printf("\nPodaj element do usuniecia");
  66. scanf("%d",&liczba);
  67.  
  68. e=lista_szukaj(head,liczba);
  69. if(e!=NULL)
  70. {
  71. head=lista_usun(head,e);
  72. lista_wyswietl(head);
  73. } else printf("\n brak danego elementu na liscie\n");
  74.  
  75. case 'o':
  76. head=lista_odwroc(head);
  77. printf("\nLista odwrocona");
  78. lista_wyswietl(head);
  79. break;
  80. case 'm':
  81. e=lista_min(head);
  82. if(e!=NULL)
  83. {
  84. printf("Minimum wynosi %d, adres: 0x%x",e->k,e);
  85. }else printf("Brak liczb");
  86. break;
  87. case 'v':
  88. head=lista_sort(head);
  89. lista_wyswietl(head);
  90. break;
  91. }
  92. }
  93. }
  94. struct element *lista_dodaj(struct element *head,struct element *nowy)
  95. {
  96. nowy->prev=NULL;
  97. nowy->next=head;
  98.  
  99. if(head!=NULL) head->prev=nowy;
  100. head=nowy;
  101. return head;
  102. }
  103.  
  104. void lista_wyswietl(struct element*head)
  105. {
  106. struct element *x=head;
  107. if(head==NULL)printf("lista pusta");
  108. while(x!=NULL)
  109. {
  110. printf("%d",x->k);
  111. x=x->next;
  112. }
  113. }
  114. struct element *lista_szukaj(struct element *head,int x)
  115. {
  116. struct element *e=head;
  117.  
  118. while(e!=NULL)
  119. {
  120. if(e->k==x) return e;
  121. e=e->next;
  122. }
  123. return NULL;
  124. }
  125.  
  126. struct element *lista_usun(struct element *head,struct element *x)
  127. {
  128. if(x->prev==NULL) head=x->next;
  129. else x->prev->next=x->next;
  130. if(x->next!=NULL) x->next->prev=x->prev;
  131.  
  132. //free(x);
  133.  
  134. return head;
  135. }
  136. struct element *lista_odwroc(struct element*head)
  137. {
  138. struct element *nowa=NULL,*x=head,*e=NULL;
  139.  
  140. while(x!=NULL)
  141. {
  142. e=(struct element*)malloc(sizeof(struct element));
  143. e->k=x->k;
  144.  
  145. nowa=lista_dodaj(nowa,e);
  146. x=x->next;
  147. }
  148. lista_zwolnij(head);
  149. return nowa;
  150. }
  151.  
  152. struct element *lista_zwolnij(struct element *head)
  153. {
  154. struct element *e=head;
  155.  
  156. while(head!=NULL)
  157. {
  158. e=head->next;
  159. free(head);
  160. head=e;
  161. }
  162. return head;
  163. }
  164. struct element *lista_min(struct element *head)
  165. {
  166. struct element *x=head,*min;
  167. min=(struct element*)malloc(sizeof(struct element));
  168. if (head==NULL)
  169. return NULL;
  170. min->k=x->k;
  171. while(x!=NULL)
  172. {
  173. if(min->k > x->k)
  174. {
  175. min->k=x->k;
  176. } else x=x->next;
  177. }
  178. return min;
  179. }
  180. struct element *lista_sort(struct element*head)
  181. {
  182. struct element*nowy,*min,*x=head;
  183. min=(struct element*)malloc(sizeof(struct element));
  184.  
  185. while(x!=NULL)
  186. {
  187. min=lista_min(x);
  188. lista_usun(x,min);
  189. nowy=(struct element*)malloc(sizeof(struct element));
  190. lista_dodaj(nowy,min);
  191. x=x->next;
  192. }
  193. lista_odwroc(nowy);
  194. return nowy;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement