Circular Queue in Java

Circular Queue in Java

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. In this article we will discuss Circular Queue in java and how can we overcome some of the limitations of normal queue using circular queue.

Circular Queue in java

Why Circular Queue?

A Queue is a linear data structure which follows First In First Out (FIFO) principle. But there are some limitations in normal queue. If the rear reaches to the end of the queue then there might be possibility that some vacant spaces are left in the beginning which cannot be utilized. So , to overcome such limitations , the concept of the circular queue was introduced.

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 at the rear end.
  • deQueue() : This function deletes an element from the queue. The deletion in queue always takes place from the front end.

Example of a Circular Queue

Circular Queue Example in java
Steps for performing enQueue and deQueue operation in Circular Queue:
  1. Initially queue has a single value 1 and front and rear are  set to 1. Then insert the value 2 after incrementing the rear.
  2. Similarly insert the value 3 by incrementing the rear.
  3. Again insert the value 4 by incrementing the rear.
  4.  Again insert the value 5 by incrementing the rear.
  5. Now our queue becomes full so delete the element from the front and increment the front .So our front will set at value 2.
  6. Now Again insert the value 6 by incrementing the rear.
 

Implementation of Circular Queue in Java

import java.util.*;
public class CircularQueue
{
private int size, front, rear; //Variable declaration

private ArrayList < Integer > queue = new ArrayList < Integer > (); //Declaring Integer array list

CircularQueue(int size) //Constructor
{
this.size = size;
this.front = this.rear = -1;
}

public void enQueue (int value) //Insertion Function
{
if ((front == 0 && rear == size - 1) || (rear == (front - 1) % (size - 1))) // Condition if queue is full
{
System.out.print ("Queue Full!");
}
else if (front == -1) // Condition for empty queue.
{
front = 0;
rear = 0;
queue.add(rear,value);
}
else if (rear == size - 1 && front != 0)
{
rear = 0;
queue.set(rear,value);
}
else
{
rear = (rear + 1);
// Adding a new element if
if (front <= rear)
{
queue.add(rear, value);
}
// Else updating old value
else
{
queue.set(rear, value);
}
}
}

public int deQueue () //Dequeue Function
{
int temp;

if (front == -1) //Checking for empty queue
{
System.out.print ("Queue Empty!");
return -1;
}

temp =queue.get (front);

if (front == rear) // For only one element
{
front = -1;
rear = -1;
}
else if (front == size - 1)
{
front = 0;
}
else
{
front = front + 1;
}
return temp; // Returns dequeued element
}

public void displayQueue () // Display the elements of queue
{
if (front == -1) // Check for empty queue
{
System.out.print ("Queue is Empty");
return;
}
System.out.print ("Elements in the " + "circular queue are: ");

if (rear >= front) //if rear has not crossed the size limit
{
for (int i = front; i <= rear; i++) //print elements using loop
{
System.out.print (queue.get (i));
System.out.print (" ");
}
System.out.println ();
}
else
{
for (int i = front; i < size; i++)
{
System.out.print (queue.get (i));
System.out.print (" ");
}
for (int i = 0; i <= rear; i++) // Loop for printing elements from 0th index till rear position
{
System.out.print (queue.get(i));
System.out.print (" ");
}
System.out.println ();
}
}

public static void main (String[]args)
{
CircularQueue queue = new CircularQueue (5); // Initialising new object of CircularQueue class.

queue.enQueue (1);
queue.enQueue (2);
queue.enQueue (3);
queue.enQueue (4);
queue.enQueue (5);
queue.displayQueue ();

int x = queue.deQueue ();

if (x != -1) // Check for empty queue
{
System.out.print ("Deleted value = ");
System.out.println (x);
}
x = queue.deQueue ();
if (x != -1) // Check for empty queue
{
System.out.print ("Deleted value = ");
System.out.println (x);
}

queue.displayQueue ();
queue.enQueue (6);
queue.enQueue (7);
queue.displayQueue ();
queue.enQueue (8);
}
}

Output:

Elements in the circular queue are: 1 2 3 4 5
Deleted value = 1
Deleted value = 2
Elements in the circular queue are: 3 4 5
Elements in the circular queue are: 3 4 5 6 7
Queue Full!