Not able to get the logic

not able to get the logic

make a recursive function let say reverseStack() in this store the top element of the stack in any variable lets say X and pop it from stack then send your remaining stack again to the reverseStack().
once you reaches the bottom of the stack means stack gets empty call a InsertatBottomStack function which will start inserting last element of the Stack X.
your InsertatBottomStack function should be such that it insert every element at the bottom of the stack (means pop if stack has any item then push your x then push popped item)

For example, let the input stack be

1  <-- top
2
3
4    

First 4 is inserted at the bottom.
4 <-- top

Then 3 is inserted at the bottom
4 <-- top
3

Then 2 is inserted at the bottom
4 <-- top
3
2

Then 1 is inserted at the bottom
4 <-- top
3
2
1

this is the code, but can you clear my one doubt. once if we call a reverse() function and at arriving 38th line again we called reverse function() ,it will again pop the element and x has new value assigned to it,it will be done till whole stack is not empty ,but how will insert(st,x) work? not able to undeestand that.

variable x is defined inside function not globally so its value will be same for that particular function when you call function reverse() again program will make new thread which will have its own variable x.

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.