Advertisement
Unisa05121

lista2.c

Apr 13th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. typedef struct nodo *lista;
  7. typedef struct nodo{
  8.     int val;
  9.     lista next;
  10. } nodo;
  11.  
  12. lista listaVuota();
  13. lista inserTesta(lista L,int el);
  14. lista cancTesta(lista L);
  15. lista inserCoda(lista L,int el);
  16. void stampaLista(lista L);
  17. lista leggiLista();
  18.  
  19. int main(void)
  20. {
  21.     lista L;
  22.     L=leggiLista();
  23.     stampaLista(L);
  24.     return 0;
  25. }
  26.  
  27. lista listaVuota()
  28. {
  29.     return NULL;
  30. }
  31.  
  32. lista inserTesta(lista L,int el)
  33. {
  34.     nodo *n;
  35.     n=malloc(sizeof(nodo));
  36.     if(n==NULL) return L; //CONTROLLO SE LO SPAZIO E' STATO EFFETTIVAMENTE ALLOCATO
  37.     n->val=el;
  38.     n->next=L;
  39.     return n;
  40. }
  41.  
  42. lista cancTesta(lista L)
  43. {
  44.     nodo *temp;
  45.     if(L==NULL) return L;
  46.     temp=L;
  47.     L=L->next;
  48.     free(temp);
  49.     return L;
  50. }
  51.  
  52. lista inserCoda(lista L,int el)
  53. {
  54.     nodo *n,*temp=L;
  55.     if(L==NULL) return inserTesta(L,el);
  56.     n=malloc(sizeof(nodo));
  57.     if(n==NULL) return NULL; //CONTROLLO SE LA MEMORIA E' STATA ALLOCATA
  58.     n->val=el;
  59.     n->next=NULL;
  60.     while(temp->next!=NULL) temp=temp->next;
  61.     temp->next=n;
  62.     return L;
  63. }
  64.  
  65. void stampaLista(lista L)
  66. {
  67.     printf("\nLa lista contiene gli elementi:\n");
  68.     while(L!=NULL)
  69.     {
  70.         printf("%d\n",L->val);  //oppure (*L).val
  71.         L=L->next;      //oppure (*L).next
  72.     }
  73.     printf("\n\n");
  74. }
  75.  
  76. lista leggiLista(lista L)
  77. {
  78.     lista temp;
  79.     L=listaVuota();
  80.     int val=1;
  81.     while(1)
  82.     {
  83.         printf("\nInserisci prossimo valore (0 per terminare): ");
  84.         scanf("%d",&val);
  85.         if(!val) return L;
  86.         temp=L;                             //--------
  87.         L=inserTesta(L,val);                        //temp=inserCoda(L,val);
  88.         if(temp==L)                         //if(temp==NULL)
  89.         {
  90.             printf("\nMEMORIA ESAURITA! INSERIMENTO TERMINATO.\n");
  91.             return L;
  92.         }
  93.                                         //L=temp;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement