Friday , 24 March 2017
Home » Class 12 » Operation on Queue using Java

Operation on Queue using Java

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.

queue insertion

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.

queue deletion

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]);
            }
        }
    }
}            

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

3 comments

  1. 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

    • /**
      * The class Pendulum_Array inputs a set of integers in an Array and arranges them in Pendulum Fashion
      * @author : www.guideforschool.com
      * @Program Type : BlueJ Program - Java
      */
      
      import java.io.*;
      class Pendulum_Array
      {
          public static void main(String args[])throws IOException
          {
              BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
              System.out.print("nEnter number of elements: "); // Inputting the number of elements
              int n = Integer.parseInt(br.readLine());
      
              int A[]=new int[n]; //original array
              int B[]=new int[n]; //array for storing the result
              
              /*Inputting the Array*/
              for(int i=0; i<n; i++)
              {
                  System.out.print("Enter Element "+(i+1)+": ");
                  A[i] = Integer.parseInt(br.readLine());
              }
              
              /*Sorting the Inputted Array in Descending Order*/
              int t=0;
              for(int i=0; i<n-1; i++)
              {
                  for(int j=i+1; j<n; j++)
                  {
                      if(A[i]<A[j]) // Note: We have changed the sign to '<' here
                      {
                          t=A[i];
                          A[i]=A[j];
                          A[j]=t;
                      }
                  }
              }
              
              /*Printing the Sorted Array*/
              System.out.println("\nThe Sorted Array Is");
              for(int i=0; i<n; i++)
              {
                  System.out.print(A[i]+"\t");
              }
              
               int mid = (n-1)/2; //finding index of middle cell
               int x = 1, lim = n-1-mid;
               /*'x' is for accessing elements of array A[] and
               'lim' is for the number of times we have to make this to-and-fro movement*/
               
               /* Pendulum Arrangement Starts Here */
               B[mid]=A[0]; //putting the maximum element in the middle cell
      
               for(int i=1; i<=lim; i++)
               {
      
                 /* Note: Below we are first going to the left side then to the right,
                    just the opposite of the above code */
      
                   if((mid-i)>=0)
                      B[mid-i]=A[x++];
                   if((mid+i)<n)
                      B[mid+i]=A[x++];
               }
               
              /*Printing the Result*/
              System.out.println("\n\nThe Result Is");
              for(int i=0; i<n; i++)
              {
                  System.out.print(B[i]+"\t");
              }
          }
      }
  2. thank you sir for your efforts.It helped me in grasping the concept. Sir what is circular queue?

Leave a Reply to Rishika Cancel reply

Your email address will not be published. Required fields are marked *