Personal tools
You are here: Home Classes Fall 2004 - Spring 2005 CS 151 Deques and Priority Queues
Navigation
Log in


Forgot your password?
« May 2008 »
Su Mo Tu We Th Fr Sa
123
456789 10
11121314151617
18192021222324
25262728293031
 
Document Actions

Deques and Priority Queues

by admin last modified 2005-05-11 18:12

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?





 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: