Correzioni al sorgente , non so se è il miglior modo per imparare , ma io faccio prima , e mi diverto , sta
a te capire , anche perchè magari posso farti un esempio , ma poi non riuscirei a spiegarlo bene a parole
codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 80
struct element {
char name[SIZE];
char surname[SIZE];
int matnumber;
struct element *next;
};
void menu();
void list(struct element *list_head);
struct element *insert(struct element *list_head);
//struct element *sortbyname(struct element *list_head);
//struct element *sortbymatnumber(struct element *list_head);
int main () {
struct element *list_start=NULL;
int comando;
do{
menu();
printf("\nComando: ");
scanf("%d",&comando);
while(getchar()!='\n');// Nel menu usi scanf() che lascia '\n' nel buffer fgets() legge '\n' e termina
switch(comando) {
case 1: {
list_start=insert(list_start);
break;
}
/* case 2: {
list_start=sortbyname(list_start);
break;
}
case 3: {
list_start=sortbymatnumber(list_start);
break;
}*/
case 4: {
list(list_start);
break;
}
}
// return 0; RETURN ESCE DAL MAIN dopo eseguito lo switch
} while(1);
}
struct element *insert(struct element *list_head){
struct element *newnode, *temp;
int x;
temp=list_head;
if (list_head!=NULL)
while(temp->next!=NULL)
temp=temp->next;
do {
newnode=(struct element*)malloc(sizeof(struct element));
/*if (newnode==NULL){
printf("ERROR");
EXIT(FAILURE_EXIT);
}*/
if(list_head==NULL) {
printf("Inserire nome:\n ");
fgets(newnode->name,SIZE,stdin);
printf("\nInserire cognome: ");
fgets(newnode->surname,SIZE,stdin);
printf("Inserire Numero matricola'");
scanf("%d",&newnode->matnumber);
while(getchar()!='\n');
newnode->next=NULL;
list_head=newnode;
temp=list_head; //temp=temp->next; Se la lista è vuota temp è NULL quindi fai un operazione
//non valida , se la lista è vuota temp è uguale alla testa appena creata primo nodo
} else {
printf("Inserire nome:\n ");
while(getchar()!='\n');
fgets(newnode->name,SIZE,stdin);
printf("\nInserire cognome: ");
fgets(newnode->surname,SIZE,stdin);
printf("Inserire Numero matricola'");
scanf("%d",&newnode->matnumber);
while(getchar()!='\n');
newnode->next=NULL;
temp->next=newnode;
temp=temp->next;
}
printf("Vuoi registrare un altro studente? (premere 1 per risposta affermativa)");
scanf("%d",&x);
} while (x==1);
return list_head;
}
void menu(){
printf("|||||||||||||||||||||");
printf("\n1)Registrare studente");
printf("\n4)Visualizza lista");
printf("|||||||||||||||||||||");
}
void list (struct element *list_head) {
/*nome e cognome sono stati acquisiti con fgets
quindi nella stringa nome e nella stringa cognome è presente il carattere '\n'
newline , perchè fgets lo considera un carattere valido e lo aggiunge alla stringa
se vuoi stampare correttamente i dati nella funzione inserimento devi toglire il
\n in questo modo
newnode->name[strlen(newnode->name)-1]=0; */
struct element *list_pointer =list_head;
printf("NOME COgnome N.Matricola \n");
while(list_pointer != NULL){
printf("%s %s %d\n", list_pointer->name,list_pointer->surname,list_pointer->matnumber);
list_pointer = list_pointer->next;
}
printf("\nEND\n") ;
}