static void just_traverse(sll **head_ref) {
sll *first = *head_ref;
sll *second = (*head_ref)->next;
if(second == NULL) {
return;
}
just_traverse(&(second));
*head_ref = second;
printf("%d \t",second->payload);
}
In above code if I remove *head_ref = second; I get output with respect to stack however If I put this line *head_ref = second; It always print last element say {4,3,2,1} then it always prints 1?Can anybody explain why?
If Your list contains {4,3,2,1}, then your output should be
1 1 1right.This because of the statement
*head_ref = second;. In the 3rd call to the functionjust_traversehead_refwill points to2andsecondwill points to1. Now you are updating**head_refto1. Now you are printingsecond. So output will be value1And then it will return to the 2nd call, here
head_refis3andsecondis1(not2becuase we updated in 3rd all). Now againg assigning1to head and printingsecond. Again it will print the value1only.And then it will return to the 1st call, here
head_refis4andsecondis1(not3because we updated in 2nd call). Now againg assigning1to head and printingsecond. Again it will print the value1only.So the output will be three
1