How to print the value contained in linked list in reverse order??
This algorithm would work
int func(struct node* temp){
if (temp==NULL) return 0;
func(temp->next);
printf("%d ",temp->data);
return 0;
}
or
add another pointer that points to the previous element in the list
or
traverse through the linked list and keep on adding elements to an array
print the array in reverse order
I hope it helped!
-
if you think that recursive calls is not so visible…how this magic going on than just do one thing
-
just traverse the linked list and push element into stack
while(stack not empty){
printf("%d ",stack.top);
stack.pop();
}
or as @prrateekk mention use array and store all value as you iterate and
print array element in reverse order..
hope it helps you
HAPPY CODING
@prrateekk …no problem at all it works fine…
actually recursive calls also store in stack till you will not get last function call…
but beginner it is quite tough to understand how recursion going on…
that’s why i have shown him to demo of stack seprately…
hope you got it…
happy coding
Yeah that’s right. After all functions execute on stack
Is there any problem with my recursive code? As I tested it, it works fine.
If there’s any problem, please let me know