1E
1E
Pre-requisite:
• Basics of Queue
• Different operations that can be performed on queue
Objective:
• To perform addition and deletion operations on queue.
Input:
Size of queue
Elements in
quque
Outcome:
• Result of addition of job operation on queue.
• Result of deletion of job operation on queue.
Theory :
What is a Queue?
Queue is the data structure that is similar to the queue in the real world. A queue is a data structure
in which whatever comes first will go out first, and it follows the FIFO (First-In-First-Out) policy.
Queue can also be defined as the list or collection in which the insertion is done from one end known
as the rear end or the tail of the queue, whereas the deletion is done from another end known as
the front end or the head of the queue.
The real-world example of a queue is the ticket queue outside a cinema hall, where the person who
enters first in the queue gets the ticket first, and the last person enters in the queue gets the ticket
at last. Similar approach is followed in the queue in data structure.
The representation of the queue is shown in the below image -
Types of Queue
There are four different types of queue that are listed as follows -
In Linear Queue, an insertion takes place from one end while the deletion occurs from another end.
The end at which the insertion takes place is known as the rear end, and the end at which the deletion
takes place is known as front end. It strictly follows the FIFO rule.
The major drawback of using a linear Queue is that insertion is done only from the rear end. If the
first three elements are deleted from the Queue, we cannot insert more elements even though the
space is available in a Linear Queue. In this case, the linear Queue shows the overflow condition as
the rear is pointing to the last element of the Queue.
Circular Queue
In Circular Queue, all the nodes are represented as circular. It is similar to the linear Queue except
that the last element of the queue is connected to the first element. It is also known as Ring Buffer,
as all the ends are connected to another end. The representation of circular queue is shown in the
below image -
The drawback that occurs in a linear queue is overcome by using the circular queue. If the empty
space is available in a circular queue, the new element can be added in an empty space by simply
incrementing the value of rear. The main advantage of using the circular queue is better memory
utilization.
To know more about the circular queue, you can click the link - https://www.javatpoint.com/circular-
queue
Priority Queue
It is a special type of queue in which the elements are arranged based on the priority. It is a special
type of queue data structure in which every element has a priority associated with it. Suppose some
elements occur with the same priority, they will be arranged according to the FIFO principle. The
representation of priority queue is shown in the below image -
Insertion in priority queue takes place based on the arrival, while deletion in the priority queue occurs
based on the priority. Priority queue is mainly used to implement the CPU scheduling algorithms.
There are two types of priority queue that are discussed as follows -
o Ascending priority queue - In ascending priority queue, elements can be inserted in arbitrary
order, but only smallest can be deleted first. Suppose an array with elements 7, 5, and 3 in the
same order, so, insertion can be done with the same sequence, but the order of deleting the
elements is 3, 5, 7.
o Descending priority queue - In descending priority queue, elements can be inserted in arbitrary
order, but only the largest element can be deleted first. Suppose an array with elements 7, 3,
and 5 in the same order, so, insertion can be done with the same sequence, but the order of
deleting the elements is 7, 5, 3.
Algorithms:
Step 1: Include all the header files which are used in the program and define a constant
'SIZE' with specific value.
Step 2: Declare all the user defined functions which are used in queue implementation.
Step 3: Create a one dimensional array with above defined SIZE (int queue[SIZE])
Step 4: Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int front = -
1, rear
= -1)
Step 5: Then implement main method by displaying menu of operations list and
make suitable function calls to perform operation selected by the user on queue.
enQueue(value) - Inserting value into the queue: