#include #include #include /* abbastanza grande da contenere un nominativo */ #define MAX_STR 100 typedef struct nome { char *s; struct nome *prox; } nome_t; void InserisciLista(nome_t **lista, char *nome); int TrovaLista(nome_t *lista, char *nome); int main(int argc, char *argv[]) { FILE *f; char prima[30] = "prima.txt"; char dopo[30] = "dopo.txt"; nome_t *lista = NULL, *p; char s[MAX_STR]; int cont, FINE; f = fopen(prima, "rt"); if (f == NULL) { printf("ERRORE: problema apertura in lettura (%s)\n", prima); return -1; } FINE = 0; while (!feof(f)) { FINE = (fgets(s, MAX_STR, f) == NULL); if (!FINE && !feof(f)) { /* printf("%s\n", s); */ InserisciLista(&lista, s); } } fclose(f); f = fopen(dopo, "rt"); if (f == NULL) { printf("ERRORE: problema apertura in lettura (%s)\n", prima); return -1; } FINE = 0; cont = 0; while (!feof(f)) { FINE = (fgets(s, MAX_STR, f) == NULL); if (!FINE && !feof(f)) { /* printf("%s\n", s); */ if (TrovaLista(lista, s) == 1) { cont++; } } } fclose(f); printf("%d\n", cont); return 0; } void InserisciLista(nome_t **lista, char *s) { /* inserisce un elemento nella lista */ nome_t *p; /* inserisce il nuovo valore in testa alla lista */ /* la ricerca sarebbe piu' rapida se l'inserimento fosse ordinato, ma dati i numeri in gioco non ne vale la pena */ p = malloc(sizeof(nome_t)); if (p == NULL) { printf("Errore allocamento\n"); exit(-1); } p->s = malloc(sizeof(char)*strlen(s+1)); if (p->s == NULL) { printf("Errore allocamento\n"); exit(-1); } strcpy(p->s, s); p->prox = *lista; *lista = p; } int TrovaLista(nome_t *lista, char *s) { /* cerca un elemento nella lista */ /* restituisce 1 se la stringa s e' presente, 0 altrimenti */ nome_t *p; p = lista; while (p != NULL) { if (strcmp(s, p->s) == 0) { return 1; } p = p->prox; } return 0; }