Question
The next Java code needs to be converted from static to dynamic (just addd changes in same code): import java.util.Scanner; class MyIntStaticCircularQueue { int capacity
The next Java code needs to be converted from static to dynamic (just addd changes in same code):
import java.util.Scanner;
class MyIntStaticCircularQueue {
int capacity = 2;
int queue[] = new int[capacity];
int front = 0;
int rear = -1;
int currentSize = 0;
void enqueue(int elemToAdd) {
if (isFull()) {
System.out.println("Queue is full. Can't enqueue.");
} else {
rear = (rear + 1) % capacity;
queue[rear] = elemToAdd;
currentSize++;
}
}
void dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty. Can't dequeue.");
} else {
front = (front + 1) % capacity;
currentSize--;
}
}
void size() {
System.out.println("Size is " + currentSize + ". Available space: " + (capacity - currentSize));
}
void show() {
if (!isEmpty())
for (int i = 0; i < currentSize; i++)
System.out.print(queue[(i + front) % capacity] + " ");
else
System.out.println("No data to show, Queue is empty.");
}
boolean isFull() {
if (currentSize == capacity)
return true;
return false;
}
boolean isEmpty() {
if (currentSize == 0)
return true;
return false;
}
}
public class IntStaticCircularQueue {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
MyIntStaticCircularQueue que = new MyIntStaticCircularQueue();
boolean exit = true;
System.out.print("Menu: 1-Enqueue 2-Dequeue 3-Size 4-Show 5-Exit ");
do {
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the number to enqueue: ");
que.enqueue(sc.nextInt());
break;
case 2:
que.dequeue();
break;
case 3:
que.size();
break;
case 4:
que.show();
break;
default:
exit = !exit;
}
} while (exit);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Answer To convert the static implementation to a dynamic one we need to modify the MyIntStaticCircularQueue class to allow resizing the queue when it ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started