Advertisement
Tankado95

Rimuovi primo dispari

May 7th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct el{
  5. int info;
  6. struct el* next;
  7. };
  8. typedef struct el ElementoDiLista;
  9. typedef struct el * ListaDiElementi;
  10.  
  11. void stampa(ListaDiElementi l){
  12.  
  13.     while(l->next!=NULL)
  14.     {
  15.         printf("%d\n", l->info);
  16.         l=l->next;
  17.     }
  18. }
  19.  
  20. /*Rimuove il primo elemento dispari della lista*/
  21. void rimuovi (ListaDiElementi *l){
  22. ListaDiElementi corr=*l, prec=NULL, save=NULL;
  23. int trovato=0;
  24.     while(corr->next!=NULL && trovato==0){
  25.         if (corr->info%2!=0){
  26.             save=prec;
  27.             trovato=1;
  28.         }
  29.         prec=corr;
  30.         corr=corr->next;
  31.     }
  32. if (save==NULL && trovato==1){
  33.     *l=(*l)->next;
  34.     }
  35. else if (trovato==1 && save!=NULL){
  36.     save->next=corr;
  37.     }
  38. }
  39.  
  40. int main(){
  41. ListaDiElementi l=malloc(sizeof(ElementoDiLista));
  42. ListaDiElementi corr=l;
  43. int x=7,n=0;
  44. int i=0;
  45.     while(i<10)
  46.     {
  47.         scanf("%d",&n);
  48.         corr->info=n;
  49.         corr->next=malloc(sizeof(ElementoDiLista));
  50.         corr=corr->next;
  51.        
  52.         i++;
  53.     }
  54.  
  55. corr->next=NULL;
  56. rimuovi (&l);
  57. stampa(l);
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement