Ciao a tutti,
il mio problema è che non riesco a scrivere in C una versione dell'algoritmo Merge Sort che lavori su una lista concatenata bidirezionale (doubly linked list).
Le strutture dati che uso sono:
Mentre il codice che ho scritto per il Merge Sort è:codice:struct nodo { char *parola; struct nodo *prec; struct nodo *next; }; struct lista { struct nodo *testa; struct nodo *coda; };
Credo che l'errore sia nel Merge, comunque sapreste indicarmi cosa sbaglio?codice:struct nodo *Split(struct nodo *n1, struct nodo *padre) { struct nodo *n2 = NULL; if((n1 && n1->next) && (n1!=n1->next)) { n2 = n1->next; n2->prec = n1->prec; n1->prec = padre; n1->next = n2->next; n2->next = Split(n1->next,n1); } return n2; } struct nodo *Merge(struct nodo *n1,struct nodo *n2) { if(n1 == NULL) return n2; else if (n2 == NULL) return n1; else if(strcmp(n1->parola,n2->parola) < 0) { n2->prec = n1; n1->next = Merge(n1->next,n2); return n1; } else { n1->prec = n2; n2->next = Merge(n1,n2->next); return n2; } } struct lista *MergeSort(struct lista *lis) { struct lista *lis2 = lis; if(lis->testa && lis->testa->next) { lis2->testa = Split(lis->testa,NULL); lis = MergeSort(lis); lis2 = MergeSort(lis2); lis->testa = Merge(lis->testa,lis2->testa); } return lis; }
Grazie.

Rispondi quotando
