4 Queues
4 Queues
These slides has been extracted, modified and updated from original slides of :
• Data Structures and Algorithms in Java, 5th edition. John Wiley& Sons, 2010. ISBN 978-0-470-38326-1.
• Dr. Hanna’s slides (http://aimanhanna.com/concordia/comp352/index.htm)
❑ Insertions are at the rear of the queue and removals are at the
front of the queue.
2 Queues
The Queue ADT
❑ Auxiliary queue operations:
◼ object front(): returns the element at the front without
removing it.
❑ Exceptions
◼ Attempting the execution of dequeue or front on an empty
queue throws an EmptyQueueException
3 Queues
Example
Operation Output Q Contents
enqueue(5) -- [5]
enqueue(3) -- [5, 3]
dequeue() 5 [3]
enqueue(7) -- [3, 7]
dequeue() 3 [7]
front() 7 [7]
dequeue() 7 []
dequeue() “error” []
enqueue(9) -- [9]
isEmpty() false [9]
size() 1 [9]
dequeue() 9 []
4 Queues
Applications of Queues
❑ Direct applications
◼ Waiting lists, bureaucracy
◼ Access to shared resources (e.g., printer)
◼ Multiprogramming
❑ Indirect applications
◼ Auxiliary data structure for algorithms
◼ Component of other data structures
5 Queues
Array-based Queue
❑ Can be implemented using an array, Q, of fixed
capacity.
6 Queues
Array-based Queue
❑ Instead, two variables keep track of the front and
rear:
f index of the front element
r index of the next available element in the array (that is the
one immediately past the rear/last element)
7 Queues
Array-based Queue
❑ This configuration would allow enqueue(), dequeue()
and front() to be performed in constant time, that is
O(1).
❑ However, this configuration has a serious problem.
0 1 2 f r 16
wrapped-around configuration
Q
9 0 1 2 r f 16 Queues
Queue Operations
❑ We use the Algorithm size()
modulo operator return ((N - f) + r) mod N
(remainder of
Algorithm isEmpty()
division) return (f = r)
Q
0 1 2 f r
Q
0 1 2 r f
10 Queues
Queue Operations (cont.)
❑ Operation enqueue Algorithm enqueue(o)
throws an exception if if size() = N - 1 then
the array is full throw FullQueueException
❑ This exception is else
implementation- Q[r] o
dependent r (r + 1) mod N
Q
0 1 2 f r
Q
0 1 2 r f
11 Queues
Queue Operations (cont.)
❑ Operation dequeue Algorithm dequeue()
throws an exception if isEmpty() then
if the queue is empty throw EmptyQueueException
❑ This exception is else
specified in the o Q[f]
queue ADT f (f + 1) mod N
return o
Q
0 1 2 f r
Q
0 1 2 r f
12 Queues
Queue Interface
❑ Java interface public interface Queue<E> {
corresponding to public int size();
our Queue ADT public boolean isEmpty();
(Note: not identical to the
shown interface) public E front()
❑ Requires the throws EmptyQueueException;
definition of class public void enqueue(E element);
EmptyQueueException
public E dequeue()
❑ There is no throws EmptyQueueException;
corresponding }
built-in Java class
13 Queues
Application: Round Robin Schedulers
❑ We can implement a round robin scheduler using a
queue Q by repeatedly performing the following
steps:
1. e = Q.dequeue()
2. Service element e
Q.enqueue(e)
3.
Queue
Dequeue Enqueue
Shared
Service
Queues 14
Growable Array-based Queue
❑ In an enqueue() operation, when the array is full, we
can replace the array with a larger one instead of
throwing an exception.
15 Queues
Double-Ended Queues
❑ A double-ended queue (dequeue, or D.Q.) ADT is
richer than the stack ADT and the queue ADT.
16 Queues
Double-Ended Queues
❑ The fundamentals operations allowed by such queue
are:
◼ addFirst(e): Insert as new element at the head of the queue.