Queue is a linear data structure which enables the user to add elements from the rear end and remove elements from the front end only, using the concept of FIFO (First In First Out).

**Note:** Since insertion and deletion both takes place from different ends, we have to keep record of the index of both the front as well as the rear end.

Consider a queue implemented using an array Q[ ]. The index of the front (first) element is stored in the variable ‘front’ and index of the last element is stored in the variable ‘rear’.

Let the size of the queue be denoted by the variable ‘size’. In our example, the value of ‘size’ = 5.

Initially the values of ‘front’ and ‘rear’ are 0. So insertion will start from index zero.

**Inserting element in the queue**

Since insertion in a queue takes place from the rear end, hence, the first element which you insert in the queue must go to the rear end which is at index ‘0’ after which the value of ‘rear’ should increase from 0 to 1 to accommodate the next element.

Similarly, when we insert the next element, after insertion, the value of ‘rear’ should increase from 1 to 2 and so on.

Now when the last element is inserted at index 4, the value of ‘rear’ changes from 4 to 5. We now see that the queue is full and no more element can be inserted. This situation is known as ‘OVERFLOW’.

While inserting element in the queue, always remember to check for a condition where the queue is full and no more element can be inserted.

So, **if(rear == size)** then OVERFLOW will occur and you cannot insert any more element. In all other cases you can insert, by first inserting at the rear end and then increasing the ‘rear’ index.

**To cut the long story short, while inserting, ‘rear’ index increases.**

**Programming Code Implementing Insertion Operation:**

void insert(int v) // Function to insert element in Queue { if(rear == size) // Condition for Overflow { System.out.println("OVERFLOW"); } else { Q[rear] = v; // Storing value in Queue rear = rear + 1; } }

**Deleting element from the queue**

The first element which you delete will be from the ‘front’ index (following the FIFO pattern). Before deleting, save the element to be deleted and print (or return it, as asked in the question). After that, the value of ‘front’ index should increase from 0 to 1.

Similarly, when we delete the next element, the value of ‘front’ should increase from 1 to 2 and so on.

Now, when the value of ‘front’ and ‘rear’ becomes equal, there will be no element in the queue. So, we re-set their values to 0 to denote that the queue is now in its initial state (empty). So now no more element can be deleted. This situation is known as ‘UNDERFLOW’.

While deleting element from the queue, always remember to check for a condition where the queue is empty and no more element can be deleted.

So, **if(front == 0 && rear == 0)** then UNDERFLOW will occur and you cannot delete any more element. In all other cases you can delete, by increasing ‘front’.

**To cut the long story short, while deleting, ‘front’ index increases.**

**Programming Code Implementing Deletion Operation:**

int delete() // Function to delete element from Queue { if(front == 0 && rear == 0) // Condition for Underflow { System.out.println("UNDERFLOW"); return -999; } else { int val = Q[front]; // Storing the element which will be removed front = front + 1; if(front == rear) // Condition for emptying the Queue { front = 0; rear = 0; } return val; } }

#### Complete Java Program implementing Operations on Queue:

/** * The class Queue implements operations of queue using Java * @author : www.guideforschool.com * @Program Type : BlueJ Program - Java */ class Queue { int Q[]; // Array to implement Queue int size; // Maximum size of the Queue int front; // Index of front element int rear; // Index of rear element Queue(int cap) // Parameterised Constructor { size = cap; Q = new int[size]; front = 0; rear = 0; } void insert(int v) // Function to insert element in Queue { if(rear == size) // Condition for Overflow { System.out.println("OVERFLOW"); } else { Q[rear] = v; // Storing value in Queue rear = rear + 1; } } int delete() // Function to delete element from Queue { if(front == 0 && rear == 0) // Condition for Underflow { System.out.println("UNDERFLOW"); return -999; } else { int val = Q[front]; // Storing the element which will be removed front = front + 1; if(front == rear) // Condition for emptying the Queue { front = 0; rear = 0; } return val; } } void display() // Function for printing elements in the queue { if(front == 0 && rear == 0) { System.out.println("The Queue is empty"); } else { System.out.println("The elements in the queue are : "); for(int i=front; i<rear; i++) { System.out.println(Q[i]); } } } }

Sir I want to know about an array program of sorting elements by keeping the highest element in the middle{for example:entering elements in an array :1 3 9 5 7 8 2 output: 1 3 5 9 8 7 2

thank you sir for your efforts.It helped me in grasping the concept. Sir what is circular queue?