Circular Queue in Java with Examples – CodeSpeedy

Circular Queue – Array Implementation in Java

In this Java tutorial, we are going to discuss the circular queue and its array implementation in java.

What is Circular Queue?

Circular Queue is a linear data structure in which operations are performed on FIFO ( First In First Out ) basis . Element at last position is connected to front element in circular queue .

circular queue in Javacircular queue in Java

In linear queue we can insert elements till the size of the queue is not fully occupied after that we cannot insert new element even if there is space in the front position.

In a circular queue if the queue becomes full and if there is a vacant position in front then the new element is added at the front. Items can be inserted and deleted from Queue in O(1) time.

Operations on Circular Queue

1.) Front:- Get the front item from the queue.

2.) Rear:- Get the last item from the queue.

3.) enqueue(item):- This function is used to insert an element with value item in the queue.

4.) dequeue():- This function is used to remove an element from the front of the queue.

Code Snippet to enqueue an element in queue

  if((rear+1) % n != front)

{
   rear = (rear+1)%n;
    
     Queue[rear] = item;
}

Code Snippet to dequeue an element from queue

int item;
  if(front!=rear)
{
  front = (front+1)%n;
   item = Q[front];
  return item;
}

Also read,

Java code on operations on Circular Queue

import java.util.Scanner;

  public class Codespeedy
{
  
   int Queue[] = new int[50];
    
      int n, front, rear;
  
    public CircularQueue(int size)
  
  {
    n=size;
    
      front = 0;
    
         rear=0;
  }
  
    public static void enqueue(int item)
  
  {
      if((rear+1) % n != front)
    
     {
        rear = (rear+1)%n;
        
           Queue[rear] = item;
     }
      else
     
    {
        System.out.println("Queue is full !");
    }
  }
  
    public static int dequeue()
  
  {
    int item;
  
    	if(front!=rear)
    {
      front = (front+1)%n;
      
       item = Queue[front];
      
       return item;
    }

      else
    {
      
         System.out.println("Can't remove element from queue");
    }
  }
  
    public static void display()
  
  {
      int i;
    
       if(front != rear)
    {
      
        for(i=(front+1)%n ; i<rear ; i=(i+1)%n)
      
      {
      
          System.out.println(Queue[i]);
      }
    
    }
    
       else
      
         System.out.println("Queue is empty !");
  }
  
    public static void main(String args[]) 
  
  {
  
     System.out.print("Enter the size of the queue : ");
   
       Scanner scan = new Scanner (System.in);
     
         int size = scan.nextInt();
    
     CircularQueue cqueue = new CircularQueue(size);
    
         int x;
     
       int flag=1;
    
       while(flag)
    
    {
    
       System.out.print("\n 1 : Add\n 2 : Delete\n 3 : Display\n 4 : Exit\n\n\n\n Enter Choice : ");
     
         x = scan.nextInt();
       
          switch(x)
      
       {
       
           case 1 :
          
                     System.out.print("\n Enter element u want to add in queue: ");
          
                     int num = scan.nextInt();
          
                     cqueue.enqueue(num);
          
                       break;
        
           case 2 :
          
                     int data = cqueue.dequeue();
           
                     System.out.println("\n Item deleted from queue is : " + data);
          
                       break;
        
          case 3 :
          
                     cqueue.display();
          
                      break;
        
         case 4 :
         
                    flag=0;
          
                      break;
        
        default :
        
                   System.out.println("\n Wrong Choice !");
         
                      break;
      }
    }
  }
}

Application of Circular Queue

Circular Queue has application in CPU Scheduling , Memory management  and in Traffic System .