PDA

View Full Version : [C] - Liste - ricorsione di coda


gabmac2
13-03-2010, 21:13
Avrei questa lista e sul testo mi dice che farla cosė č pių semplice ripetto alla tail ,mi potete dare una motivazione?
node *somma(node *l1,node *l2){
node *p;int s=0;
if((l1==NULL) && (l2==NULL)) return NULL;
else if((l1!=NULL) && (l2==NULL)){
return somma(l1->next,l2);}

else if((l1==NULL) && (l2!=NULL)){
return somma(l1,l2->next);}

else if((l1!=NULL) && (l2!=NULL)){
if(l1->data==l2->data){
p=newnode();
p->data=l1->data;
p->next=somma(l1->next,l2->next);
return p;}

if(l1->data<l2->data){
return somma(l1->next,l2);}

if(l1->data>l2->data){
return somma(l1,l2->next);}
}
}

Grazie mille in anticipo!

Kenger
13-03-2010, 22:49
Puoi riscrivere il codice usanto il tag CODE e indentando per favore? E' illeggibile cosė.

bobbytre
14-03-2010, 18:16
Avrei questa lista e sul testo mi dice che farla cosė č pių semplice ripetto alla tail ,mi potete dare una motivazione?
node *somma(node *l1,node *l2){
node *p;int s=0;
if((l1==NULL) && (l2==NULL)) return NULL;
else if((l1!=NULL) && (l2==NULL)){
return somma(l1->next,l2);}

else if((l1==NULL) && (l2!=NULL)){
return somma(l1,l2->next);}

else if((l1!=NULL) && (l2!=NULL)){
if(l1->data==l2->data){
p=newnode();
p->data=l1->data;
p->next=somma(l1->next,l2->next);
return p;}

if(l1->data<l2->data){
return somma(l1->next,l2);}

if(l1->data>l2->data){
return somma(l1,l2->next);}
}
}

Grazie mille in anticipo!


dovresti essere un po piu chiaro ,

qui hai postato il codice di una funzione chiamata somma ,

e vorresti sapere cosa ?

gabmac2
14-03-2010, 19:34
questa funzione l' ho chiamata "somma",la funzione duplica i valori uguali in 2 liste.non č di coda,ma perchč in un caso del genere una ricorsione non di coda č da preferire a una di coda?