please help me with the flow of programme
Stack reverse using recursion
Hello @advaitkr91
In this problem you need to reverse the elements inside the stack using recursion. You cannot use any other stack other than this.
We can use recursion to solve problems which can be broken down to SAME problems but of smaller sizes.
Example in this one, let say we have N elements and we are coding a recursive function with name “reverseStackRecursive”
Then we need to divide our problem to SAME problems but of smaller size, we can do something like
- Pop the topmost element
- call reverseStackRecursive(n-1) // Ask our function to reverse the n - 1 elements (SAME problem but of smaller size)
- Insert the popped element at the bottom
Example lets say we have in the stack
5
4
3
2
1
- Pop the topmost element - 5
Now stack is 4 3 2 1 (Rightmost is the bottom most element) - call reverseStackRecursive(n-1)
Now stack is 1 2 3 4 - Insert the popped element at the bottom
Now stack is 1 2 3 4 5
If you need any help, leave a reply to this thread and I will get back to you.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.