Understanding Queue Data Structures
Understanding Queue Data Structures
QUEUE
OUTLINES
Introduction
Types of Queue data structure
Linear Queue Operations
Insertion Operation
Deletion Operation
Circular Queue Operations
Insertion Operation
Deletion Operation
Double Ended Queue(Deque)
Priority Queue
Applications of Queue
Question & Answer Discussion
2
QUEUE IN A REAL LIFE
3
INTRODUCTION
Non-primitive linear data structure
Ordered collection of homogeneous elements
A new element is added at one end called rear end and
the existing elements are deleted from the other end
called front end.
Queue works on the principle of First-In-First-Out
(FIFO),the element that has been added first to the list
will be the element that will be removed first from the
list.
4
MODELS OF QUEUE
5
TYPES:
Linear Queue
Circular Queue
Double-Ended Queue(Deque)
Priority Queue
6
OPERATIONS ON LINEAR QUEUE
Rear Front
7
Contd...
Rear Front
8
INSERTION(ENQUEUE) OPERATION
10
INSERTION(ENQUEUE) OPERATION
•Max=5
•Front=-1
•Rear=-1
Rear=-1 Front=-1
11
INSERTION(ENQUEUE) OPERATION
Insert 10
10
Rear=0 Front=0
12
INSERTION(ENQUEUE) OPERATION
Insert 20
20 10
Rear=1 Front=0
13
INSERTION(ENQUEUE) OPERATION
Insert 30
30 20 10
Rear=2 Front=0
14
INSERTION(ENQUEUE) OPERATION
Insert 40
40 30 20 10
Rear=3 Front=0
15
INSERTION(ENQUEUE) OPERATION
Insert 50
50 40 30 20 10
Rear=4 Front=0
16
INSERTION(ENQUEUE) OPERATION
Insert 60
Rear=Max-1 (Rear=4)
50 40 30 20 10
Rear=4 Front=0
Queue Overflow 17
Implementation
#include <stdio.h>
#include <stdlib.h>
18
Implementation
// Function to insert an item into the queue // Step 3: Insert the item into the queue
void Q_INSERT(int QUEUE[], int ITEM) { QUEUE[REAR] = ITEM;
// Step 1: Check for queue overflow
if (REAR == MAX - 1) { // Optional: Print the inserted item and
printf("Queue Overflow\n"); current queue state
return; printf("Inserted %d at position %d\n",
} ITEM, REAR);
}
// Step 2: Check if the queue is empty
if (FRONT == -1 && REAR == -1) {
// If empty, set both FRONT and REAR to 0
FRONT = 0;
REAR = 0;
} else {
// Move the rear pointer to the next position
REAR++;
}
19
Implementation
// Function to print the current queue elements
void printQueue(int QUEUE[ ]) {
if (FRONT == -1) {
printf("Queue is empty\n");
return;
}
20
Implementation
int main() {
int QUEUE[MAX];
21
DELETION(DEQUEUE) Operation
ALGORITHM: Q_DELETE(QUEUE,MAX,FRONT,REAR,
ITEM)
Input: QUEUE is the linear array with maximum size
[Link] and REAR are the pointer variables
pointing to Front end and Rear end respectively.
Output: This algorithm deletes an element from the queue
QUEUE and stores it in the variable ITEM.
23
DELETION(DEQUEUE) Operation
Delete
50 40 30 20 10
Rear=4 Front=0
Deleted Element=10
24
DELETION(DEQUEUE) Operation
Delete
50 40 30 20
Rear=4 Front=1
Deleted Element=20
25
DELETION(DEQUEUE) Operation
Delete
50 40 30
Rear=4 Front=2
Deleted Element=30
26
DELETION(DEQUEUE) Operation
Delete
50 40
Rear=4 Front=3
Deleted Element=40
27
DELETION(DEQUEUE) Operation
Delete
50
Rear=4 Front=4
Deleted Element=50
28
DELETION(DEQUEUE) Operation
Delete
Rear=-1 Front=-1
29
DELETION(DEQUEUE) Operation
Delete
Rear=-1 Front=-1
Queue Underflow 30
Implementation
#include <stdio.h>
#include <stdlib.h>
31
Implementation
// Step 3: Check if the queue becomes empty
after deletion
// Function to delete an item from the queue
if (FRONT == REAR) {
int Q_DELETE(int QUEUE[]) {
// If the queue becomes empty, reset
// Step 1: Check for queue underflow
FRONT and REAR
if (FRONT == -1 && REAR == -1) {
FRONT = -1;
printf("Queue Underflow\n");
REAR = -1;
return -1; // Indicate that no item was
} else {
deleted
// Move the front pointer to the next
}
position
FRONT++;
// Step 2: Delete an element from the queue
}
int ITEM = QUEUE[FRONT];
// Optional: Print the deleted item
printf("Deleted %d from position %d\n",
ITEM, FRONT);
return ITEM; // Return the deleted item
}
32
Implementation
// Function to print the current queue elements
void printQueue(int QUEUE[]) {
if (FRONT == -1) {
printf("Queue is empty\n");
return;
}
33
Implementation
// Delete an item from the queue
// Example usage
int deletedItem = Q_DELETE(QUEUE);
int main() {
if (deletedItem != -1) {
int QUEUE[MAX];
printf("Deleted item: %d\n", deletedItem);
}
// Insert elements into the queue
Q_INSERT(QUEUE, 10);
// Print the current queue state after deletion
Q_INSERT(QUEUE, 20);
printQueue(QUEUE);
Q_INSERT(QUEUE, 30);
// Final delete to show underflow
// Print the current queue state
deletedItem = Q_DELETE(QUEUE);
printQueue(QUEUE);
deletedItem = Q_DELETE(QUEUE);
deletedItem = Q_DELETE(QUEUE); // This
should trigger underflow
return 0;
}
34
Implementation of Queue using
Linked List
35
Implementation
#include <stdio.h>
#include <stdlib.h>
36
Implementation
// Enqueue function to add an element to the queue
void enqueue(int value) {
// Create a new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Heap Overflow\n");
return;
}
newNode->data = value; // Set the value
newNode->next = NULL; // Since it's the last node, next is NULL
// If the queue is empty, both front and rear will point to the new node
if (front == NULL) {
front = rear = newNode;
} else {
rear->next = newNode; // Link the new node at the end of the queue
rear = newNode; // Make the new node the rear node
}
printf("Enqueued %d into the queue\n", value);
}
Implementation
// Dequeue function to remove an element from the queue
int dequeue() {
if (front == NULL) {
printf("Queue Underflow\n");
return -1; // Return -1 if the queue is empty
}
int dequeuedValue = front->data; // Get the data of the front node
struct Node* temp = front; // Temporarily store the front node
front = front->next; // Move the front to the next node
void printQueue() {
if (front == NULL) {
printf("Queue is empty\n");
return;
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
printQueue(); // Print all elements in the queue
printf("Front element is %d\n", peek());
dequeue();
printQueue(); // Print the queue after one dequeue operation
dequeue();
printQueue(); // Print the queue after another dequeue operation
printf("Front element after dequeuing is %d\n", peek());
return 0;
}
Circular Queue
Circular Queue is just a variation of the linear queue in
which front and rear-end are connected to each other
to optimize the space wastage of the Linear queue
and make it efficient.
42
Circular Queue
• As the insertion in the queue is from the rear end
and in the case of Linear Queue of fixed size
insertion is not possible when rear reaches the end
of the queue.
43
OPERATION ON CIRCULAR QUEUE
44
INSERTION (ENQUEUE) OPERATION
ALGORITHM: C_Q_INSERT(C_QUEUE,MAX,FRONT,REAR,
ITEM)
Input: C_QUEUE is the Circular Queue with maximum size
[Link] and REAR are the pointer variables
pointing to Front end and Rear end respectively.
Output: This algorithm inserts an element ITEM on to
the circular queue C_QUEUE.
46
INSERTION(ENQUEUE) OPERATION
C_Queue
7 0 rear = -1 front = -1
6 1
5 2
4 3
47
INSERTION(ENQUEUE) OPERATION
rear = 0
C_Queue
Insert 10 front = 0
7 0
10
6 1
5 2
4 3
48
INSERTION(ENQUEUE) OPERATION
C_Queue
Insert 20 front = 0
7 0
rear = 1
10
6 1
20
5 2
4 3
49
INSERTION(ENQUEUE) OPERATION
C_Queue
Insert 30 front = 0
7 0
10
6 1
20
30
5 2
rear = 2
4 3
50
INSERTION(ENQUEUE) OPERATION
C_Queue
Insert 40 front = 0
7 0
10
6 1
20
30
5 2
40
4 3
rear = 3
51
INSERTION(ENQUEUE) OPERATION
C_Queue
Insert 50 front = 0
7 0
10
6 1
20
30
5 2
50 40
4 3
rear = 4
52
INSERTION(ENQUEUE) OPERATION
C_Queue
Insert 60 front = 0
7 0
10
6 1
20
60 30
5 2
50 40
rear = 5
4 3
53
INSERTION(ENQUEUE) OPERATION
C_Queue
Insert 70 front = 0
7 0
10
rear = 6 6 1
70 20
60 30
5 2
50 40
4 3
54
INSERTION(ENQUEUE) OPERATION
rear = 7
C_Queue
Insert 80 front = 0
7 0
80 10
6 1
70 20
60 30
5 2
50 40
4 3
55
INSERTION(ENQUEUE) OPERATION
rear = 7
C_Queue
Insert 90 front = 0
7 0
Front =(rear+1)%max
80 10
6 1
70 20
60 30
5 2
50 40
4 3
Queue Overflow
56
DELETION(DEQUEUE) Operation
ALGORITHM:
C_Q_DELETE(C_QUEUE,MAX,FRONT,REAR, ITEM)
Input: C_QUEUE is the Circular Queue with maximum size
MAX. FRONT and REAR are the pointer variables
pointing to Front end and Rear end respectively.
Output: This algorithm deletes an element from the Circular
Queue C_QUEUE and stores it in the variable ITEM.
57
DELETION(DEQUEUE) Operation
58
DELETION(DEQUEUE) Operation
rear = 7
C_Queue
front = 0
7 0
80 10
6 1
70 20
60 30
5 2
50 40
4 3
59
DELETION(DEQUEUE) Operation
rear = 7
C_Queue
Delete front = 0
7 0
80 10
6 1
70 20
60 30
5 2
50 40
4 3
60
DELETION(DEQUEUE) Operation
rear = 7
C_Queue
Delete
7 0
80
6 1
front = 1
70 20
60 30
5 2
50 40
4 3
61
DELETION(DEQUEUE) Operation
rear = 7
C_Queue
Delete
7 0
80
6 1
70
60 30
5 2 front = 2
50 40
4 3
62
DELETION(DEQUEUE) Operation
rear = 7
C_Queue
Delete
7 0
80
6 1
70
60
5 2
50 40
4 3
front = 3
63
DELETION(DEQUEUE) Operation
rear = 7
C_Queue
Delete
7 0
80
6 1
70
60
5 2
50
4 3
front = 4
64
DELETION(DEQUEUE) Operation
rear = 7
C_Queue
Delete
7 0
80
6 1
70
60
5 2
front = 5
4 3
65
DELETION(DEQUEUE) Operation
rear = 7
C_Queue
Delete
7 0
front = 6
80
6 1
70
5 2
4 3
66
DELETION(DEQUEUE) Operation
rear = 7
C_Queue
Delete front = 7
7 0
80
6 1
5 2
4 3
67
DELETION(DEQUEUE) Operation
C_Queue
Delete
7 0 rear = -1 front = -1
5 2
4 3
Queue Underflow
68
Example: Consider the following circular queue with N = 8.
1. Initially, Rear = -1, Front =- 1 4. Insert 20, Rear = 2, Front = 0.
7 0 rear = -1 front = -1 front
7 0
6 1 10
6 50 1
5 2 20
5 2 Rear
4 3
4 3
2. Insert 10, Rear = 0, Front = 0 5. Insert 70, Rear = 3, Front = 0
front front
7 0 rear 7 0
10 10
6 1 6 50 1
20
5 2 5 70 2
4 3 4 3 Rear
3. Insert 50, Rear = 1, Front = 0 6. Delete , Rear = 3, Front = 1
7 0
front 6 50 1 front
7 0
20
10
6 50 1 rear 5 70 2
4 3
5 2 Rear
69 4 3
69
7. Insert 35, Rear = 4, Front =1 10. Insert 20, Rear = 7, Front = 1
7 0 Rear
7 0
6 50 1 front
20 front
6 30 50 1
20
5 35 70 2 40 20
5 35 70 2
4 3
Rear
4 3
8. Insert 40, Rear = 5, Front = 1 11. Insert 25,Rear=0,Front=1
Rear
7 0 7 0
20 25
6 50 1 front 6 30 50 1 front
40 20 40 20
Rear 5 35 70 2 5 35 70 2
4 3 4 3
9. Insert 30, Rear = 6, Front = 1 12. Delete , Rear =Rear
0, Front = 2.
7 0
20 25
6 30 1
7 0
40 20
rear 6 30 1 front 5 35 70 2 front
50
40 20 4 3
5 35 70
2
70 4 3
70
DOUBLE ENDED QUEUE(DEQUE)
Insertion Deletion
10 20 30 40 50 60
Deletion Insertion
71 Front Rear
DOUBLE ENDED QUEUE(DEQUE)
72
DOUBLE ENDED QUEUE(DEQUE)
As STACK
When insertion and deletion is made at the same side.
As Queue
When items are inserted at one end and removed at the other
end.
73
Uses of Queue
Simulation of Traffic Control System
Implementation of CPU Scheduling Algorithms
Round Robin Scheduling Algorithm
Multilevel Queue Scheduling Algorithm
Multilevel Feedback Queue Scheduling Algorithm
Areas of Customer Services(Railway Reservation)
Memory Management
Trees traversing
74
Priority Queue
75
Priority Queue
76
Questions
b)Queue
77
Questions
a. Input-restricted deque
b. Output-restricted deque
c. Priority queues
d. None of above
a)Input-restricted Deque
78
Questions
a) Circular Queue
b) Random Queue
c) Priority Queue
d) Deque
d)Deque
79
Questions
a. Strings
b. Lists
c. Queues
[Link] of above
80