Circular Queue Implementation Using
Java
Objective:
To implement and demonstrate the operations of a Circular Queue in Java using array-based
representation.
Theory:
A Circular Queue is a type of queue where the last element is connected back to the first
element to form a circle. This data structure helps to solve the problem of wasted space in a
linear queue.
Operations on a Circular Queue:
1. Enqueue: Adds an element to the queue.
2. Dequeue: Removes an element from the queue.
3. isFull: Checks if the queue is full.
4. isEmpty: Checks if the queue is empty.
5. Display: Displays the elements of the queue.
Code Implementation:
class CQueue {
int rear;
int front;
int cqueue[];
int size;
int numberOfElements;
public CQueue(int size) {
[Link] = size;
cqueue = new int[size];
front = 0;
rear = -1;
numberOfElements = 0;
}
public boolean isEmpty() {
return numberOfElements == 0;
}
public boolean isFull() {
return numberOfElements >= size;
}
public void enQueue(int element) {
if (isFull()) {
[Link]("Overflow");
} else {
rear = (rear + 1) % size;
cqueue[rear] = element;
numberOfElements++;
}
}
public int deQueue() {
if (isEmpty()) {
[Link]("Underflow");
return -1;
} else {
int delement = cqueue[front];
front = (front + 1) % size;
numberOfElements--;
return delement;
}
}
public void display() {
if (numberOfElements == 0) {
[Link]("Empty Queue");
} else {
[Link]("Elements:");
int i = front;
int n = 0;
while (n <= numberOfElements - 1) {
[Link](cqueue[i] + " ");
i = (i + 1) % size;
n++;
}
[Link]();
}
}
}
import [Link];
public class CQueueOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int size, element, delement, choice = 0;
[Link]("Enter the size of the Circular Queue:");
size = [Link]();
CQueue cQueue = new CQueue(size);
while (choice < 6) {
[Link]("1. EnQueue 2. DeQueue 3. Display 4. isFull 5. isEmpty 6. Exit");
choice = [Link]();
switch (choice) {
case 1:
if ([Link]()) {
[Link]("Queue is full");
} else {
[Link]("Enter the element to EnQueue:");
element = [Link]();
[Link](element);
}
break;
case 2:
delement = [Link]();
if (delement != -1) {
[Link]("Deleted Element: " + delement);
}
break;
case 3:
[Link]();
break;
case 4:
[Link]("Is Full: " + [Link]());
break;
case 5:
[Link]("Is Empty: " + [Link]());
break;
case 6:
[Link]("Exiting...");
break;
default:
[Link]("Invalid choice! Please enter a valid option.");
}
}
}
}
Test Cases:
Test Case Table:
Test Case No. Input Expected Output
1 Size: 3, Enqueue: 10, 20, 30, Overflow
Enqueue 40
2 Size: 3, Enqueue: 10, 20, Deleted Element: 10,
Dequeue, Enqueue 30, Deleted Element: 20
Dequeue
3 Size: 2, Enqueue: 5, Elements: 5 15
Enqueue: 15, Display
4 Size: 3, Enqueue: 1, 2, 3, Elements: 2 3 4
Dequeue, Enqueue: 4,
Display
5 Size: 4, Check isFull after isFull: true
Enqueue: 2, 4, 6, 8
6 Size: 3, Dequeue with an Underflow, Deleted
Empty Queue Element: -1
Viva Questions:
1. What is a Circular Queue?
2. How does the rear pointer behave in a Circular Queue?
3. What is the advantage of using a Circular Queue over a Linear Queue?
4. Explain the logic behind the wrap-around mechanism in a Circular Queue.
5. Can a Circular Queue be implemented using a linked list? How?