Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class NonZeroNumQueue{ private int[] data; private int total; private int

ID: 3711632 • Letter: P

Question

public class NonZeroNumQueue{
private int[] data;
private int total;
private int front;
private int tail;
public NonZeroNumQueue(){
data=new int[10];
total=0;
front=tail=0;
}
public void enqueue(int num){
//Implement this method here
}
public void dequeue(){
if(!isEmpty()){
total--;
front = (front+1) %data.length;
}
}
public int peek(){
if(isEmpty())
return 0; //0 means the queue is empty
return data[front];
}
public boolean isEmpty(){
return total==0;
}
public boolean isFull(){
return false;
}
public int size(){
return total;
}
}

Explanation / Answer

public class NonZeroNumQueue{
private int[] data;
private int total;
private int front;
private int tail;
public NonZeroNumQueue(){
data=new int[10];
total=0;
front=tail=0;
}

public void dequeue(){
if(!isEmpty()){
total--;
front = (front+1) %data.length;
}
}
public int peek(){
if(isEmpty())
return 0; //0 means the queue is empty
return data[front];
}
public boolean isEmpty(){
return total==0;
}
public boolean isFull(){
return false;
}
public int size(){
return total;
}

}

public void enqueue(int num){
if (!isFull()) {
   total++;
   tail = (tail+1)%data.length
   data[tail] = num;
   System.out.println("Element " + num
     + " is pushed to Queue !");
} else {
   System.out.println("Overflow !"); */For array overflow(Necessary condition while implementing). Note that, here isFull() doesnt necessarily check overflow condition*/
}
}