Frage im Vorstellungsgespräch bei Microsoft

Implement enqueue and dequeue using stacks.

Antwort im Vorstellungsgespräch

Anonym

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