Advertisement
Tankado95

Rimuovi l'ultimo elemento dispari della lista

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