Topic 7
Queue
TSE3124 - DATA STRUCTURES
Introduction
A queue is an abstract linear data structure, in which elements are
inserted at one end and removed from another end of the
structure.
Operations in a queue are also known as First-In-First-Out (FIFO).
The first element inserted into a queue will be the front element
and the last element inserted will be the tail of queue.
Insertion is only done at the tail of the queue while removal is
done to the front element.
A queue can be implemented using an array (static storage) or a
linked list (dynamic storage).
TSE3124 - DATA STRUCTURES
Queues
queue: Retrieves elements in the order they were added.
First-In, First-Out ("FIFO")
Elements are stored in order of
insertion but don't have indexes.
Client can only add to the end of the
queue, and can only examine/remove
the front of the queue.
TSE3124 - DATA STRUCTURES
Basic queue operations:
Methods
Modifier and Type Method and Description
boolean add(E e)
Inserts the specified element into this queue if it is possible to do so
immediately without violating capacity restrictions, returning true upon
success and throwing an IllegalStateException if no space is currently
available.
E element()
Retrieves, but does not remove, the head of this queue.
boolean offer(E e)
Inserts the specified element into this queue if it is possible to do so
immediately without violating capacity restrictions.
E peek()
Retrieves, but does not remove, the head of this queue, or returns null if
this queue is empty.
E poll()
Retrieves and removes the head of this queue, or returns null if this
queue is empty.
E remove()
Retrieves and removes the head of this queue.
TSE3124 - DATA STRUCTURES
The Queue ADT
The Queue ADT stores Auxiliary queue
arbitrary objects operations:
Insertions and deletions follow object front(): returns the
the first-in first-out scheme element at the front without
removing it
Insertions are at the rear of
integer size(): returns the
the queue and removals are at number of elements stored
the front of the queue
boolean isEmpty():
Main queue operations: indicates whether no
enqueue(object): inserts an elements are stored
element at the end of the Exceptions
queue
Attempting the execution of
object dequeue(): removes dequeue or front on an
and returns the element at the empty queue throws an
front
TSE3124 - DATA of the queue
STRUCTURES EmptyQueueException
Queue Example
Operation Output Q
enqueue(5) – (5)
enqueue(3) – (5, 3)
dequeue()5 (3)
enqueue(7) – (3, 7)
dequeue()3 (7)
front() 7 (7)
dequeue()7 ( )
dequeue()“error” ( )
isEmpty()true ( )
enqueue(9) – (9)
enqueue(7) – (9, 7)
size() 2 (9, 7)
enqueue(3) – (9, 7, 3)
enqueue(5) – (9, 7, 3, 5) TSE3124 - DATA
STRUCTURES
dequeue()9 (7, 3, 5)
Array-based Queue
Use an array of size N in a circular fashion
Two variables keep track of the front and rear
f index of the front element
r index immediately past the rear element
Array location r is kept empty
normal configuration
Q
0 1 2 f r
wrapped-around configuration
Q
0 1 2 r f
TSE3124 - DATA STRUCTURES
Queue Operations
We use the Algorithm size()
return (N - f + r) mod N
modulo operator
(remainder of Algorithm isEmpty()
return (f = r)
division)
Q
0 1 2 f r
Q
0 1 2 r f
TSE3124 - DATA STRUCTURES
Queue Operations (cont.)
Operation enqueue Algorithm enqueue(o)
if size() = N - 1 then
throws an exception if
throw FullQueueException
the array is full else
This exception is Q[r] o
implementation- r (r + 1) mod N
dependent
Q
0 1 2 f r
Q
0 1 2 r f
TSE3124 - DATA STRUCTURES
Queue Operations (cont.)
Algorithm dequeue()
Operation dequeue if isEmpty() then
throws an exception throw EmptyQueueException
if the queue is empty else
o Q[f]
This exception is f (f + 1) mod N
specified in the return o
queue ADT
Q
0 1 2 f r
Q
0 1 2 r f
TSE3124 - DATA STRUCTURES
TSE3124 - DATA STRUCTURES
Application of Queues
Operating systems:
queue of print jobs to send to the printer
queue of programs / processes to be run
queue of network data packets to send
Programming:
modeling a line of customers or clients
storing a queue of computations to be performed in order
Real world examples:
people on an escalator or waiting in a line
cars at a gas station (or on an assembly line)
TSE3124 - DATA STRUCTURES
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
3. Q.enqueue(e)
The Queue
1. Deque the 2 . Service the 3. Enqueue the
next element next element serviced element
Shared
Service
TSE3124 - DATA STRUCTURES
Programming with Queues
add(value places given value at back of queue
)
remove() removes value from front of queue and returns it;
throws a NoSuchElementException if queue is
empty
peek() returns front value from queue without removing it;
returns null if queue is empty
size() returns number of elements in queue
Queue<Integer> q = new LinkedList<Integer>();
isEmpty()
q.add(42); returns true if queue has no elements
q.add(-3);
q.add(17); // front [42, -3, 17] back
System.out.println(q.remove()); // 42
IMPORTANT: When constructing a queue you must use a new LinkedList object instead of a new Queue
object.
This has to do with a topic we'll discuss later called interfaces. TSE3124 - DATA
STRUCTURES
Queue Interface in Java
public interface Queue {
Java interface public int size();
corresponding to public boolean isEmpty();
our Queue ADT public Object front()
throws EmptyQueueException;
Requires the public void enqueue(Object o);
definition of class public Object dequeue()
EmptyQueueException throws EmptyQueueException;
}
No corresponding
built-in Java class
TSE3124 - DATA STRUCTURES
Queue Idioms
As with stacks, must pull contents out of queue to view them.
while (!q.isEmpty()) {
do something with q.remove();
}
another idiom: Examining each element exactly once.
int size = q.size();
for (int i = 0; i < size; i++) {
do something with q.remove();
(including possibly re-adding it to the queue)
}
Why do we need the size variable? TSE3124 - DATA STRUCTURES
Another method of
queue
implementation is by
using linked list, as
shown in class
MyQueue2
TSE3124 - DATA STRUCTURES
TSE3124 - DATA STRUCTURES
TSE3124 - DATA STRUCTURES
TSE3124 - DATA STRUCTURES
Mixing stacks and
queues
We often mix stacks and queues to achieve certain effects.
Example: Reverse the order of the elements of a queue.
Queue<Integer> q = new LinkedList<Integer>();
q.add(1);
q.add(2);
q.add(3); // [1, 2, 3]
Stack<Integer> s = new Stack<Integer>();
while (!q.isEmpty()) { // Q -> S
s.push(q.remove());
}
while (!s.isEmpty()) { // S -> Q
q.add(s.pop());
}
System.out.println(q); // [3, 2, 1]
TSE3124 - DATA STRUCTURES