Please help me understand this question.
A question. How to read a singly linked list backwards? Here is the implementation,
Code:
void ReadListBackward(node* head)
{
node* p = head;
if(!p)
return;
if(p->next)
ReadListBackward(p->next);
else
{
printf("%d\n", p->data);
return;
}
printf("%d\n", head->data);
}
Could any guru here help me understand why it works? Thanks a lot.
Re: Please help me understand this question.
Quote:
Originally Posted by
LarryChen
Could any guru here help me understand why it works?
It works because of recursion (a function calls itself repeatedly until a termination criterion is met).
Here the ReadListBackward function is calling itself and each time advancing along the list in the forward direction. Finally when the end is reached all accumulated ReadListBackward calls are terminated in reverse order one by one. It's during this "rewinding" process the printing takes place so it will be in reverse. If you put the print immediately before the ReadListBackward call the order would be in the forward direction instead.
A warning. If the list is very long the number of accumulated ReadListBackward calls may be too many which will result in a stack overflow.
And the ReadListBackward function is little too complicated. It's possible to do this with just one print statement.
Re: Please help me understand this question.
Quote:
Originally Posted by
nuzzle
It works because of recursion (a function calls itself repeatedly until a termination criterion is met).
Here the ReadListBackward function is calling itself and each time advancing along the list in the forward direction. Finally when the end is reached all accumulated ReadListBackward calls are terminated in reverse order one by one. It's during this "rewinding" process the printing takes place so it will be in reverse. If you put the print immediately before the ReadListBackward call the order would be in the forward direction instead.
A warning. If the list is very long the number of accumulated ReadListBackward calls may be too many which will result in a stack overflow.
And the ReadListBackward function is little too complicated. It's possible to do this with just one print statement.
Thanks for your reply. First of all, let me take an example here,
assume there is four nodes in the list, node 1, node 2, node 3, node 4 like this,
1=>2=>3=>4
So the calling sequence would be like this,
ReadListBackward(1)=>ReadListBackward(2)=>ReadListBackward(3)=>ReadListBackward(4)=>print(4)=>return
So when ReadListBackward(4) returns, the control goes to ReadListBackward(3). Here what I really don't understand is that since node 3's next node is always not NULL, so how come it will print out 3? Thanks a lot.
Re: Please help me understand this question.
It will work because after returning, the line "printf("%d\n", head->data);" will be executed, which will print 3.
JeffB
Re: Please help me understand this question.
Quote:
So when ReadListBackward(4) returns, the control goes to ReadListBackward(3).
Correct. The program resumes at
Code:
printf("%d\n", head->data);
as this is ReadListBackward(3) then head->data is 3 and hence 3 is printed. When a function calls itself it re-enters the function and creates a new set of non-static variables. When the function exits, it goes back to the place in the function from which it was called with the same variable values that existed in the function before the call. This is called function recursion.
You might find this useful
http://www.dcs.bbk.ac.uk/~roger/cpp/week15.htm
Re: Please help me understand this question.
Quote:
Originally Posted by
2kaud
Correct. The program resumes at
Code:
printf("%d\n", head->data);
as this is ReadListBackward(3) then head->data is 3 and hence 3 is printed. When a function calls itself it re-enters the function and creates a new set of non-static variables. When the function exits, it goes back to the place in the function from which it was called with the same variable values that existed in the function before the call. This is called function recursion.
You might find this useful
http://www.dcs.bbk.ac.uk/~roger/cpp/week15.htm
I got it. Thank you very much!
Re: Please help me understand this question.
Quote:
Originally Posted by
LarryChen
I got it.
As I indicated, the recursive function can be simplified and then it becomes even clearer how it works,
Code:
void ReadListBackward(node* p) {
if (!p) {
printf("T!\n"); // recursion terminates
} else {
printf("F: %d\n", p->data); // print in forward direction
ReadListBackward(p->next); // recursive call
printf("B: %d\n", p->data); // print in backward direction
}
}