Facebook
From Idiotic Bushbaby, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 194
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct element{
  5.         int k;
  6.         struct element *prev;
  7.         struct element *next;
  8. };
  9.  
  10. struct element *lista_dodaj(struct element *head, struct element *nowy);
  11.  
  12. void lista_wyswietl(struct element *head);
  13.  
  14. struct element *lista_szukaj(struct element *head,int k);
  15.  
  16. struct element* lista_usun(struct element *head, struct element *do_usu);
  17.  
  18. int main(){
  19.        
  20.         struct element *head = NULL,*nowy=NULL;
  21.         char z;
  22.         int liczba;
  23.         while(1){
  24.                 printf("\nco chcesz zrobic?");
  25.                 printf("\nd - dodać");
  26.                 printf("\ns - szukac");
  27.                 printf("\nu - usunac");
  28.                 printf("\no - odwrocic liste");
  29.                 printf("\nw - wyswietlic");
  30.                 printf("\nq - wyjsc\n");
  31.  
  32.                 fflush(stdin);
  33.                 z = getchar();
  34.  
  35.                 switch(z){
  36.                         case 'd': nowy=(struct element*) malloc (sizeof(struct element));
  37.                                 printf("\npodaj wartosc elelmentu do wstawienia: ");
  38.                                 fflush(stdin);
  39.                                 scanf("%d",&liczba);
  40.                                 nowy->k=liczba;
  41.                                 head = lista_dodaj(head,nowy);
  42.                                 break;
  43.  
  44.                         case 'w': lista_wyswietl(head);
  45.                                 break;
  46.  
  47.                         case 's':       printf("\npodaj wartosc do znalezienia: ");
  48.                                 fflush(stdin);
  49.                                 scanf("%d",&liczba);
  50.                                 nowy = lista_szukaj(head,liczba);
  51.                                 printf("\n%p \n",nowy);
  52.                                 break;
  53.  
  54.                         case 'u': printf("\npodaj wartosc do usuniecia: ");
  55.                                 fflush(stdin);
  56.                                 scanf("%d",&liczba);
  57.                                 nowy = lista_szukaj(head,liczba);
  58.                                 if (nowy == NULL){
  59.                                         printf("\n brak elementu do usuniecia");
  60.                                 }else{
  61.                                         head = lista_usun(head,nowy);
  62.                                         printf("\n element usnieto");
  63.                                 }
  64.                                 break;
  65.                         case 'q':  
  66.                                 return 0;
  67.  
  68.                 }
  69.         }
  70.  
  71.        
  72.         return 0;
  73. }
  74.  
  75. struct element *lista_dodaj(struct element *head, struct element *nowy){
  76.         nowy->prev=NULL;
  77.         nowy->next=head;
  78.         if(head!=NULL)
  79.                 head->prev=nowy;
  80.         head=nowy;
  81.         return head;
  82. }
  83.  
  84. void lista_wyswietl(struct element *head){
  85.         struct element *x = head;
  86.         while(x!=NULL){
  87.                 printf("%d ",x->k);
  88.                 x=x->next;
  89.         }
  90. }
  91.  
  92.  
  93. struct element *lista_szukaj(struct element *head,int sz){
  94.         struct element *x = head;
  95.         while((x!=NULL)&&(x->k!=sz)){
  96.                 x=x->next;
  97.         }
  98.         return x;
  99. }
  100.  
  101. struct element* lista_usun(struct element *head, struct element *do_usu){
  102.        
  103.         if(do_usu->prev != NULL){
  104.                 do_usu->prev->next = do_usu->next;
  105.         }
  106.         else{
  107.                 head = do_usu->next;
  108.         }
  109.         if(do_usu->next != NULL){
  110.                 do_usu->next->prev = do_usu->prev;
  111.         }
  112.  
  113.         return head;
  114. }