hbs5 - S i +1 to make room. But if S i +1 is already full,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
CS473ug Head Banging Session #5 10/03/06 - 10/05/06 1. Simulating Queues with Stacks A queue is a first-in-first-out data structure. It supports two operations push and pop . Push adds a new item to the back of the queue, while pop removes the first item from the front of the queue. A stack is a last-in-first-out data structure. It also supports push and pop. As with a queue, push adds a new item to the back of the queue. However, pop removes the last item from the back of the queue (the one most recently added). Show how you can simulate a queue by using two stacks. Any sequence of pushes and pops should run in amortized constant time. 2. Multistacks A multistack consists of an infinite series of stacks S 0 , S 1 , S 2 , . . . , where the i th stack S i can hold up to 3 i elements. Whenever a user attempts to push an element onto any full stack S i , we first move all the elements in S i to stack
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: S i +1 to make room. But if S i +1 is already full, we first move all its members to S i +2 , and so on. To clarify, a user can only push elements onto S . All other pushes and pops happen in order to make space to push onto S . Moving a single element from one stack to the next takes O (1) time. Figure 1. Making room for one new element in a multistack. (a) In the worst case, how long does it take to push one more element onto a multistack containing n elements? (b) Prove that the amortized cost of a push operation is O (log n ), where n is the maximum number of elements in the multistack. 3. Powerhungry function costs A sequence of n operations is performed on a data structure. The i th operation costs i if i is an exact power of 2, and 1 otherwise. Determine the amortized cost of the operation. 1...
View Full Document

This note was uploaded on 10/14/2011 for the course ECON 101 taught by Professor Smith during the Spring '11 term at West Virginia University Institute of Technology.

Ask a homework question - tutors are online