A similar question was asked earlier there, but the question here is the reverse of it, using two queues as a stack. The question…
Given two queues with their standard operations (enqueue, dequeue, isempty, size), implement a stack with its standard operations (pop, push, isempty, size).
There should be two versions of the solution.
- Version A: The stack should be efficient when pushing an item; and
- Version B: The stack should be efficient when popping an item.
I am interested in the algorithm more than any specific language implementations. However, I welcome solutions expressed in languages which I am familiar (java,c#,python,vb,javascript,php).
Version A (efficient push):
Version B (efficient pop):