Salve ragazzi, scrivo per avere un piccolo aiuto riguardo un esercizio di programmazione in C.
Sostanzialmente l'esercizio prevede la lettura di un file di testo e l'inserimento ordinato in un albero di ricerca. In tale file vi sono diversi parametri, del tipo nome, cognome e un intero. Devo inserire il tutto in un albero ordinato in base a giorno.
Ho pensato a questo codice
Mentre in fase di compilazione ottengo alcuni warning, in esecuzione ottengo un "Errore Di Segmentazione"codice:#include <stdio.h> #include <stdlib.h> #include <string.h> #define k 20 typedef struct{ char paziente[k]; char infermiera[k]; int giorno; }element; typedef struct item{ element value; struct item *left,*right; }nodo; typedef nodo *tree; int emptytree(){ return NULL; } int empty(tree t){ if(t==NULL) return 1; else return 0; } element root(tree t){ return t->value; } tree left(tree t){ return t->left; } tree right(tree t){ return t->right; } tree cons_tree(element e,tree left,tree right){ tree aux; aux=(tree)malloc(sizeof(nodo)); aux->value=e; aux->left=left; aux->right=right; return aux; } tree insord_tree(element e,tree t){ if(empty(t)) return cons_tree(e,NULL,NULL); else{ if(e.giorno<=root(t).giorno){ t->left=insord_tree(e,left(t)); return t; } else{ t->right=insord_tree(e,right(t)); return t; } } } tree carica_tree(FILE *f1,tree T){ element e; while(!feof(f1)){ fscanf(f1,"%s%s%d",e.paziente,e.infermiera,&e.giorno); T=insord_tree(e,T); } return T; } void stampa(tree t){ if(!empty(t)){ stampa(t->left); printf("%s, \t%s, \t%d\n",root(t).paziente,root(t).infermiera,root(t).giorno); stampa(t->right); } } main(){ element e; tree T=NULL; FILE *f1; f1=fopen("visite.txt","rt"); T=carica_tree(f1,T); stampa(T); fclose(f1); }
Secondo voi, dove o cosa sbaglio?

Rispondi quotando