• A Queueis a linear data structure in which elements are
inserted from one end (called the rear) and removed from
the other end (called the front).
• It follows the FIFO (First In, First Out) principle, which means
that the element that is added first will be removed first.
• The Queue structure is similar to a real-world queue, such
as a queue in a line at a ticket counter, where the person
who stands first in line is the first to be served.
Introduction
• In asimple queue, elements are added at the rear and
removed from the front, following the FIFO principle.
• Insertion (enqueue) and deletion (dequeue)
operations are performed at opposite ends.
Simple Queue
Enqueue Algorithm
Step 1:Check if the queue is full. If the rear pointer equals
the maximum size (or if rear == size - 1), then the queue is
full, and you cannot add more elements.
Step 2: If there is space, insert the element at the rear of
the queue.
Step 3: Move the rear pointer one position forward.
Step 4: If the rear pointer exceeds the size, no further
insertion is allowed.
9.
Dequeue Algorithm
Step 1:Check if the queue is empty. If the front pointer
equals the rear pointer (or front > rear), the queue is
empty, and you cannot dequeue.
Step 2: If the queue is not empty, remove the element
from the front of the queue.
Step 3: Move the front pointer one position forward.
Step 4: If after dequeuing, the front pointer exceeds the
rear pointer, the queue becomes empty.
10.
• A CircularQueue is the extended version of a
regular queue where the last element is
connected to the first element. Thus forming a
circle-like structure
• In a simple queue, once the queue is full, no new
elements can be added until there is space available.
Circular Queue
Enqueue Algorithm
Step 1:Check if the queue is full. In a circular queue, it is
full if the next position of the rear pointer is equal to the
front pointer (i.e., (rear + 1) % size == front).
Step 2: If the queue is not full, insert the element at the
rear.
Step 3: Move the rear pointer forward, using the formula
(rear + 1) % size to make sure it wraps around when it
reaches the end of the array.
13.
Dequeue Algorithm
Step 1:Check if the queue is empty. If the front equals the
rear, the queue is empty, and you cannot dequeue.
Step 2: If there is an element to remove, remove the
element at the front.
Step 3: Move the front pointer forward, using the formula
(front + 1) % size to ensure the circular behavior.
Step 4: If the front and rear pointers meet after a dequeue
operation, the queue becomes empty.
14.
• In apriority queue, each element is assigned a
priority. Elements are dequeued in order of their
priority rather than their arrival order.
• Higher priority element will get served first as
compared to low priority element.
Priority Queue
Enqueue Algorithm
Step 1:Assign a priority to the new element.
Step 2: Compare the element’s priority with other
elements in the queue.
Step 3: Insert the new element at the correct position
according to its priority (usually in sorted order, based on
priority).
17.
Dequeue Algorithm
Step 1:Identify the element with the highest priority (or
lowest priority, depending on the implementation).
Step 2: Remove the element with the highest (or lowest)
priority from the queue.
Step 3: Adjust the remaining elements in the queue to
maintain the priority order.
18.
• A dequeallows insertion and deletion of elements at
both ends (front and rear).
• There are two types of deques:
• Input-restricted deque: Deletion is allowed from
both ends, but insertion is allowed only at one end.
• Output-restricted deque: Insertion is allowed at both
ends, but deletion is allowed only at one end.
Double-ended Queue
Enqueue Algorithm
Step 1:Check if there is space to insert the element.
Step 2: Depending on the type of deque, either insert the
element at the front or rear.For Input-restricted deques,
insertion is allowed only at the rear.For Output-restricted
deques, insertion is allowed at both ends.
Step 3: Move the corresponding pointer(s) accordingly
(either front or rear).
21.
Dequeue Algorithm
Step 1:Check if the queue is empty. If it is, there is nothing
to dequeue.
Step 2: Depending on the type of deque,either remove the
element from the front or rear.For Input-restricted deques,
removal is allowed only from the front.For Output-
restricted deques, removal is allowed at both ends.
Step 3: Adjust the corresponding pointer (either front or
rear) accordingly.
22.
Enqueue Algorithm :
voidenqueue(int queue[], int& rear, int value, int size) {
if (rear == size - 1) {
cout << "Queue is full!" << endl;
return;
}
queue[rear + 1] = value; // Add value to the queue
rear++; // Move rear pointer
}
23.
Dequeue Algorithm:
int dequeue(intqueue[], int& front, int rear) {
if (front > rear) {
cout << "Queue is empty!" << endl;
return -1; // Indicating empty queue
}
int value = queue[front]; // Get value from the front
front++; // Move front pointer
return value;
}
24.
Enqueue (Insertion)
The processof adding an element to the rear (or end)
of the queue.
Dequeue (Deletion)
The process of removing an element from the front of
the queue.
Operations
25.
Peek (Front)
Returns theelement at the front of the queue without
removing it.
isEmpty
Checks if the queue is empty.
Operations
• In operatingsystems, a queue is used for scheduling
tasks in the CPU, where processes are executed in the
order they arrive.
• Printers often handle print jobs in a queue. The first
job to be received is printed first, following the FIFO
principle.
• Call centers use queues to manage incoming
customer calls. The first call received is the first to be
answered.
Applications
28.
• In abank queue, customers waiting in line are served
based on the order of their arrival, with the first
customer in the queue being the first to be served.
• A ticket booking system where users are in a queue
to buy tickets. The first user to enter the system is the
first to be served.
Applications
29.
• FIFO Principle:Ensures that the first element added
is the first one to be removed.
• Efficient for Scheduling: Great for real-time
processing, such as CPU scheduling, task
management, and handling requests.
• Simple Operations: The basic operations (enqueue
and dequeue) are very straightforward and easy to
implement.
Advantages
30.
• Fixed Size:In a static queue (simple queue), the size
is fixed, meaning that if the queue is full, no new
elements can be added.
• Limited Access: In a queue, only the front element
can be accessed (dequeued), and there’s no direct
access to other elements like in an array or linked list.
Disadvantages
31.
• Queues area fundamental data structure in
computer science that allows efficient management
of elements in a FIFO manner.
• They are widely used in many real-world applications,
such as task scheduling, request handling, process
management, and network packet management.
Conclusion