Implementation of stack using 2 queues

i have done without pointers but its not running

Hey Shreyas!
You have not considered a lot of factors while writing push, pop and top functions, that’s why the code is not running. Please check the following code for correct implementation:

I am swapping q1 and q2 when q1 becomes empty hence that should satisfy.

You have to check the conditions on push and pop as well.

What you are checking if q2 is not empty or not I am ignoring it because I will always swap q1 with q2 after popping out from q1 and making it empty after swapping each time q2 becomes empty hence I am not checking for that case

But that’s not making the implementation any easier and neither is the complexity any better, if you can use 2 queues then that can simply be done by just putting those extra conditions in all 3 functions instead of always swapping

Conditions usage is a better way but on swapping code should work but it is not why so.

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.