Circular Queue Java Program | ISC Computer Science 2020 Theory

A circular queue is a linear data structure which works on the principle of FIFO, enables the user to enter data from the rear end and remove data from the front end with the rear end connected to the front end to form a circular pattern.

Define a class CirQueue with the following details:

Class name: CirQueue
Data members/instance variables:
cq[]: array to store the integers
cap: stores the maximum capacity of the array
front: to point the index of the front end
rear: to point the index of the rear end
Member functions:
CirQueue(int max): constructor to initialize the data member cap = max, front = 0 and rear = 0
void push(int n): to add integer in the queue from the rear end if possible, otherwise display the message “QUEUE IS FULL”
int pop(): removes and returns the integer from the front end of the queue if any, else returns -9999
void show(): displays the queue elements

(a) Specify the class CirQueue giving details of the functions void push(int) and int pop(). Assume that the other functions have been defined.
The main() function and algorithm need not be written.

import java.util.Scanner;
class CirQueue{
    int cq[];
    int cap;
    int front;
    int rear;
    public CirQueue(int max){
        cap = max;
        cq = new int[cap];
        front = 0;
        rear = 0;
    }
    public void push(int v){
        if(cap == 0)
            System.out.println("QUEUE IS FULL");
        else{
            cq[rear] = v;
            rear = (rear + 1) % cq.length;
            cap--;
        }
    }
    public int pop(){
        if(cap == cq.length)
            return -9999;
        else{
            int v = cq[front];
            front = (front + 1) % cq.length;
            cap++;
            if(front == rear)
                front = rear = 0;
            return v;
        }
    }
    public void show(){
        if(cap == cq.length)
            System.out.println("QUEUE IS EMPTY");
        else{
            int i = front;
            int count = cq.length;
            while(count > cap){
                System.out.print(cq[i] + " ");
                i = (i + 1) % cq.length;
                count--;
            }
            System.out.println();
        }
    }
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.print("Circular queue size: ");
        int size = Integer.parseInt(in.nextLine());
        CirQueue obj = new CirQueue(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(in.nextLine());
            switch(choice){
            case 1:
                System.out.print("Element to be pushed: ");
                int d = Integer.parseInt(in.nextLine());
                obj.push(d);
                break;
            case 2:
                d = obj.pop();
                if(d == -9999)
                    System.out.println("QUEUE IS EMPTY");
                else
                    System.out.println(d + " POPPED");
                break;
            case 3:
                obj.show();
                break;
            default:
                System.out.println("Bye!");
                return;
            }
        }
    }
}

(b) How is a linear queue structure different from a circular queue structure?

In a linear queue structure, the last position isn’t connected to the first position. But in a circular queue, the last position is connected to the front position.

Leave a Reply

Your email address will not be published. Required fields are marked *