Advertisement
Unisa05121

Liste

Apr 8th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. #include <stdio.h>
  2. typedef struct nodo *lista;
  3. typedef struct nodo{int dato;lista next;} nodo;
  4.  
  5. /*lista creaLista(lista *N)         //crea una nuova lista
  6. {
  7.     N->next=NULL;
  8.     return N
  9. }*/
  10.  
  11. void inserisci(lista L,nodo *N,nodo *P) //inserisce N dopo P
  12. {
  13.     N->next=P->next;
  14.     P->next=N;
  15. }
  16.  
  17. void cancella(lista L,nodo *P)      //cancella l'elemento dopo P
  18. {
  19.     nodo *temp;
  20.     temp=P->next;
  21.     if(temp==NULL) return;
  22.     P->next=temp->next;
  23.     free(temp);
  24. }
  25.  
  26. lista inserTesta(lista L,nodo *N)   //N diventa il primo elemento della lista
  27. {
  28.     N->next=L;
  29.     return N;
  30. }
  31.  
  32. lista cancTesta(lista L)        //cancella il primo elemento della lista
  33. {
  34.     nodo *temp;
  35.     temp=L->next;
  36.     if(!temp) return L;
  37.     free(L);
  38.     return temp;
  39. }
  40.  
  41. lista  creaLista()
  42. {
  43.     lista L=NULL;
  44.     int val;
  45.     nodo *N;
  46.     while(1)
  47.     {
  48.         printf("\nInserisci un altro valore (0 per terminare): ");
  49.         scanf("%d",&val);
  50.         if(!val) break;
  51.         N=malloc(sizeof(nodo));
  52.         N->dato=val;
  53.         N->next=L;
  54.         L=N;
  55.     }
  56.     return L;
  57. }
  58.  
  59. void stampaLista(lista L)
  60. {
  61.     printf("\nI valori inseriti sono:\n");
  62.     while(L!=NULL)
  63.     {
  64.         printf("%d\n",L->dato);
  65.         L=L->next;
  66.     }
  67. }
  68.  
  69. int main(void)
  70. {
  71.     lista L;
  72.     int ris;
  73.     L=creaLista();
  74.     printf("\n");
  75.     stampaLista(L);
  76.     printf("\n");
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement