Salve a tutti, ho una strutturi BST definita nel modo seguente:
codice:
struct SKey{ //CHIAVE
char codice[11];
};
typedef struct SKey TKey;
struct SSat { //SATELLITE
char razza[16];
float peso;
int croccantini;
};
typedef struct SSat TSat;
struct SInfo{ //INFO
TKey key;
TSat satellite;
};
typedef struct SInfo TInfo;
struct SNode { //NODO
TInfo info;
struct SNode *left;
struct SNode *right;
};
typedef struct SNode TNode;
typedef TNode* TTree;
Devo scrivere una funzione che riceve in ingresso l'albero e un array di stringhe e restiuisca l'array riempito con tutte le razze ( ripetute una volta sola ) e il numero delle razze. Il prototipo deve essere il seguente :
codice:
int crea_elenco_razze(TTree tree,char elenco [][MAX])
e la funzione che ho scritto io è:
codice:
int crea_elenco_razze(TTree tree,char elenco [][MAX])
{
if(tree==NULL)
return 0;
else
{
int l,r,c;
if(tree->right!=NULL && tree->left!=NULL)
{
l=crea_elenco_razze(tree->left,elenco);
r=crea_elenco_razze(tree->right,elenco);
c=aggiungi(tree->info.satellite.razza, elenco,l+r);
return c;
}
else
{
if(tree->right!=NULL)
{
r=crea_elenco_razze(tree->right,elenco);
c=aggiungi(tree->info.satellite.razza, elenco,r);
return c;
}
else
{
if(tree->left!=NULL)
{
l=crea_elenco_razze(tree->left,elenco);
c=aggiungi(tree->info.satellite.razza,elenco,l);
return c;
}
else
{
c=aggiungi(tree->info.satellite.razza,elenco,0);
return c;
}
}
}
}
}
dove
codice:
int aggiungi(char razza[], char elenco [][MAX], int dim)
{
int i;
for(i=0;i<dim;i++)
{
if(strcmp(razza,elenco[i])==0)
return dim;
}
strcpy(elenco[i],razza);
return dim+1;
}
purtroppo nell'elenco razze c'è una riga vuota, e quindi il numero di razze risulta essere più grande dell'effettivo numero di 1. Qualcuno può darmi una mano?
La chiamata nel main è:
codice:
case 2:
n_razze=crea_elenco_razze(tree,elenco);
printf("Il numero delle razze presenti e': %d \n",n_razze);
printf("L'elenco delle razze e' il seguente\n");
stampa_elenco(elenco,n_razze);