2. The Queue ADT
Objectives
• examine queue processing
• define a queue abstract data type
• demonstrate how a queue can be used to solve problems
• examine various queue implementations
• compare queue implementations
3. The Queue ADT
Data
a collection whose elements are added at one end (the rear or
tail of the queue) and removed from the other end (the front or
head of the queue)
a queue is a FIFO (first in, first out) data structure
Example
• Line of people waiting at a bank machine
• Collection of documents sent to a shared printer
Note • Items are removed from a queue in the
same order as they were inserted
• it is not legal to access the elements in
the middle of the queue!
5. Queue Errors
1. Underflow – occurs when trying to Dequeue (or Front at) an empty queue
2. Overflow – occurs when trying to Enqueue onto an already full queue.
6. Uses of Queues in Computing
Queue is useful for any for any kind of problem involving FIFO data
1. printer queue
2. keyboard input buffer
3. GUI event queue
4. in simulation studies, where the goal is to reduce waiting times:
− optimize the flow of traffic at a traffic light
− determine number of cashiers to have on duty at a grocery store at different times of day.
7. Implementation of Queue ADT
1. Array implementation of Queue
• The entire queue is represented by an array together with two external “pointers” to keep track of the
front and rear.
• Suitable for applications in which the maximum queue size is known ahead of
time.
front Index prior the front element
rear Index of the rear (last) element
Both pointers are initially set to -1 (i.e. point before the
first cell of the array.
14. Implementation of Queue ADT
1. Linked List implementation of Queue
• The entire queue is represented by a linked list of nodes
together with a pointer to the first node of the linked list
(front) and a pointer to the end of the linked list (rear).
• Suitable for applications in which the maximum queue
size is not known beforehand.
Note ▪ With a linked-list representation, overflow
will not happen (unless you exhaust
memory, which is another kind of problem)
▪ Underflow can happen, and should be
handled.