What is a Queue
What is a Queue
Queue is a linear data structure in which the insertion and deletion operations are
performed at two different ends. In a queue data structure, adding and removing
elements are performed at two different positions. The insertion is performed at one
end and deletion is performed at another end. In a queue data structure, the
insertion operation is performed at a position which is known as 'rear' and the
deletion operation is performed at a position which is known as 'front'. In queue
data structure, the insertion and deletion operations are performed based on FIFO
(First In First Out) principle.
Example
Queue after inserting 25, 30, 51, 60 and 85.
Operations on a Queue
The following operations are performed on a queue data structure...
1. Using Array
2. Using Linked List
When a queue is implemented using an array, that queue can organize an only
limited number of elements. When a queue is implemented using a linked list, that
queue can organize an unlimited number of elements.
implemented using array stores only fixed number of data values. The
implementation of queue data structure using array is very simple. Just define a one
dimensional array of specific size and insert or delete the values into that array by
using FIFO (First In First Out) principle with the help of variables 'front' and
'rear'. Initially both 'front' and 'rear' are set to -1. Whenever, we want to insert a
new value into the queue, increment 'rear' value by one and then insert at that
position. Whenever we want to delete a value from the queue, then delete the
Before we implement actual operations, first follow the below steps to create an
empty queue.
Step 1 - Include all the header files which are used in the program and
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
and make suitable function calls to perform operation selected by the user on
queue.
In a queue data structure, enQueue() is a function used to insert a new element into
the queue. In a queue, the new element is always inserted at rear position. The
enQueue() function takes one integer value as a parameter and inserts that value
into the queue. We can use the following steps to insert an element into the queue...
Step 3 - If it is NOT FULL, then increment rear value by one (rear++) and
the queue. In a queue, the element is always deleted from front position. The
deQueue() function does not take any value as parameter. We can use the following
both front and rear are equal (front == rear), if it TRUE, then set
the function.
Step 3 - If it is NOT EMPTY, then define an integer variable 'i' and set
'i = front+1'.
Step 4 - Display 'queue[i]' value and increment 'i' value by one (i++).
Repeat the same until 'i' value reaches to rear (i <= rear)