GATE CSE
First time here? Checkout the FAQ!
x
+3 votes
173 views

Can Someone explain either Tree or Stack method to trace out this recursion ?

What is the output of this Program ?

asked in Programming by Veteran (42k points)   | 173 views

2 Answers

+4 votes
Best answer

This  code results in the printing of elements alternatively in linked list

suppose 10 || ----> 20 || ------> 30 || ------> 40 || -------> 50 || NULL

prints(10)        head->next->next ------->3rd node

prints(30)        head->next->next ------->5th node

prints(50)        here head->next is null prints(50) and again moves back prints results in reverse

10 30 50 50 30 10


if(head == null)  ---------------- if head is null nothing is done

 return



 

answered by Veteran (13.9k points)  
edited by
Can you explain something about order .in what order they will be inserted .
yes i will elobrate it
@shekhar i think thier is problem in this code

if head is null it means nothing is stored in  it how we can forward to head->data

Suppose first is pointer which stores address of list

then if(head == first )

then we can move to head->data na
IF HEAD=NULL MEANS ...IT IS NOT POINTING TO ANY THING(BUT CONTAINS DATA IN IT)
this is the condition checking means before doing anything we have to check whether Linked list has atleast one node in it or not if there is no node in it then there is no point of process it .and we simply return .but this is not the case ...to explain it you just have to assume a linked list in which atleast 2 nodes are there (for exp)
yeah once check out my answer say me if anything wrong
when it is going to print in reverse order then where will be the pointer of head ......is it going to take the I/p as the o/p of first printf .
since from node 1->node 3 --> node 5 then recursive function takes place since their is printf statement left at each recursion so at that position head-> data will be as it is

I mean Fun2(3) --->fun2(5) jump has been taken to finish fun2(5)  it again prints 50 (in my example)

then move back to fun2(3) at that function previously head is at 3rd position so it prints 30
+4 votes

First printf will print alternate elements from start to last 

Second printf will print in reverse order , same data which was printed by first printf .

answered by Veteran (47.4k points)  
edited by
why alternative ? How did you come to know ?
Sir,(take a ll with 8 elements)

it starts with the first node printing its data and then goes to 3rd node by head -> next -> next

and then last printf prints that data in reverse order .
take an ex - 1 ,2 ,null
first printf which is head -->data it should simply print the data continuously isn't ...explain me this first then i'll come to the second printf .i did not understand why would it be printing alternative .....

why continuously, there is no loop

it will just print current data .

Take an ex - 1 ,2 ,null

first printf prints 1

then checks for head->next not equal to null .(yes it is true)

the goes for head->nxt->next recursively which is null .

after recursion, it again enetrs the fun2

and then returns as head is null

 

Thanks to both @Kapil and Shiv to make me understand .


Top Users Jun 2017
  1. Bikram

    3704 Points

  2. Hemant Parihar

    1502 Points

  3. junaid ahmad

    1432 Points

  4. Arnab Bhadra

    1416 Points

  5. Niraj Singh 2

    1391 Points

  6. Debashish Deka

    1246 Points

  7. Rupendra Choudhary

    1194 Points

  8. rahul sharma 5

    1158 Points

  9. Arjun

    956 Points

  10. srestha

    950 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 Jun 19 - 25
  1. Bikram

    1960 Points

  2. Niraj Singh 2

    1386 Points

  3. junaid ahmad

    502 Points

  4. Debashish Deka

    414 Points

  5. sudsho

    410 Points


23,373 questions
30,079 answers
67,406 comments
28,396 users