stavo guardando il codice qui sotto da Stanford libreria:Linked lista ricorsiva invertire
void recursiveReverse(struct node** head_ref)
{
struct node* first;
struct node* rest;
/* empty list */
if (*head_ref == NULL)
return;
/* suppose first = {1, 2, 3}, rest = {2, 3} */
first = *head_ref;
rest = first->next;
/* List has only one node */
if (rest == NULL)
return;
/* put the first element on the end of the list */
recursiveReverse(&rest);
first->next->next = first;
/* tricky step -- see the diagram */
first->next = NULL;
/* fix the head pointer */
*head_ref = rest;
}
Quello che non capisco è l'ultimo passo ricorsivo per esempio se la lista è 1-2-3-4 Ora per l'ultimo passo ricorsivo prima sarà 1 e resto sarà 2. Quindi, se si imposta * head_ref = rest .. che rende il capo della lista 2 ?? Qualcuno può spiegare come dopo aver invertito la testa della lista diventa 4 ??
Grande spiegazione! Grazie mille! – harihb
@chris: \t * head_ref = rest; Non lo capisco..per favore aiutami a superare questo – kTiwari
head_ref è un riferimento al primo nodo nella (sotto) lista al livello attuale di ricorsione. Il resto si riferisce al primo nodo nel resto dell'elenco. Quindi, impostare head_ref a riposo ha l'effetto di tagliare la vecchia testa. –