2014年(12)
分类: IT职场
2014-07-13 11:15:54
Show how to implement a stack using two queues. Analyze the running time of the stack operations.
We have two queues and mark one of them as active. PUSH queues an element on the active queue. POP should dequeue all but one element of the active queue and queue them on the inactive. The roles of the queues are then reversed, and the final element left in the (now) inactive queue is returned.
The PUSH operation is Θ(1), but the POP operation is Θ(n) where n is the number of elements in the stack.