Title: The Queue ADT
1The Queue ADT
- CS241
- Western Washington University
2What is a Queue?
A Queue is a type of collection where data is
accessed in a FIFO or LILO manner. FIFO
First In First Out LILO Last In Last Out Some
applications of a queue Process queues Printer
job queues Assembly line processing Any kind of
simulation
3Specifying Queue ADT Behavior
What operations are associated with a
queue? Create an empty queue Destroy a
queue Determine whether a queue is
empty Determine the number of items in a
queue Insert an item into the end of the
queue Retrieve an item from the front of the
queue Remove an item from the front of the
queue Display the contents of the queue These
operations specify an interface to a Queue ADT,
regardless of which implementation we will choose.
4The Queue Interface
class Queue public // Create and Destroy
Queue() Queue() // Insert, Retrieve, and
Remove void insert(const QueueItemType item)
const QueueItemType front() const const
QueueItemType remove() // Size bool empty()
const int size() const // Print out
queue ostream operatorltlt(ostream os,
const Queue q)
5Choosing an Implementation
We have quite a few choices for our Queue data
representation 1. A statically allocated
array typedef int QueueItemType const int
MAX_QUEUE 100 class Queue private QueueItem
Type _dataMAX_QUEUE int _size 2. A
dynamically allocated array typedef int
QueueItemType class Queue private QueueItemTy
pe _data int _size
6Limitations???
Lets analyze these implementations and identify
their limitations. The static array
implementation requires us to set an upper bound
on the number of items we can hold in the
queue. The dynamic array implementation requires
us to know the number of items in the queue when
we are creating the list. How practical is the
Queue ADT with these limitations?
7Other Options
When we studied the List ADT, we learned about a
linked representation. How can we apply that
representation to the queue? 1) Public
Inheritance - Is-A class Queue public List
private ???? 2) Aggregation -
Has-A class Queue private List _data
3) Private Inheritance - As-A class Queue
private List private ????
8Pros and Cons
What are the advantages, if any, of these other
options? Same as the advantages of the linked
listthe data structure can grow as needed. No
longer need to try to guess how many items will
be in the queue. What are the disadvantages, if
any, of these other options? Not necessarily the
same as the linked list. We dont care about
efficient random access in a queue!
9A Template Solution
Finally, we could use a template representation
template ltclass Tgt class Queue public //
Create and Destroy Queue() Queue() //
Insert, Retrieve, and Remove void insert(const
T item) const T front() const const T
remove() // Size bool empty() const int
size() const // Print out queue template
ltclass Tgt ostream operatorltlt(ostream os, const
QueueltTgt s)
10Or the STL Template Queue
include ltqueuegt template ltclass Tgt class
queue This queue class is part of the C
Standard Template Library.