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

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.
Operation in Circular Queue

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

Run
import java.util.*;

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 class Main
    {
        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!

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java