Q7. For the following bounded array-based generic queue implementation public cl
ID: 3726058 • Letter: Q
Question
Q7. For the following bounded array-based generic queue implementation public class ArrayBndQueue implements BoundedQueuelnterface protected final int DEFCAP 100; // default capacity protected T[I queue; protected int numElements = 0; // number of elements in the queue protected int front = 0; protected int rear; ndex of rear of queue public ArrayBndQueue() // array that holds queue elements // index of front of queue queue = (T[]) new Object[DEFCAPj, rear- defCap -1; Complete the implementation of the dequeue method: public T dequeue()Explanation / Answer
you asked about only dequeue method so I only implementing it:- any wrong answer please let me know via comment I will edit and post it again;
In this circular queue element is inserted using front and remove using rear.
thats is the rear points to 99 initially. so dequeue from this queue we first check weather the queue is empty or not.
if queue is empty we return null and print the message queue is empyt;
else we increment rear by 1.
and insert that element or object to return variable.
and make that rear element null.
decrement that numElements by 1.
return that variable.
thats it.
below is the code.
public T dequeue(){
//checking weather queue is empty or not.
if(numElements==0){
System.out.println("Queue is empty");
return null;
}
//if queue is not empty then below code will execute
T rearElement;
rear = (rear + 1)%numElements;
rearElement = queue[rear];
queue[rear] = null;
numElements--;
return rearElement;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.