Buongiorno,
avrei bisogno che qualcuno mi chiarisse come gestire puntatori a strutture all'interno di altre strutture. Anche qualche chiarimento sulle strutture stesse è graditissimo.
Ho provato a costruire un albero con una struttura dati caratterizzata da un solo valore intero e funziona, appena provo a gestire una struttura non mi ci raccapezzo.
Questo è ciò che ho prodotto:
Graziecodice:struct tpunto { int valore; }; typedef struct tpunto punto; struct tnode { punto *ppunto; struct tnode *pleft; struct tnode *pright; }; typedef struct tnode nodo; struct tnode *addtree (nodo *p, punto *w) { if (p==NULL) { p = (nodo *)malloc(sizeof(struct tnode)); p->ppunto = w; p->pleft=p->pright=NULL; } else if (p->ppunto->valore <= w->valore) { p->pleft = addtree(p->pleft, w); } else { p->pright = addtree(p->pright,w); } return p; } void visita(nodo *p) { if (p != NULL) { printf("Valore = %d\n",p->ppunto->valore); visita(p->pleft); visita(p->pright); } } int stampa_albero(nodo *p, char *nome_file) { void stampa(nodo *p, int spacer, FILE *fileptr); int spacer = 0; FILE *file_ptr; file_ptr = fopen(nome_file,"w"); if (file_ptr == NULL) { return 0; } else { if (p != NULL) { stampa(p, spacer, file_ptr); fclose(file_ptr); } } return 1; } void stampa(nodo *p, int spacer, FILE *fileptr) { int spacerL = spacer; int spacerR = spacer; int i = 0; for (i; i < spacer; i++) { fputs("\t",fileptr); } if (p != NULL) { fprintf(fileptr,"Valore = %d \n",p->ppunto->valore); stampa(p->pleft, ++spacerL, fileptr); stampa(p->pright, ++spacerR, fileptr); } else fprintf(fileptr,"NODO - > NULL\n"); }

Rispondi quotando
