Lecture 08 - Queue
Lecture 08 - Queue
February 2, 2025
+
Objectives
At the end of this topic, you should be able to:
Analyze and define memory requirements for queue data structure.
Apply programming skills to implement queue data structures.
Develop an appropriate algorithm to perform different operations on queue
data structures.
February 2, 2025
+
Recommended Readings
February 2, 2025
+
What is a Queue
February 2, 2025
+
The Queue ADT
The Queue ADT stores arbitrary objects
Insertions and deletions follow the first-in first-out
scheme
Insertions are at the rear of the queue and removals are
at the front of the queue
February 2, 2025
+
The Queue ADT
February 2, 2025
+
Example
Operation Output Q
enqueue(5)
enqueue(3)
dequeue()
enqueue(7)
dequeue()
getfront()
getrear()
dequeue()
dequeue()
isEmpty()
enqueue(9)
enqueue(7)
getsize()
enqueue(3)
enqueue(5)
dequeue()
February 2, 2025
+
Example
Operation Output Q
enqueue(5) – (5)
enqueue(3) – (5, 3)
dequeue() 5 (3)
enqueue(7) – (3, 7)
dequeue() 3 (7)
getfront() 7 (7)
getrear() 7 (7)
dequeue() 7 ()
dequeue() null ()
isEmpty() true ()
enqueue(9) – (9)
enqueue(7) – (9, 7)
getsize() 2 (9, 7)
enqueue(3) – (9, 7, 3)
enqueue(5) – (9, 7, 3, 5)
dequeue() 9 (7, 3, 5)
February 2, 2025
+ Queue – Implementation
or Circular
February 2, 2025
+
Queue Implementation – Array
February 2, 2025
+
Queue Implementation – Array
The Front of the Queue is indicated by the
index front_index which is initialized with 0.
If the Queue is not empty, data[front_index]
Remove Insert
is the element in the front, next to be served front rea
(Dequeue) (Enqueue)
Dequeue off of the Queue at front_index++
r
data[rear_index++] = value
February 2, 2025
+ Queue Implementation – Array (Java Class)
The EnQueue() and DeQueue() methods will be implemented in the Class February 2, 2025
+ Queue Implementation - Array (Algorithms)
Algorithm: isFull
Input: Array
Output: TRUE if count == size
Method:
1. Return (count == size)
2. End
February 2, 2025
+ Queue Implementation - Array (Algorithms)
Algorithm: isEmpty
Input: Array
Output: TRUE if count == 0
Method:
1. Return (count == 0)
2. End
February 2, 2025
+ Queue Implementation – Array (Algorithms)
Algorithm: EnQueue
Input: Array, value
Output: Update Array after EnQueue operation
Method:
1. If (isFull()) Then Display(“Queue is overflow”);
Return
[ End If]
2. Set Queue[rear_index] = value
3. Set rear_index = rear_index + 1
4. Set count = count + 1
5. End February 2, 2025
+ Queue Implementation – Array (Algorithms)
Algorithm: DeQueue
Input: Array
Output: Update Array after DeQueue operation
Method:
1. If (isEmpty()) Then Display(“Queue is underflow”);
Return [ End If]
2. Set temp = Queue[front_index]
3. Set front_index = front_index + 1
4. Set count = count - 1
5. Return temp
6. End February 2, 2025
+ Problem
Rear increases by 1 after each EnQueue( )
Front increases by 1 after each DeQueue( )
Rear Front
Now:
49 48 47 4 3 2 1 0
Rear Front
After Enqueue:
49 48 47 4 3 2 1 0
Rear Front
After Dequeue:
49 48 47 4 3 2 1 0
February 2, 2025
+ Problem
Suppose 50 calls to EnQueue() have been made, now the queue array
is full and Rear is 50
The queue is not full, but it is WRONG to add at position indexed by Rear
as it is out of bound! February 2, 2025
+ Solution 1 – change the DeQueue method
Algorithm: DeQueue
Input: Array
Output: Update Array after DeQueue operation (shift
everything to the left, front_index is always 0
Method:
1. Set temp = Queue[front_index]
2. Repeat for i = 0 to rear_index - 2
Very expensive, in the
3. Queue[i]=Queue[i+1] [End for]
worst case, n-1
4. Set rear_index = rear_index - 1 operations.
Method:
1. If (isFull()) Then Display(“Queue is overflow”);
Return
[ End If]
2. Set Queue[rear_index] = value
3. Set rear_index = (rear_index + 1) mod size
4. Set Count = Count + 1
5. End February 2, 2025
+ Queue Implementation - Circular Array
(Algorithms)
Algorithm: DEQueue
Input: Array
Output: Update Array after DEQueue operation
Method:
1. If (isEmpty()) Then Display(“Queue is underflow”);
Return
[ End If]
2. Set temp = Queue[front_index]
3. Set front_index = (front_index + 1) mod size
4. Set Count = Count – 1
5. Return temp
6. End
February 2, 2025
+
Queue Implementation – Array
Java Code
February 2, 2025
+ Linear Queue (array) 1- Create QueueArray Class that
implements queue using array
and implements the main
operations of the queue.
February 2, 2025
+
2- Create object from QueueArray Class.
You should specify the maximum size of the
queue.
February 2, 2025
+ Queue (Circular array)
February 2, 2025
+ Queue Implementation – Linked
List
February 2, 2025
+ Queue Implementation – Linked List
February 2, 2025
+ Queue – Linear Linked-List
February 2, 2025
+ Queue – Linear Linked-List
February 2, 2025
+ Queue – Linear Linked-List (Java Class)
February 2, 2025
+ Queue – Linear Linked-List (Algorithms)
Algorithm: EnQueue
Input: Linear Linked-List, value
Output: Update Linear Linked-List after EnQueue
operation
Method:
1. Set Node newNode = new Node(Value, null)
2. If (isEmpty()) Then
Set front = newNode Set rear
= newNode
3. Else Set rear.Next=newNode Set rear =
newNode
February 2, 2025
4. [End If]
+ Queue – Linear Linked-List (Algorithms)
Algorithm: DeQueue
Input: Linear Linked-List
Output: Update Linear Linked-List after DeQueue
operation
Method:
1. If (isEmpty()) Then Display(“Queue is underflow”);
Return
[ End If]
2. Set front = front.Next
3. Set Count = Count – 1
4. If (Count == 0) then rear = null
5. End
February 2, 2025
+ Queue – Circular Linked List (Algorithms)
Algorithm: EnQueue
Input: Circular Linked-List, value
Output: Update the Circular Linked-List after EnQueue
operation
Method:
1. Set Node newNode = new Node(Value, null)
2. If (isEmpty()) Then
Set front = newNode, Set
rear = newNode
3. Else Set rear.Next=newNode, Set rear =
newNode
4. [End If] February 2, 2025
+ Queue – Circular Linked List (Algorithms)
Algorithm: DeQueue
Input: Circular Linked-List
Output: Update Circular Linked-List after DeQueue
operation
Method:
1. If (isEmpty()) Then Display(“Queue is underflow”);
Return
[ End If]
2. Set front = front.Next
3. Set rear.Next=front
4. Set Count = Count - 1
February 2, 2025
5. End
+
Two More Varieties of Queue
1. Double-Ended Queue (DEQueue)
Can add and delete from both front and rear
EnQueueFront, EnQueueRear, DeQueueFront, DeQueueRear
2. Priority Queue,
Add element in the queue
Delete the element with highest priority February 2, 2025
+ DEQueue – using Double Linked-List
February 2, 2025
+ DEQueue – using Linked-List
Algorithm: EnQueueRear
Input: Double Linked-List, value
Output: Update Double Linked-List after ENQueue operation
Method:
1. Set Node newNode = new Node(Value, null, null)
2. If (isEmpty()) Then
Set front = newNode Set rear
= newNode
3. Else Set rear. Next=newNode
Set newNode.Prev=rear, Set
rear = newNode
4. [End If] February 2, 2025
+ DEQueue – using Linked-List
Algorithm: EnQueueFront
Input: Double Linked-List, value
Output: Update Double Linked-List after EnQueue operation
Method:
1. Set Node newNode = new Node(Value, null, null)
2. If (isEmpty()) Then
Set front = newNode, Set rear
= newNode
3. Else Set front.Prev = newNode
Set newNode.Next = front, Set
front = newNode
4. [End If] February 2, 2025
+ DEQue – using Linked-List
Algorithm: DeQueueFront
Input: Double Linked-List
Output: Update Double Linked-List after DeQueue
operation
Method:
1. If (isEmpty()) Then Display(“Queue is underflow”);
Return
[ End If]
2. Set front = front.Next
3. Set front.previous=Null
4. Set Count = Count - 1
February 2, 2025
5. End
+ DEQue – using Linked-List
Algorithm: DeQueueRear
Input: Double Linked-List
Output: Update Double Linked-List after DeQueue
operation
Method:
1. If (isEmpty()) Then Display(“Queue is underflow”);
Return
[ End If]
2. Set rear = rear.Prev
3. Set rear.next = Null
4. Set Count = Count - 1
February 2, 2025
5. End
+ DEQue – using Linked-List
Example:
Operation Output Q
EnQueueFront(5)
EnQueueFront(3)
EnQueueRear(7)
getFirst()
DeQueueFront()
getLast()
DeQueueRear()
getSize()
DeQueueRear()
isEmpty()
February 2, 2025
+ DEQue – using Linked-List
Example:
Operation Output Q
EnQueueFront(5) - (5)
EnQueueFront(3) - (3,5)
EnQueueRear(7) - (3,5,7)
getFirst() 3 (3,5,7)
DeQueueFront() 3 (5,7)
getLast() 7 (5,7)
DeQueueRear() 7 (5)
getSize() 1 (5)
DeQueueRear() 5 ()
isEmpty() true ()
February 2, 2025
+
Priority Queue
February 2, 2025
+ Priority Queue
Priority Queue is a queue, where each element has a
"priority" associated with it. In a priority queue, an
element with high priority is served before an element
with low priority.
February 2, 2025
+ Two ways to keep the Priority Queue
Performance: Performance:
insert takes 1 operation insert takes n operations
since we can insert the since we have to find the
item at the rear end place where to insert the
remove takes n operations item
since we have to remove takes 1 operation
traverse the entire queue since the element with
of length n to find the highest priority is at the
highest priority element front
February 2, 2025
+ Priorities
Each element in the Priority Queue is modeled as: Key-Value composite
known as entry. The Key indicates the priority of the element.
Example
A sequence of priority queue methods:
Unsorted List Sorted List Output Method (Key/priority,
value)
EnQueue(5,A)
EnQueue(9,C)
EnQueue(3,B)
DeQueue()
EnQueue(7,D)
DeQueue()
DeQueue()
DeQueue()
Dequeue()
IsEmpty() February 2, 2025
+ Priorities
Each element in the Priority Queue is modeled as: Key-Value composite known as
entry. The Key indicates the priority of the element.
Example
A sequence of priority queue methods:
Unsorted List Sorted List Output Method (Key/priority,
value)
{ (5,A) } { (5,A) } EnQueue(5,A)
{ (5,A) (9,C)} { (9,C) (5,A)} EnQueue(9,C)
{ (5, A) (9,C) (3,B) {(9,C) (5,A) EnQueue(3,B)
} (3,B)}
{ (5, A) (3,B) } {(5,A) (3,B)} (9,C) DeQueue()
{(5, A) (3,B) {(7,D) (5,A) EnQueue(7,D)
(7,D)} (3,B)}
{(5, A) (3,B)} {(5,A) (3,B)} (7,D) DeQueue()
{ (3, B) } { (3, B) } (5,A) DeQueue()
{} {} (3, B) DeQueue()
{} {} null Dequeue() February 2, 2025
+
Priority Queue Operations:
February 2, 2025
+ Priority Queue – using Linear Linked-List
Algorithm: insert/ EnQueue
Input: Singly Linked-List, key, value
Output: Update Singly Linked-List after insert operation (insert in the correct
order)
Method:
1. Set Node newNode = new Node(value, key, null), Current = front , Prev=null
2. Repeat while (Current != null and key < Current.key) [ locate the correct
position]
Method:
1. If (isEmpty()) Then Display(“Queue is underflow”); Return null [ End
If]
2. Set Node temp=front
3. Set front = front.Next
4. Set size = size – 1
5. Return temp
6. End
February 2, 2025
+ Queue – Linear Linked-List (Algorithms)
Algorithm: getMax
Input: Linear Linked-List
Output: Node having the maximum key
Method:
1. If (isEmpty()) Then Display(“Queue is underflow”); Return null
Else Return front
[ End If]
2. End
February 2, 2025
+ Queue – Linear Linked-List (Algorithms)
Algorithm: isEmpty
Input: Linear Linked-List
Output: TRUE if size == 0
Method:
1. Return (size == 0)
2. End
February 2, 2025
PriorityQueue class
February 2, 2025
PriorityQueue class
February 2, 2025
PriorityQueue class
February 2, 2025
In the main(), create object from
PriorityQueue class
February 2, 2025
Key: 15 Value: BB
Outputs: Key: 10 Value: AA
Key: 9 Value: CC
Key: 5 Value: FF
Key: 3 Value: EE
Key: 2 Value: DD
Key: 1 Value: GG
BB has the max priority
Key: 15 Value: BB is deleted
-------Display---------
Key: 10 Value: AA
Key: 9 Value: CC
Key: 5 Value: FF
Key: 3 Value: EE
Key: 2 Value: DD
Key: 1 Value: GG
AA has the max priority
Key: 10 Value: AA is deleted
-------Display---------
Key: 9 Value: CC
Key: 5 Value: FF
Key: 3 Value: EE
Key: 2 Value: DD
Key: 1 Value: GG
February 2, 2025
+
Applications of Queues
February 2, 2025
+
Applications of Queues
Direct applications
Call center phone system use Queue to hold people inline until a service
representative is free
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
February 2, 2025
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)
Queue
Dequeue Enqueue
Shared
Service
February 2, 2025
+
Practice
February 2, 2025
+
From the book
R-6.11: Suppose you have a DEQueue D containing the numbers
(1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially
empty queue Q. Give a code fragment that uses only D and Q (and no other
variables) and results in D storing the elements in the order (8,7,6,5,4,3,2,1).
R-6.12: Repeat the previous problem using the DEQueue D and an initially
empty stack S.
EnQueueFront D EnQueueRear
8 7 6 5 4 3 2 1
DeQueueFront
DeQueueRear
1 2 3 4 5 6 7 8
EnQueueFront D EnQueueRear
1 2 3 4 5 6 7 8
DeQueueFront
DeQueueRear
February 2, 2025
+
R-6.11: Solution:
Algorithm: reverse D using Q
Input: D – (1,2,3,4,5,6,7,8)
Method:
Q=new Queue(8)
End
February 2, 2025
+
R-6.12 : Description:
D: (1,2,3,4,5,6,7,8) Stack S: Empty
February 2, 2025
+
R-6.12 : Description:
EnQueueFront EnQueueRear
D
DeQueueFront
8 7 6 5 4 3 2 1
DeQueueRear
8
7
6
S
5
4
3
2
1
EnQueueFront EnQueueRear
D
DeQueueFront
1 2 3 4 5 6 7 8
DeQueueRear
February 2, 2025
+
R-6.12 : Solution:
Algorithm: reverse D using S
Input: D – (1,2,3,4,5,6,7,8)
Method:
S=new Stack(8)
End
February 2, 2025
+
C-6.24: Solution:
Let Original Q1: (1,2,3,4,5,6,7,8)
Input: Q2 – (9,10,11,12)
Method:
• While(Q2 is not Empty)
• Q1.EnQueue(Q2.DeQueue())
• [Endloop]
End
February 2, 2025
+
End of Queue - 07
February 2, 2025