0% found this document useful (0 votes)
6 views

Circular Queue Implementation Using Arrays

Uploaded by

zakwan1292003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Circular Queue Implementation Using Arrays

Uploaded by

zakwan1292003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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) {


this.size = 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()) {
System.out.println("Overflow");
} else {
rear = (rear + 1) % size;
cqueue[rear] = element;
numberOfElements++;
}
}

public int deQueue() {


if (isEmpty()) {
System.out.println("Underflow");
return -1;
} else {
int delement = cqueue[front];
front = (front + 1) % size;
numberOfElements--;
return delement;
}
}

public void display() {


if (numberOfElements == 0) {
System.out.println("Empty Queue");
} else {
System.out.println("Elements:");
int i = front;
int n = 0;
while (n <= numberOfElements - 1) {
System.out.print(cqueue[i] + " ");
i = (i + 1) % size;
n++;
}
System.out.println();
}
}
}

import java.util.Scanner;

public class CQueueOperations {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int size, element, delement, choice = 0;

System.out.println("Enter the size of the Circular Queue:");


size = scanner.nextInt();

CQueue cQueue = new CQueue(size);

while (choice < 6) {


System.out.println("1. EnQueue 2. DeQueue 3. Display 4. isFull 5. isEmpty 6. Exit");
choice = scanner.nextInt();

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:

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?

You might also like