C Programs Tutorials | IT Developer
IT Developer

Java Programs - Advanced



Share with a Friend

Circular Queue Program - Java Program

circular queue is a linear data structure that allows data insertion at the rear and removed from the front, with the rear end connected to the front end forming a circular arrangement.

The details of the members of the class are given below:

Class name: CirQueue
Data members/instance variables:
Q[]: array to hold integer values
cap: maximum capacity of the circular queue
front: to point the index of the front
rear: to point the index of the rear


Methods/Member functions:
CirQueue(int n): constructor to initialize cap = n, front = 0 and rear = 0
void push(int v): to add integers from the rear index if possible else display the message “QUEUE IS FULL”

 

int remove(): to remove and return the integer from front if any, else return -999
void print(): to display the elements of the circular queue in the order of front to rear

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

(ii) State one application of a circular queue.
import java.util.Scanner; class CirQueue{ int Q[]; int cap; int front; int rear; public CirQueue(int n){ cap = n; Q = new int[cap]; front = 0; rear = 0; } public void push(int v){ if(cap > 0){ Q[rear] = v; rear = (rear + 1) % Q.length; cap--; } else System.out.println("QUEUE IS FULL"); } public int remove(){ if(cap == Q.length) return -999; int temp = Q[front]; front = (front + 1) % Q.length; cap++; if(front == rear){ front = 0; rear = 0; cap = Q.length; } return temp; } public void print(){ if(cap == Q.length) System.out.println("QUEUE IS EMPTY"); else{ int index = front; for(int i = 1; i <= Q.length - cap; i++){ System.out.print(Q[index] + " "); index = (index + 1) % Q.length; } System.out.println(); } } public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.print("CIRCULAR QUEUE CAPACITY: "); 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. PRINT"); System.out.print("ENTER YOUR CHOICE: "); int choice = Integer.parseInt(in.nextLine()); switch(choice){ case 1: System.out.print("ELEMENT TO BE PUSHED: "); int elem = Integer.parseInt(in.nextLine()); obj.push(elem); break; case 2: elem = obj.remove(); if(elem == -999) System.out.println("QUEUE IS EMPTY"); else System.out.println(elem + " REMOVED"); break; case 3: obj.print(); break; default: System.out.println("BYE"); return; } } } }

Output

 
OUTPUT 1:
CIRCULAR QUEUE CAPACITY: 10
1. PUSH
2. POP
3. PRINT
ENTER YOUR CHOICE: 1
ELEMENT TO BE PUSHED: 1
1. PUSH
2. POP
3. PRINT
ENTER YOUR CHOICE: 1
ELEMENT TO BE PUSHED: 2
1. PUSH
2. POP
3. PRINT
ENTER YOUR CHOICE: 1
ELEMENT TO BE PUSHED: 3
1. PUSH
2. POP
3. PRINT
ENTER YOUR CHOICE: 1
ELEMENT TO BE PUSHED: 4
1. PUSH
2. POP
3. PRINT
ENTER YOUR CHOICE: 1
ELEMENT TO BE PUSHED: 5
1. PUSH
2. POP
3. PRINT
ENTER YOUR CHOICE: 3
1 2 3 4 5 
1. PUSH
2. POP
3. PRINT
ENTER YOUR CHOICE: 2
1 REMOVED
1. PUSH
2. POP
3. PRINT
ENTER YOUR CHOICE: 1
ELEMENT TO BE PUSHED: 6
1. PUSH
2. POP
3. PRINT
ENTER YOUR CHOICE: 2
2 REMOVED
1. PUSH
2. POP
3. PRINT
ENTER YOUR CHOICE: 3
3 4 5 6 
1. PUSH
2. POP
3. PRINT
ENTER YOUR CHOICE: 


(ii) Process scheduling in operating systems.