// ESERCIZIO FINALE DI CALCOLATORI SULL'ORDINAMENTO DI LISTE. VERSIONE 2 // #include <stdio.h> #include <stdlib.h> #include <string.

h> struct city { char name[20]; int people; int extension; char region[20]; struct city *next; } *plist; struct city *createlist(struct city *plist); void printlist(struct city *plist); void option_a(); void option_b(); void option_c(); void option_d(); main( ) { printf("Inserire printf("'a' crea printf("'b' crea printf("'c' crea printf("'d' crea

operazione da svolgere:\n"); lista e stampa cosi' com'e'\n"); lista e stampa ordinando per abitanti\n"); lista e stampa ordinando per estensione\n"); lista e stampa ordinando per nome\n");

char choise = getchar(); switch (choise) { case 'a': { option_a(); } break; case 'b': { option_b(); } break; case 'c': { option_c(); } break; case 'd': { option_d(); } break; default: break; } while(getchar() != EOF) ; return 0; } struct city *createlist(struct city *plist)

plist->extension). char string[20]. "%d". &(pcity->next->name)). printf("Regione: %s\n\n". fscanf(pfile. &(plist->region)). . if (n <= 0) return NULL. &(pcity->next->people)). } } void option_a() { plist = createlist(plist). } return plist. } } void printlist(struct city *plist) { if(plist) { printf("%s\n". "%s". &(plist->extension)). fscanf(pfile. plist->people). fscanf(pfile. printf("Abitanti: %d\n". &(plist->people)). "%d". "%s". "%d". "%d". fscanf(pfile. "r"). fscanf(pfile. printf("Elenco citta':\n"). &(pcity->next->region)). fscanf(pfile. fscanf(pfile. "%s". printlist(plist). plist->next = NULL. pfile = fopen("city.txt". int n. struct city *pcity = plist. for(int i = 1. i++) { pcity->next = (struct city *)malloc(sizeof(struct city)). pcity->next->next = NULL. pcity = pcity->next. printf("Estensione: %d\n". &n). "%d". fscanf(pfile. fscanf(pfile. &(pcity->next->extension)). } void option_b() { plist = createlist(plist). printlist(plist->next). plist->region). "%s". i < n. plist->name). &(plist->name)). else { plist = (struct city *)malloc(sizeof(struct city)).{ FILE *pfile.

} struct city *ptemp = plist. int n = 0. ptemp->next->next = pcity. int n = 0. printlist(plist). pcity->next = plist->next. struct city *pcity = plist. pcity->next = ptemp->next->next. k > i + 1. i++) { if(plist->people < plist->next->people) { pcity = plist. } } } else if ( n == 1) { } else printf("error\n"). } if( n >= 2) { for( int i = 1. plist->next = pcity. pcity = pcity->next. plist = pcity->next. i < n. if(plist) n = 1. if(plist) n = 1. n). } ptemp = ptemp->next. printf("Elenco di %d citta':\n\n". k--) { if(ptemp->next->people < ptemp->next->next->people) { pcity = ptemp->next. ptemp->next = pcity->next. while(pcity->next) { . while(pcity->next) { n++. for( int k = n. } void option_c() { plist = createlist(plist).struct city *pcity = plist.

for( int k = n. pcity->next = ptemp->next->next. i++) { if(strcmp(plist->name. int n = 0. i < n. pcity = pcity->next. printf("Elenco di %d citta':\n\n". pcity = pcity->next. while(pcity->next) { n++. if(plist) n = 1. pcity->next = plist->next. } struct city *ptemp = plist. plist->next->name) > 0) . ptemp->next = pcity->next. i < n. i++) { if(plist->extension < plist->next->extension) { pcity = plist. printlist(plist). } if( n >= 2) { for( int i = 1. } } } else if ( n == 1) { } else printf("error\n"). } void option_d() { plist = createlist(plist). ptemp->next->next = pcity.n++. plist->next = pcity. plist = pcity->next. k--) { if(ptemp->next->extension < ptemp->next->next->extension) { pcity = ptemp->next. k > i + 1. } ptemp = ptemp->next. struct city *pcity = plist. n). } if( n >= 2) { for( int i = 1.

n). } struct city *ptemp = plist. k--) { if(strcmp(ptemp->next->name. plist->next = pcity. for( int k = n. pcity->next = plist->next. pcity->next = ptemp->next->next.{ pcity = plist. plist = pcity->next. ptemp->next = pcity->next. printf("Elenco di %d citta':\n\n". printlist(plist). ptemp->next->next = pcity. } ptemp = ptemp->next. } . } } } else if ( n == 1) { } else printf("error\n"). ptemp->next->next->name) > 0) { pcity = ptemp->next. k > i + 1.