Question: Can someone fix this code for me so that I dont have to do size + 1 and queue = new String[6] in the contructors
Can someone fix this code for me so that I dont have to do size + 1 and queue = new String[6] in the contructors to get the result I want? It works but only when I do this and id like the constructors to stay the same but I cant figure out where I messed up. Whenever it shows a full queue it shows it as empty when using both constructors.
public class MinimalQueue {
String queue[];
int front = -1;
int back = -1;
public MinimalQueue(){ queue = new String[5]; } public MinimalQueue(int size) {
queue = new String[size];
}
public void enqueue(String element) {
back = (back + 1) % queue.length;
queue[back] = element;
if (front == -1) { front = 0; }
}
public String dequeue() {
String element = queue[front];
queue[front] = null;
if (front == back) { front = back = -1; } else { front = (front + 1) % queue.length; }
return element;
}
public boolean isFull() {
return back == queue.length - 1;
} public boolean isEmpty() {
return front == -1;
}
public void showQueue() {
for (int i = front; i <= back; i++) { System.out.print(queue[i] + " "); } System.out.println();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
