Advertisement
Simone_99

lista.c

Dec 7th, 2019
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.21 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct elemento {
  5.     int valore;
  6.     struct elemento *next;
  7. };
  8.  
  9. void printList(struct elemento *lis){
  10.     printf("Lista: \n");
  11.     while(lis!=NULL){
  12.         printf("%d\n",lis->valore);
  13.         lis=lis->next;
  14.     }
  15.     printf("------\n\n");
  16.     return;
  17. }
  18.  
  19. void append(struct elemento **lis, int val){
  20.     struct elemento *e = malloc(sizeof(struct elemento));
  21.     e->valore = val;
  22.     e->next = NULL;
  23.    
  24.     if((*lis)==NULL){
  25.         (*lis) = e;
  26.         return;
  27.     }
  28.    
  29.     if((*lis)->next == NULL){
  30.         (*lis)->next = e;
  31.         return;
  32.     }
  33.    
  34.     struct elemento *t = *lis;
  35.     while(t->next!=NULL){
  36.         t=t->next;
  37.         if(t->next==NULL){ 
  38.             t->next = e;
  39.             return;
  40.         }
  41.     }
  42. }
  43.  
  44. void deleteFirst(struct elemento **lis, int val){
  45.     if(*lis==NULL){
  46.         return;
  47.     }
  48.     struct elemento *l;
  49.     l = *lis;
  50.     struct elemento *l2;
  51.     l2 = *lis;
  52.     while(l!=NULL){
  53.         if(l==(*lis) && l->valore==val){
  54.             struct elemento *el;
  55.             el=(*lis)->next;
  56.             free(*lis);
  57.             (*lis)=el;
  58.             return;
  59.         } else if(l->valore == val){
  60.             struct elemento *el;
  61.             el=l->next;
  62.             free(l);
  63.             l2->next = el;
  64.             return;
  65.         }
  66.         if(l!=*lis){
  67.             l2=l2->next;
  68.         }
  69.         l=l->next;
  70.     }
  71.     return;
  72. }
  73.  
  74. void deleteAll(struct elemento **lis, int val){
  75.     if(*lis==NULL){
  76.         return;
  77.     }
  78.     struct elemento *l;
  79.     l = *lis;
  80.     struct elemento *l2;
  81.     l2 = *lis;
  82.     while(l!=NULL){
  83.         if(l==(*lis) && l->valore==val){
  84.             struct elemento *el;
  85.             el=(*lis)->next;
  86.             free(*lis);
  87.             (*lis)=el;
  88.             l2=el;
  89.             l=el;
  90.             continue;
  91.         } else if(l->valore == val){
  92.             struct elemento *el;
  93.             el=l->next;
  94.             free(l);
  95.             l2->next = el;
  96.             l=l2->next;
  97.             continue;
  98.         }
  99.         if(l!=*lis){
  100.             l2=l2->next;
  101.         }
  102.         l=l->next;
  103.     }
  104.     return;
  105. }
  106.  
  107. int main(){
  108.    
  109.     // creo una lista
  110.     struct elemento *lis = NULL;
  111.    
  112.     append(&lis,12);
  113.     append(&lis,2);
  114.     append(&lis,12);
  115.     append(&lis,12);
  116.     printList(lis);
  117.     deleteFirst(&lis,12);
  118.     deleteFirst(&lis,10);
  119.     printf("\ndeleteFirst(&lis,12)\ndeleteFirst(&lis,10)\n");
  120.     printList(lis);
  121.     append(&lis,22);
  122.     append(&lis,12);
  123.     printf("\nappend(&lis,22)\nappend(&lis,12)\n");
  124.     printList(lis);
  125.     deleteAll(&lis,10);
  126.     printf("\ndeleteAll(&lis,10)\n");
  127.     printList(lis);
  128.     deleteAll(&lis,12);
  129.     printf("\ndeleteAll(&lis,12)\n");
  130.     printList(lis);
  131.    
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement