Sayukoo

[LISTY] Usuwanie struktur list

Dec 17th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5.  
  6. struct osoba{
  7.     struct osoba *next;
  8.     int wiek;
  9.     int wzrost;
  10. };
  11.  
  12. void wypisz(struct osoba*lista)
  13. {
  14.      struct osoba*pom=lista;
  15.  
  16.      while(pom!=NULL)
  17.      {
  18.          printf("%d\n ",pom->wiek);
  19.          printf("%d\n ",pom->wzrost);
  20.          pom=pom->next;
  21.      }
  22.  
  23. }
  24.  
  25. void kasuj(struct osoba**lista, int x)
  26. {
  27.     struct osoba*pom=*lista;
  28.     struct osoba*pop;
  29.     if(pom->wiek==x)
  30.     {
  31.         *lista=(*lista)->next;
  32.         free(pom);
  33.     }
  34.     else{
  35.         pop=pom;
  36.         pom=pom->next;
  37.         while(pom!=NULL)
  38.         {
  39.             if(pom->wiek==x)
  40.             {
  41.                 pop->next=pom->next;
  42.                 free(pom);
  43.                 break;
  44.             }
  45.             pop=pom;
  46.             pom=pom->next;
  47.         }
  48.     }
  49.  
  50.  
  51. }
  52. int main()
  53. {
  54.     struct osoba *pocz;
  55.  
  56.     pocz=(struct osoba*)malloc(sizeof(struct osoba));
  57.  
  58.     pocz->wiek=5;
  59.     pocz->wzrost=190;
  60.  
  61.     struct osoba *pom;
  62.     pom=(struct osoba*)malloc(sizeof(struct osoba));
  63.     pocz->next=pom;
  64.     pom->wiek=6;
  65.     pom->wzrost=160;
  66.  
  67.     struct osoba *trzeci;
  68.     trzeci=(struct osoba*)malloc(sizeof(struct osoba));
  69.     pom->next=trzeci;
  70.     trzeci->wiek=7;
  71.     trzeci->wzrost=123;
  72.     trzeci->next=NULL;
  73.  
  74.     wypisz(pocz);
  75.     kasuj(&pocz,6);
  76.  
  77.  
  78.     return 0;
  79. }
Add Comment
Please, Sign In to add comment