Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 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, struct element* u)
  44. {
  45. if(u->prev!=NULL)
  46. u->prev->next=u->next;
  47. else
  48. head=u->next;
  49. if(u->next!=NULL)
  50. u->next->prev=u->prev;
  51. free(u);
  52. return head;
  53. }
  54.  
  55. struct element* lista_odwroc(struct element *head)
  56. {
  57. struct element *x=head;
  58. struct element *head2=NULL;
  59. struct element *y;
  60.  
  61. while(x!=NULL)
  62. {
  63. y=(struct element*)malloc(sizeof(struct element));
  64. y->k=x->k;
  65. head2=lista_dodaj(head2,y);
  66. x=x->next;
  67. }
  68. head=head2;
  69. return head;
  70. }
  71.  
  72. struct element * lista_zwolnij(struct element *head)
  73. {
  74.  
  75. struct element *nast;
  76.  
  77. while(head!=NULL)
  78. {
  79. nast=head->next;
  80. free(head);
  81. head=nast;
  82.  
  83. }
  84. return head;
  85. }
  86.  
  87. int main()
  88. {
  89. struct element *head=NULL, *nowy=NULL, *x=NULL;
  90. char z;
  91. int liczba;
  92. while(1)
  93. {
  94. system("cls");
  95. printf("Co chcesz zrobic?");
  96. printf("\nd - dodac");
  97. printf("\ns - szukac");
  98. printf("\nu - usunac");
  99. printf("\no - odwrocic liste");
  100. printf("\nw - wyswietlic");
  101. printf("\nq - wyjsc\n");
  102. fflush(stdin);
  103. z=getchar();
  104. switch(z)
  105. {
  106. case 'd':
  107. nowy=(struct element*)malloc(sizeof(struct element));
  108. printf("\nPodaj wartosc elementu do wstawienia: ");
  109. scanf("%d",&liczba);
  110. nowy->k=liczba;
  111. head=lista_dodaj(head,nowy);
  112. break;
  113. case 's':
  114. printf("Podaj liczbe do wyszukania: ");
  115. fflush(stdin);
  116. scanf("%d",&liczba);
  117. x=lista_szukaj(head,liczba);
  118. if(x==NULL)
  119. printf("Brak szukanego elementu.\n");
  120. else
  121. printf("Adres szukanego elementu:%p\n",x);
  122. getch();
  123. break;
  124. case 'u':
  125. printf("Podaj cyfre do usuniecia: ");
  126. scanf("%d",&liczba);
  127. struct element *u=NULL;
  128. u=lista_szukaj(head,liczba);
  129. while(u==NULL)
  130. {
  131. printf("Nie ma takiego elementu do usuniecia. Mozliwe elementy do usuniecia:\n");
  132. lista_wyswietl(head);
  133. printf("\nPodaj prawidlowy element do usuniecia:\n");
  134. scanf("%d",&liczba);
  135. u=lista_szukaj(head,liczba);
  136. }
  137. head=lista_usun(head,u);
  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