Queues


Overview of Queues

A queue is a first-in first-out data structure (FIFO). Conceptually, the queue data structure behaves like a line. New data is placed at the rear of the queue and when data is removed it is taken from the front. A printer maintains a list of jobs in a queue; the oldest job, the one that has been in the queue the longest, is serviced first.

The operations supported by a queue are as follows:

Queues are typically depicted as follows:

queues

The previous queue would be the result of the following operations:

enqueue(1)

enqueue(2)

enqueue(3)

A dequeue on the queue above would return the object 1 and the resulting queue would look as follows:

queues 2

A front on the queue above would return the object 2 and the queue itself would remain unchanged.

Implementation

A queue can be implemented using an array or a linked list. In essense, the queue operations simply limit the way in which we access data stored in a data structure such as an array. A queue can also be implemented using two stacks and a stack using two queues. Though these implementations are not terribly efficient, it is a good exercise to consider how you would build such an implementation.

When choosing which rudimentary data structure to use for an implementation, it is imperative that you consider efficiency. How many steps will enqueue and dequeue require?

To implement a queue, you will need to implement a Queue class that provides the queue operations described above. You will also want to provide appropriate error reporting. Therefore, it might make sense to have the dequeue operation throw a QueueEmptyException in the event that the programmer tries to dequeue from a queue with no elements.

+Implementing a Queue Using an Array

+Implementing a Queue Using a Linked List


Sami Rollins

Date: 2007-11-05