Circular Queue on Arrays 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 to make a circle.

Define a class CQueue with the following details:
Class name: CQueue
Data members/instance variables:
ele[ ]: array to hold the integer elements.
cap: stores the maximum capacity of the array.
front: to point the index of the front.
rear: to point the index of the rear.
Member functions/methods:
CQueue(int max): constructor to initialize the data member cap = max, front = rear = 0 and create the integer array.
void insert(int v): to add integers from the rear index if possible else display the message “full from rear”.
int delete(): to remove and return elements from front, if any, else returns -999.
void display(): to display elements of circular queue.

Specify the class CQueue giving the details of void insert(int) and int delete(). Assume
that the other functions have been defined.

The main() function and algorithm need not be written.

import java.io.*;
class CQueue{
    int ele[];
    int cap;
    int front;
    int rear;
    public CQueue(int max){
        cap = max;
        ele = new int[cap];
        front = -1;
        rear = -1;
    }
    public void insert(int v){
        if((rear == cap - 1 && front == 0) || (rear + 1 == front)){
            System.out.println("Full from rear!");
            return;
        }
        if(rear == cap - 1)
            rear = 0;
        else
            rear++;
        ele[rear] = v;
        if(front == -1)
            front = 0;
        
    }
    public int delete(){
        int v = 0;
        if(front == -1)
            return -999;
        v = ele[front];
        ele[front] = 0;
        if(front == rear){
            front = -1;
            rear = -1;
        }
        else{
            if(front == cap - 1)
                front = 0;
            else
                front++;
        }
        return v;
    }
    public void display(){
        if(front == -1 && rear == -1)
            System.out.println("Queue is empty!");
        else{
            for(int i = 0; i < cap; i++)
                System.out.print(ele[i] + "\t");
            System.out.println();
        }
    }
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Number of elements: ");
        int size = Integer.parseInt(br.readLine());
        CQueue obj = new CQueue(size);
        while(true){
            System.out.println("1. Push");
            System.out.println("2. Pop");
            System.out.println("3. Display");
            System.out.print("Enter your choice: ");
            int choice = Integer.parseInt(br.readLine());
            switch(choice){
                case 1:
                System.out.print("Element to be pushed: ");
                int v = Integer.parseInt(br.readLine());
                obj.insert(v);
                break;
                case 2:
                int d = obj.delete();
                if(d == -999)
                    System.out.println("Queue underflow!");
                else
                    System.out.println(d + " popped.");
                break;
                case 3:
                obj.display();
                break;
                default:
                System.out.println("Bye...");
                return;
            }
        }
    }
}

4 thoughts on “Circular Queue on Arrays in Java

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.