Deques and Priority Queues
Deques and Priority Queues
A deque is a doubly-ended queue, which permits insertions and deletions at both the front and rear.Interface:
boolean isEmpty();
void addFront(Object obj);
void addRear(Object obj);
Object removeFront();
Object removeRear();
Question: Is there an implementation in which all of these operations are O(1)?
A priority queue is a collection of objects with the property that when an object is removed, the largest object (according to some ordering) is chosen for removal.
Interface:
boolean isEmpty();
void add(Object obj); // insert an object into the priority queue.
Object remove(); // remove an object from the priority queue.
example Use a priority queue to sort the elements in an array.
Two strategies are possible for using a list to implement a priority queue. One is to keep the list in descending (or ascending) order at all times. When a removal is performed, the object to be removed is the one at the beginning (or end) of the list. An alternative approach is to store the objects in any order, such as the order in which they are inserted. When a removal is performed, the list must be searched for the largest element.
Q: What would be a good choice of list implementation for each of these strategies?
Q: What are the running times of add and remove in each case?