Advertisement
Unisa05121

inversione Lista

Apr 15th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. typedef struct nodo *lista;
  4. typedef struct nodo{
  5.     int val;
  6.     lista succ;
  7. } nodo;
  8.  
  9. lista insCoda(lista L,int el)
  10. {
  11.     nodo *n,*temp=L;
  12.     n=malloc(sizeof(nodo));
  13.     if(n==NULL) return L;
  14.     n->val=el;
  15.     n->succ=NULL;
  16.     if(L==NULL) return n;
  17.     while(temp->succ!=NULL) temp=temp->succ;
  18.     temp->succ=n;
  19.     return L;
  20. }
  21.  
  22. lista leggiLista(lista L)
  23. {
  24.     int val;
  25.     char a[2];
  26.     while(1)
  27.     {
  28.         printf("\nInserisci elemento: ");
  29.         scanf("%d",&val);
  30.         L=insCoda(L,val);
  31.         printf("\nInserire un altro elemento? (n per fermarti): ");
  32.         scanf("%s",a);
  33.         if(a[0]=='n') break;
  34.     }
  35.     return L;
  36. }
  37.  
  38. void stampaLista(lista L)
  39. {
  40.     printf("INIZIO ");
  41.     while(L!=NULL)
  42.     {
  43.         printf("%d -> ",L->val);
  44.         L=L->succ;
  45.     }
  46.     printf("FINE\n");
  47. }
  48.  
  49. lista insTesta(lista L,int el)
  50. {
  51.     nodo *n;
  52.     n=malloc(sizeof(nodo));
  53.     if(n==NULL) return NULL;
  54.     n->val=el;
  55.     if(L==NULL)
  56.     {
  57.         n->succ=NULL;
  58.         return n;
  59.     }
  60.     n->succ=L;
  61.     return n;
  62. }
  63.  
  64. lista invLista(lista L)
  65. {
  66.     lista M=NULL;
  67.     while(L!=NULL)
  68.     {
  69.         M=insTesta(M,L->val);
  70.         L=L->succ;
  71.     }
  72.     return M;
  73. }
  74.  
  75. int main(void)
  76. {
  77.     lista L=NULL;
  78.     L=leggiLista(L);
  79.     stampaLista(L);
  80.     L=invLista(L);
  81.     stampaLista(L);
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement