COMP 250 Winter 2010
lecture 8 – queues
January 22, 2010
You are familiar with queues in daily life. You know that when you have a single resource such as
a cashier in the cafeteria, you need to “join the end of the line” and the person at the front of the
line is the one being served. The key property of a queue is that, among those objects/persons/etc
currently in the queue,
the one being served/removed is the one who frst entered the queue.
The queue abstract data type (ADT) has two basic operations associated with it:
which adds an element to the queue, and
which removes an element from the queue.
We could also have operations
which checks if there is an element in the queue, and
which returns the Frst element in the queue (but does not remove it), and
returns the number of items in the queue.
Often one writes
Notice that these opera-
tions are basically the same as those of a stack, except that their meaning is di±erent: removing an
element from a queue removes the least recently added element, whereas removing an element from
a stack removes the most recently added. We say that queues implement “Frst come, Frst served”
policy (also called ²I²O, Frst in Frst out), whereas stacks implement a LI²O policy, namely last
in, Frst out.
Suppose we add (and remove) items
in the following order, shown on left. On the
right is show the corresponding state of the queue
Data structures for implementing a queue
Singly linked list
One way to implement a queue is with a singly linked list. Just as you join a line at the back,
when you add an element to a singly linked list queue, you manipulate the
can be done by modifying two references, namely the
reference and the
reference of the
The reason is presumably because it is somewhat awkward to say “we enqueue the element” or “we dequeue an