# Circular queue Java  Written by Nilima PaulJanuary 3, 2022
java
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE Nilima Paul

Technology Security Analyst

## Circular queue Java

The following article provides an outline for Circular queue Java. A circular queue is a linear data structure and the operations are performed in a FIFO (First In First Out) manner just like the simple Queue. In the Circular queue, the last position is connected to the first position making a circle. It is also called the Ring Buffer. In the simple Queue structure, if the rear reaches the end of the queue, i.e. the queue becomes full, there are chances that the spaces at the beginning elements are empty which can’t be utilized. This limitation of the queue is resolved by the CIrcular queue. In this topic, we are going to learn about Circular queue Java.

## How to create a Circular queue in Java along with its working?

Let us understand the creation and working of Circular queue in detail:

Basic operations that are performed on the CIrcular Queue are given below:

1. Front: It is used to point to the first element of the queue.
2. Rear: It is used to point to the last element of the queue.
3. Enqueue(value): It is a function that is used to insert the element on the queue. Elements in the Queue are inserted at the rear position.
4. Dequeue(): This function is used to delete the element of the Queue. As the basic nature of the Queue, deletion of elements takes place from the front of the Queue.

Below given are the steps followed while creating, inserting, or deleting the elements in the Circular Queue:

• Initially, the value of the Front and Rear is set to -1.
• In Enqueue (value) operation:
• If we insert the first element in the Queue, both the Front and Rear are set to 0.
• On inserting the new element in the queue, the rear becomes rear + 1.
• The new element is added at the position of the rear.
• The rear is incremented in a circular way, i.e. if it reaches the end and the queue becomes full, it points to the start of the queue.
• In Dequeue Operation:
• First, it is checked whether the queue is empty or not. Mean of the front== rear== -1, means the queue is empty, deletion cannot be performed.
• If the queue has only 1 element, i.e. front == rear, then delete the element and set front == rear == -1.
• If there are elements in the queue, the value pointed out by front is deleted and the circularly increases the front index by 1.

## Examples

Below given is the example showing the implementation of Circular Queue in the Java program:

• Inserting elements in the Queue
• Displaying items of the empty queue
• Inserting elements in the Queue
• Removing elements from the Queue
• Inserting elements in a full Queue
• Displaying elements of the Queue along with the position of front and rear.
`public class CirQueue {`
`// Defining the size of Circular Queue`
`int SIZE = 5;`
`int front, rear;`
`int queue[] = new int[SIZE];`
`//creating the constructor of the above class`
`CirQueue() {`
`front = -1;`
`rear = -1;`
`}`
`// Implementing the 2 scenarios to check if the queue is full or not`
`boolean isFullQueue() {`
`if (front == 0 && rear == SIZE - 1) {`
`return true;`
`}`
`if (front == rear + 1) {`
`return true;`
`}`
`return false;`
`}`
`// Check if the queue is empty or not`
`boolean isEmptyQueue() {`
`if (front == -1)`
`return true;`
`else`
`return false;`
`}`
`// Adding an element in the queue`
`void enQueue(int value) {`
`if (isFullQueue()) {`
`System.out.println("Sorry !! Queue is full.. No more elements could be inserted in it");`
`}`
`else {`
`// if there is no element in the queue`
`if (front == -1)`
`front = 0;`
`// incrementing the rear position in circular manner using modulo operator`
`rear = (rear + 1) % SIZE;`
`//placing the value at the rear position`
`queue[rear] = value;`
`System.out.println("Element " + value + " is inserted successfully");`
`}`
`}`
`// Deleting the element from the queue`
`void deQueue() {`
`int value;`
`// checking of the queue is empty or not`
`if (isEmptyQueue()) {`
`System.out.println("Sorry !!The Queue is empty.. ");`
`} else {`
`value = queue[front];`
`// if there is only one element in the queue`
`if (front == rear) {`
`front = -1;`
`rear = -1;`
`}`
`else {`
`// Incrementing the front in a circular manner`
`front = (front + 1) % SIZE;`
`}`
`}`
`}`
`// Displaying the elements of the Circular queue`
`void displayQueue() {`
`int i;`
`if (isEmptyQueue()) {`
`System.out.println("Sorry!! The Queue is Empty");`
`} else {`
`System.out.println("Position of Front:  " + front);`
`System.out.println("Below given are the elements of the Queue");`
`for (i = front; i != rear; i = (i + 1) % SIZE)`
`System.out.print(queue[i] + " ");`
`System.out.println(queue[i]);`
`System.out.println("Position of Rear: " + rear);`
`}`
`}`
`// Main function to drive the code`
`public static void main(String[] args) {`
`// creating the object of the class to call the methods`
`CirQueue que = new CirQueue();`
`// Queue is empty. No element is inserted as of now`
`que.deQueue();`
`que.enQueue(10);`
`que.enQueue(24);`
`que.enQueue(33);`
`que.enQueue(67);`
`que.enQueue(22);`
`que.displayQueue();`
`que.deQueue();`
`que.displayQueue();`
`que.enQueue(900);`
`que.displayQueue();`
`// Element cannot be inserted as the queue is full`
`que.enQueue(867);`
`que.deQueue();`
`que.displayQueue();`
`}`
`}`

## Conclusion

The above portrayal obviously clarifies what the CIrcular Queue is and how it functions in any programming language. Roundabout Queue was acquainted all together with tackle the restriction of typical Queue. Prior to chipping away at it, the programmer really should comprehend the Queue first alongside its execution in the real program.

java
cirularqueuejava
queue
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE     You must be Logged in to comment
Code Block Nilima Paul
Technology Security Analyst   201 Blog Posts
Trending Technologies
15 Software40 DevOps47 Frontend Development24 Backend Development20 Server Administration17 Linux Administration26 Data Center24 Sentry24 Terraform23 Ansible83 Docker70 Penetration Testing16 Kubernetes21 NGINX20 JenkinsX17
Recommended Blogs
7       8 Natasha Jones Vartika Oberoi Swarnalata Roydeb Pritam Dhar Natasha Jones Natasha Jones Natasha Jones Natasha Jones Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.