Another approach

I implemented the other approach as explained by prateek bhaiya where the push operation will take O(n) time but it is not working

Can please check where and what the problem is?

do checking in top function like this

Your approach is different , as prateek bhaiya explained this program can be done in two ways. My approach is for making changes using push function please take a look at the program and the comments.

your algo is working like this


also in main function, the while loop. Before that print cout<<"s.empty() = "<<s.empty()
your s is showing empty. that’s why you are not able to go inside while loop.
Try to implement the way i have told you by sharing code of link earlier.

I got the mistake in my program
bool empty()
{

    return q1.empty() && q2.empty();

}

Wrong check condition rest is fine.

A humble request to at least try to understand my version of code and try to correct what’s wrong (though i know understanding someone’s code takes lot of effort ). I already know the approach you were giving me the link to and have coded it out at well but it does not hurt to try another approach .

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.