Circular Queue Data Structure - The Coding Shala

Home >> Data Structures >> Circular Queue

Circular Queue Data Structure

In this post, you will learn what is Circular Queue in Data Structure and how to implement a circular queue in Java using an array.

The 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 to make a circle. It is also called the Ring Buffer.
Why we need a Circular Queue? We know that in the simple queue if we remove an element from the head and we are not going to use that space again so it will be a complete waste. To prevent this we used a circular queue. For a circular queue, we may use a fixed-sized array and two pointers to indicate the starting position and the ending position.

Circular Queue Java Program

The following Java program explains the Circular queue. Here, i have implemented circular queue using an array.
Java Program: 

class MyCircularQueue {
    private int [] queue;
    private int start;
    private int rear;
    private int len;
    private int k;
    /** Initialize your data structure here. Set the size of the queue to be k. */
    public MyCircularQueue(int k) {
        queue = new int[k];
        start = 0;
        rear = -1;
        len = 0;
        this.k = k;
    }
    
    /** Insert an element into the circular queue. 
    Return true if the operation is successful.*/
    public boolean enQueue(int value) {
        if(isFull()) return false;
        rear = (rear+1)%k;
        len++;
        queue[rear] = value;
        return true;
    }
    
    /** Delete an element from the circular queue. 
    Return true if the operation is successful.*/
    public boolean deQueue() {
        if(isEmpty()) return false;
        start = (start+1)%k;
        len--;
        return true;
    }
    
    /** Get the front item from the queue. */
    public int Front() {
        if(isEmpty()) return -1;
        return queue[start];
    }
    
    /** Get the last item from the queue. */
    public int Rear() {
        if(isEmpty()) return -1;
        return queue[rear];
    }
    
    /** Checks whether the circular queue is empty or not. */
    public boolean isEmpty() {
        if(len==0) return true;
        return false;
    }
    
    /** Checks whether the circular queue is full or not. */
    public boolean isFull() {
        if(len == k) return true;
        return false;
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */

To read more about java queue Click Here



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Add two numbers in Scala - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Goal Parser Interpretation LeetCode Solution - The Coding Shala

Maximum Population Year LeetCode Solution - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala