Program to implement Circular Queue in JAVA

What is Circular Queue?

In  a standard queue data structure re-buffering problem occurs for each  dequeue operation. To solve this problem by joining the front and rear  ends of a queue to make the queue as a circular queue
Circular queue is a linear data structure. It follows FIFO principle.

  • In circular queue the last node is connected back to the first node to make a circle.
  • Circular linked list fallow the First In First Out principle.
  • Elements are added at the rear end and the elements are deleted at front end of the queue.
  • Both the front and the rear pointers points to the beginning of the array.
  • It is also called as “Ring buffer”.
  • Items can inserted and deleted from a queue in O(1) time.

 

circular queue

 

Program to implement Circular Queue in Java:

 

import java.io.*;
class CircularQ
{
	int Q[] = new int[100];
	int n, front, rear;
	static BufferedReader br = new BufferedReader(new
			InputStreamReader(System.in));
	public CircularQ(int nn)
	{
		n=nn;
		front = rear = 0;
	}
	public void add(int v)
	{
		if((rear+1) % n != front)
		{
			rear = (rear+1)%n;
			Q[rear] = v;
		}
		else
			System.out.println("Queue is full !");
	}
	public int del()
	{
		int v;
		if(front!=rear)
		{
			front = (front+1)%n;
			v = Q[front];
			return v;
		}
		else
			return -9999;
	}
	public void disp()
	{
		int i;
		if(front != rear)
		{
			i = (front +1) %n;
			while(i!=rear)
			{
				System.out.println(Q[i]);
				i = (i+1) % n;
			}
		}
		else
			System.out.println("Queue is empty !");
	}
	public static void main(String args[]) throws IOException
	{
		System.out.print("Enter the size of the queue : ");
		int size = Integer.parseInt(br.readLine());
		CircularQ call = new CircularQ(size);
		int choice;
		boolean exit = false;
		while(!exit)
		{
			System.out.print("\n1 : Add\n2 : Delete\n3 : Display\n4 : Exit\n\nYour Choice : ");
			choice = Integer.parseInt(br.readLine());
			switch(choice)
			{
				case 1 :
					System.out.print("\nEnter number to be added : ");
					int num = Integer.parseInt(br.readLine());
					call.add(num);
					break;
				case 2 :
					int popped = call.del();
					if(popped != -9999)
						System.out.println("\nDeleted : " +popped);
					else
						System.out.println("\nQueue is empty !");
					break;
				case 3 :
					call.disp();
					break;
				case 4 :
					exit = true;
					break;
				default :
					System.out.println("\nWrong Choice !");
					break;
			}
		}
	}
}

Sample Output:

 

$ javac CircularQ.java 
$ java CircularQ 
Enter the size of the queue : 5

1 : Add
2 : Delete
3 : Display
4 : Exit

Your Choice : 1

Enter number to be added : 31

1 : Add
2 : Delete
3 : Display
4 : Exit

Your Choice : 1

Enter number to be added : 28

1 : Add
2 : Delete
3 : Display
4 : Exit

Your Choice : 1

Enter number to be added : 9

1 : Add
2 : Delete
3 : Display
4 : Exit

Your Choice : 1

Enter number to be added : 56

1 : Add
2 : Delete
3 : Display
4 : Exit

Your Choice : 3
31
28
9

1 : Add
2 : Delete
3 : Display
4 : Exit

Leave a Reply