If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Please help me understand this question.

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.

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,

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.

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.

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Re: Please help me understand this question.

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.