Sollicitatievraag bij Microsoft

Implement enqueue and dequeue using stacks.

Antwoord op sollicitatievraag

Anoniem

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