Home > Java > javaTutorial > body text

Circular queue Java

WBOY
Release: 2024-08-30 15:08:27
Original
518 people have browsed it

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:

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.

The following scenarios need to be kept in mind while working in the circular queue:

  1. The queue is full if front = 0 and rear = size -1, which means that the front is pointing to the first position and the rear at the last. So the queue is full and no insertion can take place.
  2. The queue is full if front = rear+1, which means if, after so many deletions, the front is at position 3 of the queue and rear after insertions in a circular manner is at position 2, the list is full and no more insertions can take place.
  3. The queue is not full if the front!= 0 and rear = max-1, which means there are positions at the starting of the queue which are empty, so insertions can take place.
  4. If rear!= size-1, then the new value can be inserted as the rear can be incremented further using a mod(size).

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();
}
}
Copy after login

Output:

Circular queue Java

Below given is the screenshot of the output after various insertions and deletions on the Circular Queue:

Conclusion

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template