queue
queue
Out) principle, meaning the element added first will be the first to be removed.
Queue follows the First In First Out (FIFO) rule - the item that goes in first
is the item that comes out first.
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:
Working of Queue
Queue operations work as follows:
Dequeue Operation
check if the queue is empty
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
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.
Circular Queue
In a circular queue, the last element points to the first element making a
circular link.
Circular Queue
Representation
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.
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.
Here, the circular increment is performed by modulo division with the queue
size. That is,
2. Dequeue Operation
check if the queue is empty
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.