Advertisement
Nello96

ListeECognomi

May 5th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. /* Function:  stampaPrimo
  2.  * Usage:     stampaPrimo (L, lettera)
  3.  * Prototype: void stampaPrimo (lista L, char lettera);
  4.  * ----------------------------------------------------------------------
  5.  * Data una lista di elementi di tipo persona, scrivere la funzione stampaPrimo che,
  6.  * preso in input un carattere, stampa, se c'Γ¨, il cognome della prima persona nella
  7.  * lista L il cui nome inizia con lettera.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. typedef struct persona *lista;
  15. typedef struct persona {
  16.     char *nome;
  17.     char *cognome;
  18.     lista next;
  19. } persona;
  20.  
  21. lista creaLista (void);
  22. void stampaPrimo (lista L, char lettera);
  23.  
  24. int main (void) {
  25.     lista L;
  26.     char c;
  27.     L = creaLista();
  28.     printf("Dammi l'iniziale del nome: ");
  29.     scanf(" %c", &c);
  30.     stampaPrimo(L, c);
  31.     return 0;
  32. }
  33.  
  34. lista creaLista (void) {
  35.     lista L = NULL;
  36.     persona *P;
  37.     char buffnome[20], buffcognome[20];
  38.     while (1) {
  39.         printf("Inserisci il prossimo nome e cognome ($ per terminare): ");
  40.         scanf("%s %s", buffnome, buffcognome);
  41.         if (!strcmp(buffnome, "$"))
  42.             break;
  43.         P = malloc(sizeof(persona));
  44.         if (P == NULL)
  45.             return L;          
  46.         P->nome = calloc(20,sizeof(char));
  47.         P->cognome = calloc(20,sizeof(char));
  48.         strcpy(P->nome, buffnome);
  49.         strcpy(P->cognome, buffcognome);
  50.         P->next = L;
  51.         L = P;
  52.     }
  53.     return L;
  54. }
  55.  
  56. void stampaPrimo (lista L, char lettera) {
  57.     lista N = L;
  58.     while (N != NULL) {
  59.         if (lettera == N->nome[0]) {
  60.             printf("%s\n", N->cognome);
  61.             return;
  62.         }
  63.         else
  64.             N = N->next;
  65.     }
  66.     return;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement