Chap 5
Chap 5
Chap 5
Chap. 5
5.1 Queues
QueueA structure in which elements are added
to the rear and removed from the front; a first in,
first out (FIFO) structure
Operations on Queues
Constructor
new: creates an empty queue
Transformers
enqueue: adds an element to the rear of a
queue
dequeue: removesandreturnsthe front
element of the queue
Effectsof
Queue
Operations
Using Queues
A queue of processes that are ready to execute or that
are waiting for a particular event to occur.
A buffer is often implemented as a queue.
We wait in queues to buy pizza, to enter movie theaters,
to drive on a turnpike, and to ride on a roller coaster.
Another important application of the queue data structure
is to help us simulate and analyze such real world
queues.
Exceptional Situations
dequeue what if the queue is empty?
throw a QueueUnderflowException
plus define an isEmpty method for use by
the application
Boundedness
Two versions of the Queue ADT
a bounded version
an unbounded version
Three interfaces
QueueInterface: features of a queue not affected
by boundedness
BoundedQueueInterface: features specific to a
bounded queue
UnboundedQueueInterface: features specific to
an unbounded queue
QueueInterface
//--------------------------------------------------------------------------// QueueInterface.java
by Dale/Joyce/Weems
Chapter 5
//
// Interface for a class that implements a queue of Objects.
// A queue is a first-in, first-out structure.
//--------------------------------------------------------------------------package ch05.queues;
public interface QueueInterface<T>
{
T dequeue() throws QueueUnderflowException;
// Throws QueueUnderflowException if this queue is empty,
// otherwise removes front element from this queue and returns it.
boolean isEmpty();
// Returns true if this queue is empty, otherwise returns false.
}
TheUnboundedQueueInterface
package ch05.queues;
public interface UnboundedQueueInterface<T> extends QueueInterface<T>
{
void enqueue(T element);
// Adds element to the rear of this queue.
}
Floating
Front
Design
We use this
Approach.
Wrap
Around
with
Floating
Front
Design
Operation efficiency
All operations, for each approach, are O(1)
Except for the Constructors:
Array-based: O(N)
Link-based: O(1)