Queue ADT

Queues: ordered collection of items such that items are inserted and removed only from opposite ends. Insert at rear, remove from front. Implements FIFO.

Liken to waiting line.

Textbook ObjectQueue class is focus. See author's documentation at

http://www.cs.colorado.edu/~main/docs/edu.colorado.collections.ObjectQueue.html

Author also provides a family of queue classes, one for each primitive type. As an example, see author's documentation for IntQueue class at

http://www.cs.colorado.edu/~main/docs/edu.colorado.collections.IntQueue.html

Author provides separate classes for array and linked-list implementation. The linked list implementations are named by replacing "...Queue" in class name with "...LinkedQueue".

Java does not provide a class specifically for queues! Java does provide a List interface (not to be confused with the java.awt.List class which is used for scrolling lists of selectable text items). Several java classes directly implement the List interface: ArrayList, LinkedList, Vector.

Java documentation suggests that java.util.LinkedList is a reasonable choice for implementing queues, because it allows items to be inserted or removed from either end of the list. See Sun's documentation at http://java.sun.com/j2se/1.3/docs/api/java/util/LinkedList.html.

Java documentation does not address the issue of using java.util.ArrayList for implementing queues. It can be done, but must be done carefully. We will study array implementation of queues in greater depth.

Queues are used extensively in operating systems, in networks software, and in simulation programming.

 

Motivation: OS buffers

 

 

Specification of author's ObjectQueue class

Part I: standard stuff

Part II: interesting stuff

 

Array implementation of queue

Instance variables:

How should getFront and insert operate?

 

Implementation candidate 1:

What is the problem with Implementation 1? GetFront is not time-efficient.

 

Implementation candidate 2:

What is the problem with implementation 2? Is not space efficient.

 

Implementation candidate 3 uses concept of circular array (to use space efficiently):

For insert:

For getFront:

How would you implement ensureCapacity?

Note: full array does not imply that front==0 and rear==data.length-1!

It does imply that if front==0 then rear==data.length-1 else rear==front-1.

 

Linked List implementation

Instance variables:

Will a singly-linked list suffice?

Why do you not need preRear (e.g. reference to node that precedes rear)?

Given

class Node
{
	public Object data;
	public Node next;
}

How would you implement insert?

How would you implement getFront?

 

 

A Word on Priority Queues

Each item has associated priority. It should be selected only if no items of higher priority exist and all previously inserted items of same priority have been selected (e.g. goes to rear of line for its priority).

Two approaches:

1. an array of queues, one per priority level. Insert is easy, but getFront requires search for highest priority queue that is non-empty.

2. a single queue with enhanced insert method to place new item just prior to first item having lower priority (e.g. after the last item having same priority level). GetFront stays the same. You want linked-list implementation due to insertion in middle of queue.

 


[ Lectures | CSC 132 | Peter Sanderson | Computer Science | SMSU ]


Last reviewed: 27 October 2000

Peter Sanderson ( PeteSanderson@smsu.edu )