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).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…