L5 Queue
L5 Queue
•QUEUES
What is a queue?
Basic features of Queue
Enqueue
Dequeue
Queue Front
Queue Rear
Queue Example
ENQUEUE
DEQUEUE
FRONT OF QUEUE
REAR OF QUEUE
Queue: a First-In-First-Out (FIFO) list
D rear
C rear C D rear
B rear B B C
rear front A front
A A front A front B
front
CHAPTER 3 11
Queues - Application
CHAPTER 3 12
Application: Job scheduling
fron
t re
ar Q[0]Q
[1]Q[2]Q[3
]C omments
-1 -1 que
ueisemp ty
-1 0 J1 Jo
b1isadde d
-1 1 J1 J2 Jo
b2isadde d
-1 2 J1 J2 J3 Jo
b3isadde d
0 2 J2 J3 Jo
b1isd e
leted
1 2 J3 Jo
b2isd e
leted
CHAPTER 3 13
Applications of Queue
In approach [B] we remove the element from head position and then
move head to the next position.
22
Complexity Analysis of Queue Operations
•Enqueue: O(1)
•Dequeue: O(1)
•Size: O(1)
Queue variations
• While inserting elements, when you reach the end of an array and
you need to insert another element, you must insert that element at
the beginning (given that the first element has been deleted and the
space is vacant).
25
26
Basic features of Circular Queue
1. Head pointer will always point to the front of the queue, and
2.Tail pointer will always point to the end of the queue.
3.Initially, the head and the tail pointers will be pointing to the same location,
this would mean that the queue is empty.
27
New data is always added to the location pointed by the tail pointer,
and once the data is added, tail pointer is incremented to point to the
next available location.
28
The head and the tail pointer will get reinitialised to 0 every time they
reach the end of the queue.
➢ Also, the head and the tail pointers can cross each other.
➢ In other words, head pointer can be greater than the tail.
➢ This will happen when we dequeue the queue a couple of times and
the tail pointer gets reinitialised upon reaching the end of the queue.
Going Round and Round
➢ Another very important point is keeping the value of the tail and
the head pointer within the maximum queue size.
➢ In the diagrams above the queue has a size of 8, hence, the value
of tail and head pointers will always be between 0 and 7.
34
Enqueue
void enqueue(int queue[], int element, int& rear, int arraySize, int& count)
{
if(count == arraySize) // Queue is full
printf(“OverFlow\n”);
else
{
queue[rear] = element;
rear = (rear + 1)%arraySize;
count = count + 1;
}
}
35
Dequeue
IsEmpty
bool isEmpty(int count)
{
return (count == 0);
}
37
38
39
Double-ended queue
➢ In a standard queue, a character is inserted at the
back and deleted in the front.
41
Double Ended Queue
◼ Types of deque
42
Priority Queue
43
Priority Queue
44
END OF QUEUE
45