Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 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. struct element *lista_usun(struct element *head, struct element *x);
  13. struct element *lista_szukaj(struct element *head, int liczba);
  14. struct element *lista_dodaj(struct element *head, struct element *nowy);
  15. //struct element *lista_odwroc(struct element *head);
  16. //void zwolnij_pamiec(struct element *head);
  17. main()
  18. {
  19. struct element *head=NULL, *nowy=NULL;
  20. char z;
  21. int liczba;
  22. while(1)
  23. {
  24. printf("\nCo chcesz zrobic?");
  25. printf("\nd - dodac");
  26. printf("\ns - szukac");
  27. printf("\nu - usunac");
  28. printf("\no - odwrocic liste");
  29. printf("\nw - wyswietlic");
  30. printf("\nq - wyjsc\n");
  31. fflush(stdin);
  32. z=getchar();
  33. switch(z)
  34. {
  35. case 'd': case 'D':
  36. nowy = (struct element*)malloc(sizeof(struct element));
  37. printf("\nPodaj wartosc elementu do wstawienia: ");
  38. scanf("%d",&liczba);
  39. nowy->k=liczba;
  40. head = lista_dodaj(head, nowy);
  41. break;
  42. case 'w': case 'W':
  43. lista_wyswietl(head);
  44. break;
  45. case 's': case 'S':
  46. printf("\nPodaj wartosc elementu do znalezienia: ");
  47. scanf("%d",&liczba);
  48. nowy = lista_szukaj(head, liczba);
  49. if(nowy != NULL) printf("\nElement znaleziono pod adresem %p", nowy);
  50. else printf("\nNie znaleziono elementu");
  51. break;
  52. case 'u': case 'U':
  53. printf("\nPodaj wartosc elementu do usuniecia: ");
  54. scanf("%d",&liczba);
  55. nowy = lista_szukaj(head, liczba);
  56. if(nowy != NULL){
  57.  
  58. head = lista_usun(head, nowy);
  59. }
  60. else printf("\nNie usunieto elementu");
  61. break;
  62. case 'q': case 'Q':
  63. return 0;
  64. default:
  65. printf("\nBlad wyboru");
  66. break;
  67. }
  68. }
  69. }
  70.  
  71. void lista_wyswietl(struct element *head)
  72. {
  73. struct element *x=head;
  74. while(x!=NULL)
  75. {
  76. printf("%d ",x->k);
  77. x=x->next;
  78. }
  79. }
  80. struct element *lista_dodaj(struct element *head, struct element *nowy)
  81. {
  82. nowy->prev=NULL;
  83. nowy->next=head;
  84. if(head!=NULL) head->prev=nowy;
  85. head=nowy;
  86. return head;
  87. }
  88. struct element *lista_szukaj(struct element *head, int liczba)
  89. {
  90. struct element *x=head;
  91. while(x!=NULL && x->k!=liczba)
  92. {
  93. x=x->next;
  94. }
  95. return x;
  96. }
  97. struct element * lista_usun(struct element *head, struct element *x)
  98. {
  99. if(x->prev!=NULL) x->prev->next=x->next;
  100. else head=x->next;
  101. if(x->next!=NULL) x->next->prev=x->prev;
  102. free(x);
  103. return head;
  104. }
  105. /*void zwolnij_pamiec(struct element *head)
  106. {
  107. free(head);
  108. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement