Circular Queue Implementation Using Arrays
Circular Queue Implementation Using Arrays
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.
Code Implementation:
class CQueue {
int rear;
int front;
int cqueue[];
int size;
int numberOfElements;
import java.util.Scanner;
switch (choice) {
case 1:
if (cQueue.isFull()) {
System.out.println("Queue is full");
} else {
System.out.println("Enter the element to EnQueue:");
element = scanner.nextInt();
cQueue.enQueue(element);
}
break;
case 2:
delement = cQueue.deQueue();
if (delement != -1) {
System.out.println("Deleted Element: " + delement);
}
break;
case 3:
cQueue.display();
break;
case 4:
System.out.println("Is Full: " + cQueue.isFull());
break;
case 5:
System.out.println("Is Empty: " + cQueue.isEmpty());
break;
case 6:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice! Please enter a valid option.");
}
}
}
}
Test Cases:
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?