Advertisement
sildren12

listy laborki

Apr 30th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.47 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct element
  4. {
  5. int k;
  6. struct element*prev;
  7. struct element*next;
  8. };
  9. void lista_wyswietl(struct element *head);
  10. struct element *lista_dodaj(struct element *head, struct element *nowy);
  11. struct element *lista_szukaj(struct element *head, int sz);
  12. struct element *lista_odwroc(struct element *head);
  13. struct element *lista_usun(struct element *head, struct element *el);
  14. struct element *lista_zwolnij(struct element *head);
  15. main()
  16. {
  17. struct element *head=NULL, *nowy=NULL, *el=NULL;
  18. char z;
  19. int liczba;
  20. while(1)
  21. {
  22.     printf("\nco chcesz zrobic?");
  23.     printf("\nd - dodac");
  24.     printf("\ns - szukac");
  25.     printf("\nu - usunac");
  26.     printf("\no – odwrocic liste");
  27.     printf("\nw - wyswietlic");
  28.     printf("\nq - wyjsc\n");
  29.     fflush(stdin);
  30.     z=getchar();
  31. switch(z)
  32. {
  33.     case 'd':
  34.         nowy=(struct element*)malloc(sizeof(struct element));
  35.         printf("\npodaj wartosc elementu do wstawienia: ");
  36.         scanf("%d",&liczba);
  37.         nowy->k=liczba;
  38.         head=lista_dodaj(head,nowy);
  39.         break;
  40. case 'w': lista_wyswietl(head);
  41.         break;
  42. case 's':
  43.         if(head==NULL)
  44.                 printf("\nLista pusta");
  45.         printf("Podaj wartosc do wyszukania: ");
  46.         scanf("%d", &liczba);
  47.         el=lista_szukaj(head, liczba);
  48.                         if(el==NULL)
  49.                                 printf("\nNie znaleziono elementu");
  50.                         else
  51.                             printf("Znaleziono element: %d", el);
  52.                         break;
  53. case 'u':
  54.             if(head==NULL)
  55.                         printf("\nLista pusta");
  56.                         printf("Podaj wartosc do usuniecia: ");
  57.                         scanf("%d", &liczba);
  58.                         el=lista_szukaj(head, liczba);
  59.                         if(el==NULL)
  60.                                 printf("Nie znaleziono elementu");
  61.                         else
  62.                         {
  63.                                 head=lista_usun(head,el);
  64.                                 printf("\nElement poprawnie usuniety");
  65.                         }
  66.                         break;
  67. case 'o': head=lista_odwroc(head);
  68.         break;
  69. case 'q':
  70.     //head=lista_zwolnij(head);
  71.     return 0;
  72. }
  73. }
  74. }
  75. struct element *lista_dodaj(struct element *head, struct element *nowy)
  76. {
  77. nowy->prev=NULL;
  78. nowy->next=head;
  79. if(head!=NULL)
  80. head->prev=nowy;
  81. head=nowy;
  82. return head;
  83. }
  84. struct element *lista_szukaj(struct element *head, int sz)
  85. {
  86.         struct element *x=head;
  87.         while((x!=NULL) && (x->k!=sz))
  88.         {
  89.                 x=x->next;
  90.         }
  91.         return x;
  92. }
  93.  
  94. void lista_wyswietl(struct element *head)
  95. {
  96. struct element*x=head;
  97. while(x!=NULL)
  98. {
  99. printf("%d ",x->k);
  100. x=x->next;
  101. }
  102. }
  103.  
  104. struct element *lista_odwroc(struct element *head)
  105. {
  106.         struct element *LP=NULL, *x=head, *y=NULL;
  107.         while(x!=NULL)
  108.         {
  109.                 y=(struct element*)malloc(sizeof(struct element));
  110.                 y->k=x->k;
  111.                 LP=lista_dodaj(LP,y);
  112.                 x=x->next;
  113.         }
  114.         return LP;
  115. }
  116. struct element *lista_usun(struct element *head, struct element *x)
  117. {
  118.         if(x->prev!=NULL)
  119.                 x->prev->next=x->next;
  120.         else
  121.                 head=x->next;
  122.         if(x->next!=NULL)
  123.                 x->next->prev=x->prev;
  124.         free(x);
  125.         return head;
  126. }
  127.  
  128. /*struct element *lista_zwolnij(struct element *head)
  129. {
  130.         struct element *x;
  131.         while(head!=NULL);
  132.         {
  133.                 x=head->next;
  134.                 free(head);
  135.         head=x;
  136.         }    
  137.         return head;
  138. }*/
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement