hi guys
the following function is correct. i need help understand the stack
tracing.
void RecursiveReverse(struct node** head) {
struct node* first;
struct node* rest;
if (*head == NULL)
return; // empty list base case
first = *head; // suppose first = {1, 2, 3}
rest = first->next; // rest = {2, 3}
if (rest == NULL)
return; // empty rest base case
RecursiveReverse(&rest); // Recursively reverse the smaller {2, 3}
case
// after: rest = {3, 2}
first->next->next = first; // put the first elem on the end of the
list
first->next = NULL; // (tricky step -- make a drawing)
*head = rest; // fix the head pointer
}
for example given list is : 3 -> 4 ->2 ->1 ->6 ->NULL
after all recursive call finished i.e. after execution of this
statement
if (rest == NULL)
return; // empty rest base case
the stack will be like this
6 6 NULL
1 1 6
2 2 1
4 4 2
3 3 4
----- ------- -------
head first rest
1 1 6
2 2 1
4 4 2
3 3 4
----- ------- -------
head first rest
6
2 2 1
4 4 2
3 3 4
----- ------- -------
head first rest
6
1
4 4 2
3 3 4
----- ------- -------
head first rest
.
.
.
6
1
2
4
----- ------- -------
head first rest
My question is "why stack "rest" is not changing??
thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---