Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
void fun1(struct node* head)
{
  if(head == NULL)
    return;
  
  fun1(head->next);
  printf("%d  ", head->data);
}


What I have tried:

WHAT IS WORKING OF THIS CODE???ACCORDING TO ME LAST ELEMENT OF THE LINKED list should print again and again as the termination condition is null at the node end
but in the output of this code showing data of nodes printed in reverse order??
Posted
Updated 25-Aug-22 9:11am
Comments
CPallini 26-Aug-22 3:51am    
The right question is
"HOW RECURSION WORKS?"

Quote:
How do i...can solve this problem. This is a problem of data structure topic singly linked list I have to find output of this code

There is a simple way to know: Run the code and see the result.
There is another way: use the debugger to run the code step by step and see what is going on inside.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
CPallini 26-Aug-22 3:50am    
5.
Patrice T 26-Aug-22 3:52am    
Thank you
Quote:
WHAT IS WORKING OF THIS CODE??? this code showing data of nodes printed in reverse order??

The comment suggests that the output of the elements is unexpected. Since it is a recursive function, one should consider whether the output is done first or first dived to the last element. This has an effect of course.
 
Share this answer
 
Comments
CPallini 26-Aug-22 3:34am    
5.
It's called recursion, and its a fundamental algorithm.
Think about factorials for a moment: N! = N * (N - 1)! for all values of N greater than zero - for all others N! is 1. So if N is 5, then N! can be expressed as
5! = 5 * (5 - 1)!   ==   5 * 4!
and the:
4! = 4 * (4 - 1)!   ==   4 * 3!
3! = 3 * (3 - 1)!   ==   3 * 2!
2! = 2 * (2 - 1)!   ==   2 * 1!
1! = 1
So this gives us:
5! = 5 * 4 * 3 *2 * 1

The factorial expression is naturally recursive because it's result is expressed in terms of itself.

That's what your code is doing: it check if the current node exists, and if not, it returns without doing anything.
Otherwise, it recursively calls itself on the next node, and when that returns it prints the current node.

So nothing gets printed until it gets to the final node, which is printed first. Then it returns, and prints the previous node, and so on back to the beginning.

Having said that, it's a piss poor piece of coding that barely works at all - because as the linked list grows, it consumes stack space and that is very small indeed - typically 1MB, and all return addresses, parameters, and local variables take up stack space. Grow the list too large, and you will run out of stack space, and your app will crash.
Recursion is a powerful tool - but it needs to be used with care, and only where it can be completely justified. Linked lists aren't recursive, so it's a very poor idea to process and iterative collection using recursive methods.
 
Share this answer
 
Comments
CPallini 26-Aug-22 3:34am    
5.
CPallini 26-Aug-22 3:50am    
As matter of fact, the recursive factorial itself is a poor piece of code, according to The Master (Wirth).
:-D
OriginalGriff 26-Aug-22 4:14am    
I would entirely agree, but it is at least based on a recursive definition. :D
Lies-to-children again ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900