Advertisement
danpalol

traccia 2 esercizio 1

Jun 29th, 2016
74
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. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. typedef struct nodo *Lista;
  6.  
  7. typedef struct nodo{
  8.     char nome[20];
  9.     char cognome [20];
  10.     Lista next;
  11. }nodo;
  12.  
  13. //prototipi funzione
  14.  
  15. Lista crealista();
  16. void stampalista(Lista L);
  17. void stampaPrimo(Lista L, char lettera);
  18.  
  19.  
  20.  
  21. int main ()
  22. {
  23.     Lista list=NULL;
  24.     char lettera;
  25.     list=crealista();
  26.     //stampalista(list);
  27.     printf("inserisci la lettera del nome\n");
  28.     scanf("%c", &lettera);
  29.     stampaPrimo(list, lettera);
  30.     return 0;
  31. }
  32.  
  33.  
  34. Lista crealista()
  35. {
  36.     nodo *N;
  37.     Lista list=NULL;
  38.     char buf[20];
  39.     while(1)
  40.         {
  41.         printf("\ninserisci nome ($ per terminare)\n");
  42.         gets(buf);
  43.         if(strcmp(buf,"$")==0) break;
  44.         N=malloc(sizeof(nodo));
  45.         strcpy(N->nome,buf);
  46.         printf("\ninserisci cognome\n");
  47.         gets(N->cognome);
  48.         N->next=list;
  49.         list=N;
  50.         }
  51.         return list;
  52. }
  53.  
  54.  
  55.     void stampaPrimo(Lista L, char lettera)
  56.     {
  57.         while(L)
  58.         {
  59.             if(L->nome[0]==lettera)
  60.             {
  61.                 printf("%s", L->cognome);
  62.                 break;
  63.             }
  64.             L=L->next;
  65.         }
  66.         if(!L) printf("nessun nome corrispondente");
  67.     }
  68.  
  69. void stampalista(Lista L)
  70. {
  71.     while(L)
  72.     {
  73.         printf("\n%s\n", L->nome);
  74.         printf("\n%s\n",L->cognome);
  75.         L=L->next;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement