Microsoft Interview Question

Implement enqueue and dequeue using stacks.

Interview Answer

Anonymous

Oct 13, 2011

Use two stacks, 1st stack is used as temp storage, the 2nd used as queue. Stack temp; Stack queue; Enqueue(item) { while (queue.Count > 0) { stack.push(queue.pop); } queue.push(item) while (stack.Count > 0) { queue.push(stack.pop); } } Dequeue() { return queue.pop(); }