Can someone fix this code for me so that I dont have to do size + 1 and queue =
ID: 3861336 • Letter: C
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 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();
}
}
TESTING METHOD
public static void main(String[] args) {
MinimalQueue queue = new MinimalQueue();
System.out.println("Queue is empty: " + queue.isEmpty());
System.out.println();
queue.enqueue("John");
queue.enqueue("Ryan");
queue.enqueue("Tom");
System.out.print("Queue: ");
queue.showQueue();
System.out.println("Queue is empty: " + queue.isEmpty());
System.out.println();
System.out.println(queue.dequeue() + " is deleted from queue");
System.out.print("Queue: ");
queue.showQueue();
System.out.println("Queue is full: " + queue.isFull());
System.out.println();
queue.enqueue("Trey");
queue.enqueue("Harrison");
queue.enqueue("Jeremy");
System.out.print("Queue: ");
queue.showQueue();
System.out.println("Queue is full: " + queue.isFull());
System.out.println();
System.out.println();
MinimalQueue queue2 = new MinimalQueue(3);
System.out.println("Queue2 is empty: " + queue2.isEmpty());
System.out.println();
queue2.enqueue("Rebecca");
queue2.enqueue("Sarah");
System.out.print("Queue2: ");
queue2.showQueue();
System.out.println("Queue2 is empty: " + queue2.isEmpty());
System.out.println();
System.out.println(queue2.dequeue() + " is deleted from queue2");
System.out.print("Queue2: ");
queue2.showQueue();
System.out.println("Queue2 is full: " + queue2.isFull());
System.out.println();
queue2.enqueue("Anna");
queue2.enqueue("Emma");
System.out.print("Queue2: ");
queue2.showQueue();
System.out.println("Queue2 is full: " + queue2.isFull());
}
Explanation / Answer
Dear friend,
Generally as front and rear are -1 we get confused about size or queue.
Better yo declare them in this fashion
Dear friend,
You should be writing your enqueue and dequeue method like this
Take one more parameter in class MinimalQueue
public class MinimalQueue {
String queue[];
int front = 0;
int back = 0;
Int size=0;
}
public void enqueue(String element) {
//First check if queue is full
// I am assuming this is NOT a circular queue
If ( queue.length == size)
//Queue is full throw an error
queue[back]= element;
back = (back + 1) % queue.length;
Size++;
}
public String dequeue() {
If(size == 0)
{
//can not dequeue throw an error
}
String temp=queue[front];
queue[front]=NULL;
Front = (front+1) % queue.length
Size--;
Return temp;
}
public boolean isEmpty(){
Return (size == 0 )
}
public int size() {
Return size;
}
What I did is instead of advacing first back and then pushing element in queue . I already advances the back then just queued the element.
By this way you will land up in invalid enqueue and dequeue .. hope this helps you in your question.