In C programming, a queue is a linear data structure that follows the FIFO (First In, First
Out) principle, meaning the element added first will be the first to be removed.
Basic Operations of a Queue:
1. Enqueue: Add an element to the rear of the queue.
2. Dequeue: Remove an element from the front of the queue.
3. Front: Get the element at the front of the queue without removing it.
4. Rear: Get the element at the rear of the queue without removing it.
5. isEmpty: Check if the queue is empty.
6. isFull: Check if the queue is full.
Queue Data Structure
A queue is a useful data structure in programming. It is similar to the ticket
queue outside a cinema hall, where the first person entering the queue is
the first person who gets the ticket.
Queue follows the First In First Out (FIFO) rule - the item that goes in first
is the item that comes out first.
FIFO Representation of Queue
In the above image, since 1 was kept in the queue before 2, it is the first to
be removed from the queue as well. It follows the FIFO rule.
In programming terms, putting items in the queue is called enqueue, and
removing items from the queue is called dequeue.
We can implement the queue in any programming language like C, C++,
Java, Python or C#, but the specification is pretty much the same.
Basic Operations of Queue
A queue is an object (an abstract data structure - ADT) that allows the
following operations:
Enqueue: Add an element to the end of the queue
Dequeue: Remove an element from the front of the queue
IsEmpty: Check if the queue is empty
IsFull: Check if the queue is full
Peek: Get the value of the front of the queue without removing it
Working of Queue
Queue operations work as follows:
two pointers FRONT and REAR
FRONT track the first element of the queue
REAR track the last element of the queue
initially, set value of FRONT and REAR to -1
Enqueue Operation
check if the queue is full
for the first element, set the value of FRONT to 0
increase the REAR index by 1
add the new element in the position pointed to by REAR
Dequeue Operation
check if the queue is empty
return the value pointed by FRONT
increase the FRONT index by 1
for the last element, reset the values of FRONT and REAR to -1
Types of Queues
A queue is a useful data structure in programming. It is similar to the ticket
queue outside a cinema hall, where the first person entering the queue is
the first person who gets the ticket.
There are four different types of queues:
Simple Queue
Circular Queue
Priority Queue
Double Ended Queue
Simple Queue
In a simple queue, insertion takes place at the rear and removal occurs at
the front. It strictly follows the FIFO (First in First out) rule.
Simple Queue Representation
Circular Queue
In a circular queue, the last element points to the first element making a
circular link.
Circular Queue
Representation
The main advantage of a circular queue over a simple queue is better
memory utilization. If the last position is full and the first position is empty,
we can insert an element in the first position. This action is not possible in a
simple queue.
Priority Queue
A priority queue is a special type of queue in which each element is
associated with a priority and is served according to its priority. If elements
with the same priority occur, they are served according to their order in the
queue.
Priority Queue
Representation
Insertion occurs based on the arrival of the values and removal occurs
based on priority.
Deque (Double Ended Queue)
In a double ended queue, insertion and removal of elements can be
performed from either from the front or rear. Thus, it does not follow the
FIFO (First In First Out) rule.
Circular Queue Data Structure
A circular queue is the extended version of a regular queue where the last
element is connected to the first element. Thus forming a circle-like
structure.
Circular queue representation
The circular queue solves the major limitation of the normal queue. In a
normal queue, after a bit of insertion and deletion, there will be non-usable
empty space.
Limitation of the regular Queue
Here, indexes 0 and 1 can only be used after resetting the queue (deletion
of all elements). This reduces the actual size of the queue.
How Circular Queue Works
Circular Queue works by the process of circular increment i.e. when we try
to increment the pointer and we reach the end of the queue, we start from
the beginning of the queue.
Here, the circular increment is performed by modulo division with the queue
size. That is,
if REAR + 1 == 5 (overflow!), REAR = (REAR + 1)%5 = 0 (start of queue)
Circular Queue Operations
The circular queue work as follows:
two pointers FRONT and REAR
FRONT track the first element of the queue
REAR track the last elements of the queue
initially, set value of FRONT and REAR to -1
1. Enqueue Operation
check if the queue is full
for the first element, set value of FRONT to 0
circularly increase the REAR index by 1 (i.e. if the rear reaches the end, next
it would be at the start of the queue)
add the new element in the position pointed to by REAR
2. Dequeue Operation
check if the queue is empty
return the value pointed by FRONT
circularly increase the FRONT index by 1
for the last element, reset the values of FRONT and REAR to -1
However, the check for full queue has a new additional case:
Case 1: FRONT = 0 && REAR == SIZE - 1
Case 2: FRONT = REAR + 1
The second case happens when REAR starts from 0 due to circular
increment and when its value is just 1 less than FRONT , the queue is full.