A similiar 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.
- 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). Thanks in advance.