You are on page 1of 1

Hw7

1. Recall from your data structures course that a queue supports two operations, enqueue and dequeue,
and a stack supports two operations, push and pop.
(a) Illustrate how to simulate a queue using two stacks. What stack operations do you use, to
simulate the queue operations?
(b) Show that the amortized time complexity of any n simulated queue operations is O(n), assum-
ing you start with an empty structure. In other words the amortized time of any single operation
should be O(1).
You may use the accounting method or the potential method. For practice, feel free to try both.

2. An extended queue supports the two standard queue operations, but has one extra operation,
called Q-pop, through which we access and remove the item that has been enqueued most recently.
Show how to simulate an extended queue, using three stacks, such that the amortized cost of each
extended queue operation is constant.
You can only use stack operations, and you should not duplicate (copy) elements.

You might also like