Advertisement
Unisa05121

Lista di interi

May 24th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. typedef struct nodo *lista;
  5. typedef struct nodo{
  6.     int val;
  7.     lista next;
  8. } nodo;
  9.  
  10. lista insCoda(lista L,int val)
  11. {
  12.     nodo *n,*tmp=L;
  13.     n=malloc(sizeof(nodo));
  14.     if(!n)
  15.     {
  16.         printf("MEMORIA ESAURITA!");
  17.         return L;
  18.     }
  19.     n->val=val;
  20.     n->next=NULL;
  21.     while(tmp->next!=NULL) tmp=tmp->next;
  22.     tmp->next=n;
  23.     return L;
  24. }
  25.  
  26. lista leggiLista(lista L)
  27. {
  28.     int n,val;
  29.     printf("\nQuanti valori vuoi inserire?");
  30.     scanf("%d",&n);
  31.     while(n--)
  32.     {
  33.         printf("\nInserisci il valore: ");
  34.         scanf("%d",&val);
  35.         if(!L)
  36.         {   L=malloc(sizeof(nodo));
  37.             if(!L) return NULL;
  38.             L->val=val;
  39.             L->next=NULL;
  40.         }
  41.         else L=insCoda(L,val);
  42.     }
  43.     return L;
  44. }
  45.  
  46. void stampaLista(lista L)
  47. {
  48.     while(L)
  49.     {
  50.         printf("\n%d",L->val);
  51.         L=L->next;
  52.     }
  53. }
  54.  
  55. int main(void)
  56. {
  57.     lista L=NULL;
  58.     L=leggiLista(L);
  59.     stampaLista(L);
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement