Implementation of Circular Queue using Arrays in java

circular queue using array

Implementation of Circular Queue using Arrays 

Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position. 

Why we need circular queue ??

When we work with normal Queue, we can insert elements till queue is full. But when queue is full, we are not allowed to insert the next element this is the problem with normal queue . To overcome this problem circular queue is used .

                                                     Operation In Circular Queue:

The following are the operations that can be performed on a circular queue:

  • Front: It is used to get the front item from the Queue.
  • Rear: It is used to get the last element from the Queue.
  • enQueue(value): This function is used to insert the new value in the Queue. The new element is always inserted from the rear end.
  • deQueue(): This function deletes an element from the Queue. The deletion in a Queue always takes place from the front end.

Algorithm for Insertion:

  • START
  • INITIALLY PUT FRONT AND REAR BOTH AS 0
  • IN ENQUEUE FUNCTION WRITE 

           public static void enqueue(int item)

 
                      {
                                     if((rear+1) % n != front)
 
                           {
                                  rear=(rear+1)%n;
 
                                Queue[rear] = item;
                          }
                    else
 
                       {
                         System.out.println(“No Insertion-Queue is full !”);
                           }
              }
  • END 
CIRCULAR QUEUE INSERTION

Algorithm for Deletion:

  • START
  • WRITE dequeue function as 
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 “);
}
}
  • END
CIRCULAR QUEUE DELETION

Java code:

import java.util.*;

  public class prepinsta
{
  
   int Queue[] = new int[100];
    
      int nfrontrear;
  
    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(” No Insertion -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 “);
    }
  }
  
    public static void display()
  
  {
      int j;
    
       if(front != rear)
    {
      
        for(j=(front+1)%n ; j<rear ; j=(j+1)%n)
      
      {
      
          System.out.println(Queue[j]);
      }
    
    }
    
       else
      
         System.out.println(“Queue is empty cant display!”);
  }
  
    public static void main(String args[]) 
  
  {
  
     System.out.print(“Size of queue : “);
   
       Scanner sc = new Scanner (System.in);
     
         int size = sc.nextInt();
    
     CircularQueue cq = new CircularQueue(size);
     System.out.println(” element in queue are  “);
       cq.enqueue(20);
       cq.enqueue(40);
       cq.enqueue(60);
       cq.enqueue(80);
       cq.display();
       int data = cq.dequeue();
       System.out.println(” element delete is “+data);
       System.out.println(” element in queue after deletion  “);
       cq.display();


    
  }
}
Output:

elements in queue are

20

40

60

80

element deleted is 20

elements in queue after deletion 

40

60

80