Domanda di colloquio di Microsoft

Implement enqueue and dequeue using stacks.

Risposta di colloquio

Anonimo

13 ott 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(); }