bartek27210

lab_4

May 2nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.29 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. void lista_wyswietl(struct element *head);
  11. struct element *lista_dodaj(struct element *head, struct element *nowy);
  12. struct element *lista_szukaj(struct element *head, int szukana);
  13. struct element *lista_usun(struct element *head, struct element *do_usuniecia);
  14. struct element *lista_odwroc(struct element *head);
  15.  
  16. int main()
  17. {
  18.  struct element *head=NULL, *nowy=NULL;
  19.  char z;
  20.  int liczba;
  21.  
  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':   nowy=(struct element*)malloc(sizeof(struct element));
  36.                                 printf("\npodaj wartosc elementu do wstawienia: ");
  37.                                 scanf("%d",&liczba);
  38.                                 nowy->k=liczba;
  39.                                 head=lista_dodaj(head,nowy);
  40.                                 break;
  41.                 case 'w':   lista_wyswietl(head);
  42.                                 break;
  43.                 case 's':  if (head==NULL)
  44.                                     printf("\nLista jest pusta");
  45.                                     else
  46.                                         {
  47.                                             printf("\nWpisz wartosc szukana:");
  48.                                             scanf("%d", &liczba);
  49.                                             nowy=lista_szukaj(head, liczba);
  50.                                                 if (nowy != NULL)
  51.                                                     printf("\nznaleziono pod adresem %d", nowy);
  52.                                                 else
  53.                                                     printf("\nnie znaleziono");
  54.                                         }
  55.                                  break;
  56.                 case 'u': if (head==NULL)
  57.                                     printf("\nLista jest pusta");
  58.                                 else
  59.                                     {
  60.                                         printf("\nWpisz wartosc do usuniecia:");
  61.                                         scanf("%d",&liczba);
  62.                                         if(lista_szukaj(head,liczba)==NULL)
  63.                                             printf("nie znaleziono takiego elementu");
  64.                                         else
  65.                                             lista_usun(head,lista_szukaj(head,liczba) );
  66.                                     }
  67.                                     break;
  68.                 case 'o': if(head==NULL)
  69.                                     printf("\nLista jest pusta");
  70.                                 else head=lista_odwroc(head);
  71.                                 break;
  72.  
  73.  
  74.                 case 'q': return 0;
  75.         }
  76.     }
  77. }
  78. void lista_wyswietl(struct element *head)
  79. {
  80.     struct element*x=head;
  81.     while(x!=NULL)
  82.         {
  83.         printf("%d ",x->k);
  84.         x=x->next;
  85.         }
  86. }
  87.  
  88. struct element *lista_dodaj(struct element *head, struct element *nowy)
  89. {
  90.     nowy->prev=NULL;
  91.     nowy->next=head;
  92.     if(head!=NULL)
  93.         head->prev=nowy;
  94.     head=nowy;
  95.     return head;
  96.  
  97. }
  98.  
  99. struct element *lista_szukaj(struct element *head, int szukana)
  100. {
  101.     struct element *x=head;
  102.  
  103.     while((x!=NULL) && (x->k!=szukana))
  104.     {
  105.         x=x->next;
  106.     }
  107.     return x;
  108. }
  109.  
  110. struct element *lista_usun(struct element *head, struct element *do_usuniecia)
  111. {
  112.     if(do_usuniecia->prev!=NULL)
  113.         do_usuniecia->prev->next=do_usuniecia->next;
  114.     else head=do_usuniecia->next;
  115.     if(do_usuniecia->next!=NULL)
  116.         do_usuniecia->next->prev=do_usuniecia->prev;
  117.     free(do_usuniecia);
  118.     return head;
  119. }
  120.  
  121. struct element *lista_odwroc(struct element *head)
  122. {
  123.     struct element *lp=NULL;
  124.     struct element *x=head;
  125.     struct element *y=NULL;
  126.  
  127.     while(x!=NULL)
  128.         {
  129.             y=(struct element*)malloc(sizeof(struct element) );
  130.             y->k=x->k;
  131.             lp=lista_dodaj(lp,y);
  132.             x=x->next;
  133.         }
  134.     return lp;
  135. }
Add Comment
Please, Sign In to add comment