CPP - Reverse Stack Using 1 Extra Stack

I have a doubt in this question when we transfer all the lements of S1 to S2 we have got the order in S2 so we can simply swap the stacks now and we will have the reversed order in S1 . I guess this approach is right .

For reversing a stack using 1 Extra stack, basic approach is that pop the 1st element from S1 and keep it separate , transfer all other N-1 elements from S1 to S2, then 1st element that was popped is inserted back into S1, and after that insert elements from S2 to S1,
again pop the 2nd element from S1 and keep it separate and then transfer other elements.

Refer to the video : https://youtu.be/b8RBt_BzfNM

Hi Ashutosh,
Your logic is not right as if you swap the stack S1 and S2 after transferring all elements to S2, the swap will persist in the function only where you have transferred the elements from S1 to S2. But when you will print S1 outside the function the S1 will be empty. So in order to reversed the stack you have to transfer the elements back from S2 to S1 in order that elements get swapped.

I guess it will definitely work just refer the approach when using the two stacks to implement a queue . It will definitely workout .

you are simply stating what is being told in the video .

Hi Ashutosh,
this approach works in case when we implement queue using stacks because while implementing the main stack variable is global to all functions and if you change the address of the stack it will change permanently but in reversing the stack if you swap both stacks then since scope of S1 and S2 is limited to that function only in which you had transferred the elements from S1 to S2, the change of address is temporary and hence if you print the size of actual stack in main function after this it will show 0.

cool got it, Thank you