Unisa05121

Lista di stringhe

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