PDA

View Full Version : problemino di C...


marcus81
15-02-2002, 12:32
Dunque ho implementato in c la seguente funzione:

int cancella(NODO **root, RECORD buffer)
{ int x, y;
NODO *paus, *paus2;
if(*root==NULL)
return 0;
else{
x=(strcmp(buffer.cognome, (*root)->info.cognome));
if(x<0)
return( cancella(&((*root)->sx), buffer));
if(x>0)
return( cancella(&((*root)->dx), buffer));
if(x==0)
{ y=(strcmp(buffer.nome, (*root)->info.nome));
if(y<0)
return( cancella(&((*root)->sx), buffer));

if(y>0)
return( cancella(&((*root)->dx), buffer));

else /* è stato trovato il punto da eliminare*/
{ /*analisi dei vari casi*/
/*se *root non ha figli*/
if((((*root)->dx)==NULL)&&(((*root)->sx)==NULL))
free(*root);

/*se *root ha il solo figlio sinistro*/
if((*root)->dx==NULL)
{ paus=*root;
*root=(*root)->sx;
free(paus);
}
/*se *root ha il solo figlio destro*/

if((*root)->sx==NULL)
{ paus=*root;
*root=(*root)->dx;
free(paus);
}

/*se invece ha tutti e due i figli*/
else{
if((((*root)->sx)!=NULL)&&(((*root)->dx)!=NULL))
{ paus=*root;
paus2=(*root)->sx;
*root=(*root)->dx;
free(paus);
if((*root)->sx==NULL)
{ (*root)->sx=paus2;
free(paus2);
}
else
{ while((*root)->sx!=NULL)
{ ((*root)->sx)=((*root)->sx);/*scorre in avanti il puntatore*/
}
(*root)->sx=paus2;
free(paus2);
}
}
}
}
}
}

return 1;
}

Essa opera in un programmino che simula una rubrica telefonica e serve a cancellarne una voce.
Al momento della compilazione del programma è tutto ok...ma nell'esecuzione di fatto c'è qualcosa che non va infatti mi dice "segmentantion fault" ed esce dal programma...
Vi sarei molto grato se mi aiutaste a risolvere il problema.Grazie.

ilsensine
15-02-2002, 13:27
int cancella(NODO **root, RECORD buffer)
{ int x, y;
NODO *paus, *paus2;
if(*root==NULL)
return 0;
else{
x=(strcmp(buffer.cognome, (*root)->info.cognome));
if(x<0)
return( cancella(&((*root)->sx), buffer));
if(x>0)
return( cancella(&((*root)->dx), buffer));
if(x==0)
{ y=(strcmp(buffer.nome, (*root)->info.nome));
if(y<0)
return( cancella(&((*root)->sx), buffer));

if(y>0)
return( cancella(&((*root)->dx), buffer));

else /* è stato trovato il punto da eliminare*/
{ /*analisi dei vari casi*/
/*se *root non ha figli*/
if((((*root)->dx)==NULL)&&(((*root)->sx)==NULL))
{
free(*root);
*root = NULL;
return 1;
}


/*se *root ha il solo figlio sinistro*/
if((*root)->dx==NULL)
{ paus=*root;
*root=(*root)->sx;
free(paus);
return 1;
}
/*se *root ha il solo figlio destro*/

if((*root)->sx==NULL)
{ paus=*root;
*root=(*root)->dx;
free(paus);
return 1;
}

/*se invece ha tutti e due i figli*/
else{
if((((*root)->sx)!=NULL)&&(((*root)->dx)!=NULL))
{ paus=*root;
paus2=(*root)->sx;
*root=(*root)->dx;
free(paus);
if((*root)->sx==NULL)
{ (*root)->sx=paus2;
free(paus2); // ???????
}
else
{ while((*root)->sx!=NULL)
{ ((*root)->sx)=((*root)->sx);/*scorre in avanti il puntatore*/
}
(*root)->sx=paus2;
free(paus2); // ??????????
}
}
}
}
}
}

return 1;
}

marcus81
15-02-2002, 14:30
perkè hai messo quei punti interrogativi???

ilsensine
15-02-2002, 14:35
Rimuovi quelle righe orrende :D

Ti metto la pulce nell'orecchio:
Questo
(*root)->sx=paus2;
free(paus2)
E' equivalente a questo
(*root)->sx=paus2;
free((*root)->sx)

marcus81
15-02-2002, 14:54
Madonna ke caz*ata che avevo scritto!
Forse ultimamente ho fatto troppo abuso di C...:D
Thank you, very much!;)