Allora il codice serve per creare un albero binario, è già testato e funziona. Il problema da come ho capito con il debug è la stringa nome. Succede qualcosa di imprevisto quando la passo dal main alla funzione infatti crasha a puts. Aiutino? Non so che fare, mi sfugge qualcosa.
codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct Infof
{
char *nome;
char telefono[10];
};
struct albero
{
struct Infof info;
struct albero *left;
struct albero *right;
};
typedef struct albero bst;
bst *crea_nodo(char *name)
{
bst *nuovo = (bst *)malloc(sizeof(bst));
puts(name);
strcpy(nuovo->info.nome, &name);
printf("Inserisci numero di telefono: ");
scanf("%s",nuovo->info.telefono);
nuovo->left = NULL;
nuovo->right = NULL;
return nuovo;
}
bst *insert(bst *padre, char nom)
{
if(padre == NULL)
{
return crea_nodo(nom);
}
if(strcmp(padre->info.nome, &nom) < 0)
{
return insert(padre->left, nom);
}
else if (strcmp(padre->info.nome, &nom) > 0)
{
return insert(padre->right, nom);
}
return padre;
}
void preorder(bst *nodo)
{
if(nodo != NULL)
{
printf("Nome: %s\n", nodo->info.nome);
printf("Numero di telefono: ");
printf("%s",nodo->info.telefono);
puts("");
preorder(nodo->left);
preorder(nodo->right);
}
}
int main()
{
bst *head = NULL;
char nome[12];
printf("Inserisci nome: ");
gets(nome);
head = insert(head, &nome);
short int grado;
printf("Inserisci il grado della radice: ");
scanf("%hi", &grado);
grado = pow(2,grado)-2;
short int i;
for(i = 0; i < grado; i++)
{
printf("Inserisci nome: ");
scanf("%s", &nome);
insert(head, *nome);
}
preorder(head);
return 0;
}