Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct element
  5. {
  6. int k;
  7. struct element *prev;
  8. struct element *next;
  9. };
  10.  
  11. void lista_wyswietl(struct element *head)
  12. {
  13. struct element*x=head;
  14. while(x!=NULL)
  15. {
  16. printf("%d ",x->k);
  17. x=x->next;
  18. }
  19. }
  20.  
  21.  
  22. struct element *lista_dodaj(struct element *head, struct element *nowy)
  23. {
  24. nowy->prev=NULL;
  25. nowy->next=head;
  26. if(head!=NULL)
  27. head->prev=nowy;
  28. head=nowy;
  29. return head;
  30. }
  31.  
  32. struct element *lista_szukaj(struct element *head, int liczba)
  33. {
  34. struct element *s=head;
  35.  
  36. while(s!=NULL && (s->k)!=liczba)
  37. {
  38. s=s->next;
  39. }
  40. return s;
  41. }
  42.  
  43. struct element* lista_usun(struct element *head, int liczba)
  44. {
  45. struct element *u=NULL;
  46. u=lista_szukaj(head,liczba);
  47. while(u==NULL)
  48. {
  49. printf("Nie ma takiego elementu do usuniecia. Mozliwe elementy do usuniecia:\n");
  50. lista_wyswietl(head);
  51. printf("\nPodaj prawidlowy element do usuniecia:\n");
  52. scanf("%d",&liczba);
  53. u=lista_szukaj(head,liczba);
  54. }
  55. if(u->prev!=NULL)
  56. u->prev->next=u->next;
  57. else
  58. head=u->next;
  59. if(u->next!=NULL)
  60. u->next->prev=u->prev;
  61. free(u);
  62. return head;
  63. }
  64.  
  65. struct element* lista_odwroc(struct element *head)
  66. {
  67. struct element *x=head;
  68. struct element *head2=NULL;
  69. struct element *y;
  70.  
  71. while(x!=NULL)
  72. {
  73. y=(struct element*)malloc(sizeof(struct element));
  74. y->k=x->k;
  75. head2=lista_dodaj(head2,y);
  76. x=x->next;
  77. }
  78. head=head2;
  79. return head;
  80. }
  81.  
  82. struct element * lista_zwolnij(struct element *head)
  83. {
  84.  
  85. struct element *nast;
  86.  
  87. while(head!=NULL)
  88. {
  89. nast=head->next;
  90. free(head);
  91. head=nast;
  92.  
  93. }
  94. return head;
  95. }
  96.  
  97. int main()
  98. {
  99. struct element *head=NULL, *nowy=NULL, *x=NULL;
  100. char z;
  101. int liczba;
  102. while(1)
  103. {
  104. system("cls");
  105. printf("Co chcesz zrobic?");
  106. printf("\nd - dodac");
  107. printf("\ns - szukac");
  108. printf("\nu - usunac");
  109. printf("\no - odwrocic liste");
  110. printf("\nw - wyswietlic");
  111. printf("\nq - wyjsc\n");
  112. fflush(stdin);
  113. z=getchar();
  114. switch(z)
  115. {
  116. case 'd':
  117. nowy=(struct element*)malloc(sizeof(struct element));
  118. printf("\nPodaj wartosc elementu do wstawienia: ");
  119. scanf("%d",&liczba);
  120. nowy->k=liczba;
  121. head=lista_dodaj(head,nowy);
  122. break;
  123. case 's':
  124. printf("Podaj liczbe do wyszukania: ");
  125. fflush(stdin);
  126. scanf("%d",&liczba);
  127. x=lista_szukaj(head,liczba);
  128. if(x==NULL)
  129. printf("Brak szukanego elementu.\n");
  130. else
  131. printf("Adres szukanego elementu:%p\n",x);
  132. getch();
  133. break;
  134. case 'u':
  135. printf("Podaj cyfre do usuniecia: ");
  136. scanf("%d",&liczba);
  137. head=lista_usun(head,liczba);
  138. printf("Element zostal usuniety.\n");
  139. getch();
  140. break;
  141. case 'o':
  142. head=lista_odwroc(head);
  143. printf("Lista zostala odwrocona.\n");
  144. getch();
  145. break;
  146. case 'w':
  147. lista_wyswietl(head);
  148. getch();
  149. break;
  150. case 'q':
  151. head=lista_zwolnij(head);
  152. return 0;
  153. default :
  154. printf("Podano zly znak. Podaj poprawny.");
  155. getch();
  156. break;
  157. }
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement