Is this correct one for this Question?
Is this correct one for this Question?
@cbcao263 hey,yes the logic is looking fine according to the question.
(By making enQueue operation costly) This method makes sure that oldest entered element is always at the top of stack 1, so that deQueue operation just pops from stack1. To put the element at top of stack1, stack2 is used.
enQueue(q, x):
- While stack1 is not empty, push everything from stack1 to stack2.
- Push x to stack1 (assuming size of stacks is unlimited).
- Push everything back to stack1.
Here time complexity will be O(n)
deQueue(q):
- If stack1 is empty then error
- Pop an item from stack1 and return it
Here time complexity will be O(1)
Sir, I have printed that one but still it is not giving correct output. Please tell why so and how to do it properly.
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.