Pregunta de entrevista de Amazon

implement queue with two stacks

Respuesta de la entrevista

Anónimo

18 may 2012

Maintain the queue in stack1 in reversed order. If you want to retrieve the head pop the contents of stack1 into stack2, reversing it in the process. Now pop the head from stack2 and pop the rest of the items back into stack1, putting them in previous order.