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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
Below given is the basic syntax of using the Circular Queue in the Java program:
Let us understand the creation and working of Circular queue in detail:
Basic operations that are performed on the CIrcular Queue are given below:
Below given are the steps followed while creating, inserting, or deleting the elements in the Circular Queue:
The following scenarios need to be kept in mind while working in the circular queue:
Below given is the example showing the implementation of Circular Queue in the Java program:
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(); } }
Output:
Below given is the screenshot of the output after various insertions and deletions on the Circular Queue:
The above description clearly explains what the CIrcular Queue is and how it works in any programming language. Circular Queue was introduced in order to solve the limitation of normal Queue. Before working on it, it is very important for the programmer to understand the Queue first along with its implementation in the actual program.
The above is the detailed content of Circular queue Java. For more information, please follow other related articles on the PHP Chinese website!