CPEDSA 222
QUEUES
Queue
• A Queue Data Structure is a fundamental concept in
computer science used for storing and managing data
in a specific order.
• It follows the principle of “First in, First out” (FIFO),
where the first element added to the queue is the first
one to be removed.
• Queues are commonly used in various algorithms and
applications for their simplicity and efficiency in
managing data flow.
What is Queue in data structures?
• A queue is a linear data structure that follows the First-
In-First-Out (FIFO) principle.
• It operates like a line where elements are added at one
end (rear) and removed from the other end (front).
Applications of queue data structure
• Task scheduling in operating systems
• Data transfer in network communication
• Simulation of real-world systems (e.g., waiting lines)
• Priority queues for event processing queues for event
processing
Basic Operations of a Queue data
structure
• enqueue() – Insertion of elements to the queue.
• dequeue() – Removal of elements from the queue.
• peek() or front()- Acquires the data element available at
the front node of the queue without deleting it.
• rear() – This operation returns the element at the rear
end without removing it.
• isFull() – Validates if the queue is full.
• isEmpty() – Checks if the queue is empty.
• size(): This operation returns the size of the queue i.e.
the total number of elements it contains.
enqueue()
• The following steps should be taken to enqueue (insert)
data into a queue:
• Check if the queue is full.
• If the queue is full, return overflow error and exit.
• If the queue is not full, increment the rear pointer to point to
the next empty space.
• Add the data element to the queue location, where the rear is
pointing.
• return success.
dequeue()
• The following steps are taken to perform the dequeue
operation:
• Check if the queue is empty.
• If the queue is empty, return the underflow error and exit.
• If the queue is not empty, access the data where the front is
pointing.
• Increment the front pointer to point to the next available data
element.
• The Return success.
front()
• This operation returns the element at the front end
without removing it.
• The following steps are taken to perform the front
operation:
• If the queue is empty return the most minimum value.
• otherwise, return the front value.
rear()
• This operation returns the element at the rear end
without removing it.
• The following steps are taken to perform the rear
operation:
• If the queue is empty return the most minimum value.
• otherwise, return the rear value.
isEmpty()
• This operation returns a boolean value that indicates
whether the queue is empty or not.
• The following steps are taken to perform the Empty
operation:
• check if front value is equal to -1 or not, if yes then return true
means queue is empty.
• Otherwise return false, means queue is not empty
isFull()
• This operation returns a boolean value that indicates
whether the queue is full or not.
• The following steps are taken to perform the isFull()
operation:
• Check if front value is equal to zero and rear is equal to the
capacity of queue if yes then return true.
• otherwise return false
size()
• This operation returns the size of the queue i.e. the total
number of elements it contains.
Types of Queues
1.Input Restricted Queue (this is a Simple Queue)
2.Output Restricted Queue (this is also a Simple Queue)
3.Circular Queue
4.Double Ended Queue (Deque)
5.Priority Queue
1.Ascending Priority Queue
2.Descending Priority Queue
Input Restricted Queue
• In this type of Queue, the input can be taken from one
side only(rear) and deletion of elements can be done
from both sides(front and rear).
• This kind of Queue does not follow FIFO(first in first out).
This queue is used in cases where the consumption of
the data needs to be in FIFO order but if there is a need
to remove the recently inserted data for some reason
and one such case can be irrelevant data, performance
issue, etc.
Input Restricted Queue
Advantages of Input restricted Queue:
• Prevents overflow and overloading of the queue by limiting
the number of items added
• Helps maintain stability and predictable performance of the
system
Disadvantages of Input restricted Queue:
• May lead to resource wastage if the restriction is set too low
and items are frequently discarded
• May lead to waiting or blocking if the restriction is set too high
and the queue is full, preventing new items from being added.
Output Restricted Queue
• In this type of Queue, the input can be taken from both
sides(rear and front) and the deletion of the element
can be done from only one side(front).
• This queue is used in the case where the inputs have
some priority order to be executed and the input can be
placed even in the first place so that it is executed first.
Circular Queue
• Circular Queue is a linear data
structure in which the
operations are performed
based on FIFO (First In First
Out) principle and the last
position is connected back to
the first position to make a
circle.
• It is also called ‘Ring Buffer’.
Circular Queue
This queue is primarily used in the following cases:
1.Memory Management: The unused memory
locations in the case of ordinary queues can be utilized
in circular queues.
2.Traffic system: In a computer-controlled traffic
system, circular queues are used to switch on the traffic
lights one by one repeatedly as per the time set.
3.CPU Scheduling: Operating systems often maintain a
queue of processes that are ready to execute or that are
waiting for a particular event to occur.
Double Ended Queue
• Double Ended Queue is also a Queue data structure in
which the insertion and deletion operations are performed
at both the ends (front and rear).
• That means, we can insert at both front and rear positions
and can delete from both front and rear positions. Since
Deque supports both stack and queue operations, it can
be used as both. The Deque data structure supports
clockwise and anticlockwise rotations in O(1) time which
can be useful in certain applications. Also, the problems
where elements need to be removed and or added both
ends can be efficiently solved using Deque.
Double Ended Queue
• Double Ended Queue is also a Queue data structure in
which the insertion and deletion operations are
performed at both the ends (front and rear).
• That means, we can insert at both front and rear
positions and can delete from both front and rear
positions.
Double Ended Queue
• The Deque data structure supports clockwise and
anticlockwise rotations
• Also, the problems where elements need to be removed
and or added both ends can be efficiently solved using
Deque.
• Since Deque supports both stack and queue operations,
it can be used as both.
Priority Queue
• A priority queue is a type of queue that arranges
elements based on their priority values.
• Elements with higher priority values are typically
retrieved or removed before elements with lower
priority values.
• Each element has a priority value associated with it.
• When we add an item, it is inserted in a position based
on its priority value.
Priority Queues
• Priority queues are often used in real-time systems,
where the order in which elements are processed is not
simply based on the fact who came first (or inserted
first), but based on priority.
• Priority Queue is used in algorithms such as
• Dijkstra’s algorithm
• Prim’s algorithm
• Kruskal’s algorithm
• Huffnam Coding
Properties of a Priority Queue
• So, a priority Queue is an extension of
the queue with the following properties.
• Every item has a priority associated with it.
• An element with high priority is dequeued before an
element with low priority.
• If two elements have the same priority, they are served
according to their order in the queue.
Type of Priority Queues
1.Ascending Priority Queue: Element can be inserted
arbitrarily but only smallest element can be removed. For
example, suppose there is an array having elements 4, 2, 8 in
the same order. So, while inserting the elements, the
insertion will be in the same sequence but while deleting, the
order will be 2, 4, 8.
2.Descending priority Queue: Element can be inserted
arbitrarily but only the largest element can be removed first
from the given Queue. For example, suppose there is an
array having elements 4, 2, 8 in the same order. So, while
inserting the elements, the insertion will be in the same
sequence but while deleting, the order will be 8, 4, 2.
• In the below priority queue, an element with a maximum ASCII
value will have the highest priority. The elements with higher
priority are served first.