Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Function: stampaPrimo
- * Usage: stampaPrimo (L, lettera)
- * Prototype: void stampaPrimo (lista L, char lettera);
- * ----------------------------------------------------------------------
- * Data una lista di elementi di tipo persona, scrivere la funzione stampaPrimo che,
- * preso in input un carattere, stampa, se c'Γ¨, il cognome della prima persona nella
- * lista L il cui nome inizia con lettera.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct persona *lista;
- typedef struct persona {
- char *nome;
- char *cognome;
- lista next;
- } persona;
- lista creaLista (void);
- void stampaPrimo (lista L, char lettera);
- int main (void) {
- lista L;
- char c;
- L = creaLista();
- printf("Dammi l'iniziale del nome: ");
- scanf(" %c", &c);
- stampaPrimo(L, c);
- return 0;
- }
- lista creaLista (void) {
- lista L = NULL;
- persona *P;
- char buffnome[20], buffcognome[20];
- while (1) {
- printf("Inserisci il prossimo nome e cognome ($ per terminare): ");
- scanf("%s %s", buffnome, buffcognome);
- if (!strcmp(buffnome, "$"))
- break;
- P = malloc(sizeof(persona));
- if (P == NULL)
- return L;
- P->nome = calloc(20,sizeof(char));
- P->cognome = calloc(20,sizeof(char));
- strcpy(P->nome, buffnome);
- strcpy(P->cognome, buffcognome);
- P->next = L;
- L = P;
- }
- return L;
- }
- void stampaPrimo (lista L, char lettera) {
- lista N = L;
- while (N != NULL) {
- if (lettera == N->nome[0]) {
- printf("%s\n", N->cognome);
- return;
- }
- else
- N = N->next;
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement