L-6 CS-410 Data Structure and Algorithms 3 (2-1)
L-6 CS-410 Data Structure and Algorithms 3 (2-1)
Front of queue
New element is
added to the rear of
the queue
6-4
Conceptual View of a Queue
Removing an element
Element is removed
from the front of the
queue
6-5
Uses of Queues in Computing
• For any kind of problem involving FIFO
data
• Printer queue (e.g. printer in MC 235)
• Keyboard input buffer
• GUI event queue (click on buttons,
menu items)
• To encode messages (more on this later) 6-6
Uses of Queues in Computing
• 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
– Other examples? 6-7
Queue Operations
• enqueue : add an element to the tail of
a queue
• dequeue : remove an element from the
head of a queue
• first : examine the element at the head
of the queue (“peek”)
• Other useful operations (e.g. is the
queue empty)
• It is not legal to access the elements in 6-8
Operations on a Queue
Operation Description
6-9
Interface to a Queue in Java
public interface QueueADT<T> {
// Adds one element to the rear of the queue
public void enqueue (T element);
// Removes and returns the element at the front of the queue
public T dequeue( );
// Returns without removing the element at the front of the queue
public T first( );
// Returns true if the queue contains no elements
public boolean isEmpty( );
// Returns the number of elements in the queue
public int size( );
// Returns a string representation of the queue
public String toString( );
} 6-10
Using Queues: Coded
Messages
• A Caesar cipher is a substitution code
that encodes a message by shifting each
letter in a message by a constant amount k
– If k is 5, a becomes f, b becomes g, etc.
• Example: n qtaj ofaf
– Used by Julius Caesar to encode military
messages for his generals (around 50
BC)
6-11
Using Queues: Coded Messages
• Modern version: ROT13
– Each letter is shifted by 13
– “used in online forums as a means of
hiding spoilers, punchlines, puzzle
solutions, and offensive materials
from the casual glance” (Wikipedia)
6-12
Using Queues: Coded
• Messages
An improvement: change how much a letter is
shifted depending on where the letter is in the
message
• A repeating key is a sequence of integers that
determine how much each character is shifted
– Example: consider the repeating key
3 1 7 4 2 5
– The first character in the message is shifted by 3, the
next by 1, the next by 7, and so on
– When the key is exhausted, start over at the 6-13
An Encoded Message Using a
Repeated Key
Encoded message
n o v a n g j h l m u u r x l v
Key 3 1 7 4 2 5 3 1 7 4 2 5 3 1 7 4
k n o w l e d g e i s p o w e r
Decoded message
6-14
Using Queues: Coded Messages
• We can use a queue to store the values
of the key
– dequeue a key value when needed
– After using it, enqueue it back onto
the end of the queue
• Why?
6-16
Using Queues:
Ticket Counter Simulation
• Simulate the waiting line at a movie theatre:
– Determine how many cashiers are needed to
keep the customer wait time under 7 minutes
• Assume:
– Customers arrive on average every 15
seconds
– Processing a request takes two minutes once
a customer reaches a cashier
6-17
Results of Ticket Counter
Simulation
Number of
Cashiers
1 2 3 4 5 6 7 8 9 10
5317 2325 1332 840 547 355 219 120 120 120
6-18
Queue Implementation Issues
• What do we need to implement a queue?
– A data structure (container) to hold the
data elements
– Something to indicate the front of the
queue
– Something to indicate the end of the
queue
6-19
Queue Implementation
Using a Linked List
• Internally, the queue is represented as a linked
list of nodes, with each node containing a data
element
• We need two pointers for the linked list
– A pointer to the beginning of the linked list
(front of queue)
– A pointer to the end of the linked list (rear of
queue)
• We will also have a count of the number of items
6-20
in the queue
Linked Implementation of a
Queue
A queue q containing four elements
rear
q
front
4
count
6-21
Discussion
• What if the queue is empty?
6-22
Queue After Adding Element
New element is added in a node at the end of the list, rear points
to the new node, and count is incremented
rear
q
front
5
count
6-23
Queue After a dequeue
Operation
Node containing is removed from the front of the list (see previous
slide), front now points to the node that was formerly second, and count
has been decremented.
rear
q
front
4
count
6-24
Java Implementation
• The queue is represented as a linked
list of nodes:
– We will again use the LinearNode class
– front is a reference to the head of the
queue (beginning of the linked list)
– rear is a reference to the tail of the queue
(end of the linked list)
– The integer count is the number of nodes
6-25
in the queue
public class LinkedQueue<T> implements QueueADT<T> {
/**
* Attributes
*/
private int count;
private LinearNode<T> front, rear;
/**
* Creates an empty queue.
*/
public LinkedQueue() {
count = 0;
front = rear = null;
6-26
}
//-----------------------------------------------------------------
// Adds the specified element to the rear of the queue.
//-----------------------------------------------------------------
public void enqueue (T element) {
LinearNode<T> node = new LinearNode<T> (element);
if (isEmpty( ))
front = node;
else
rear.setNext (node);
rear = node;
count++;
}
6-27
//-----------------------------------------------------------------
// Removes the element at the front of the queue and returns a
// reference to it. Throws an EmptyCollectionException if the
// queue is empty.
//-----------------------------------------------------------------
public T dequeue ( ) throws EmptyCollectionException {
if (isEmpty( ))
throw new EmptyCollectionException ("queue");
T result = front.getElement( );
front = front.getNext( );
count--;
if (isEmpty( ))
rear = null;
return result;
} 6-28
Array Implementation of a
Queue
• First Approach:
– Use an array in which index 0 represents one
end of the queue (the front)
– Integer value count represents the number of
elements in the array (so the element at the
rear of the queue is in position count - 1)
• Discussion: What is the challenge with
this approach? 6-29
An Array Implementation of a
Queue
A queue aq containing four elements
front
0 1 2 3 4
…
aq queue
4
count
6-30
Queue After Adding an Element
The element is added at the array location given by the value of
count and then count is increased by 1.
0 1 2 3 4
…
aq queue
5
count
6-31
Queue After Removing an
Element
Element is removed from array location 0, remaining
elements are shifted forward one position in the array, and then
count is decremented.
0 1 2 3 4
…
aq queue
4
count
6-32
Java Array Implementation
• See ArrayQueue.java
6-33
public class ArrayQueue<T> implements QueueADT<T> {
private final int DEFAULT_CAPACITY = 100;
private int count;
private T[] queue;
public ArrayQueue() {
count = 0;
queue = (T[])(new Object[DEFAULT_CAPACITY]);
}
public ArrayQueue (int initialCapacity) {
count = 0;
queue = (T[])(new Object[initialCapacity]);
}
6-34
//-----------------------------------------------------------------
// Adds the specified element to the rear of the queue,
// expanding the capacity of the queue array if
// necessary.
//-----------------------------------------------------------------
public void enqueue (T element) {
if (size() == queue.length)
expandCapacity( );
queue[count] = element;
count++;
}
6-35
//-----------------------------------------------------------------
// Removes the element at the front of the queue and returns
// a reference to it. Throws anEmptyCollectionException if the
// queue is empty.
//-----------------------------------------------------------------
public T dequeue ( ) throws EmptyCollectionException {
if (isEmpty( ))
throw new EmptyCollectionException (“Empty queue");
T result = queue[0];
count--;
// shift the elements
for (int i = 0; i < count; i++)
queue[i] = queue[i+1];
queue[count] = null;
return result;
6-36
}
Second Approach: Queue as a
Circular Array
• If we don't fix one end of the queue at index 0,
we won't have to shift elements
• Circular array is an array that conceptually
loops around on itself
– The last index is thought to “precede” index 0
– In an array whose last index is n, the location
“before” index 0 is index n; the location
“after” index n is index 0
• Need to keep track of where the front as well6-37as
Conceptual Example of a Circular Queue
front
After 5
1 After 7 enqueues 1 dequeues
0 0
front
12 12
11 11
rear rear
10 10
rear
1
0
front
12
11
After 8 more enqueues
10
6-38
Circular Array Implementation of
a Queue
3 2 3
1 4
front queue 5
cq 0
8 5 n-1 6
rear count n-2 7
n-3 8
...
9
10
6-39
A Queue Straddling the End of
a Circular Array
98 2 3
1 4
front queue 5
cq 0
2 4 99 6
rear count 98 7
97 8
...
9
10
6-40
Circular Queue Drawn Linearly
Queue from previous slide
98
cq front queue 0 1 2 3 4 96 97 98 99
2 4 …
rear count
6-41
Circular Array Implementation
• When an element is enqueued, the value of
rear is incremented
• But it must take into account the need to
loop back to index 0:
0 1 2 3 4 5 6 7
2
front queue
cq
2 4 These locations
rear count should be in use 6-44
We could build the new array, and copy the queue elements into
contiguous locations beginning at location front:
0 1 2 3 4 5 6 7
2
front queue
cq
6 4
rear count
6-45
Better: copy the queue elements in order to the beginning of the new
array
0 1 2 3 4 5 6 7
0
front queue
cq
4 4
rear count
6-46
New element is added at rear = (rear+1) % queue.length
See expandCapacity() in CircularArrayQueue.java
0 1 2 3 4 5 6 7
0
front queue
cq
5 5
rear count
6-47
Rizwan Ch.
University Of Agriculture Faisalabad Dipalpur Campus Okara
(CS-405)
CS-410 Data Structure and Algorithms 3(2-1)
Week-6
18/04/2025 [email protected] 48