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

For this question, a circular array is used to implement a queue. You must imple

ID: 3861671 • Letter: F

Question

For this question, a circular array is used to implement a queue. You must implement a special method dequeue that takes one parameter an integer) specifying how many elements need to be removed from the queue. The method dequeue returns a reference to a list containing all the elements that have been removed, in reverse of order of removal. For this question, you are allowed to use the predefined class LinkedList. In particular, it has a constructor LinkedList(), stores an arbitrarily large number of elements, and implements the following methods. public interface List{//Add the element at the specified position of the list public abstract boolean add (int pos, E o);//Returns the element at the specified position in this list. public abstract E get (int index)://Returns the number of elements in this list public abstract int size();} bullet For your implementation of the method dequeue, you cannot use the methods of the class CircularQueue, accordingly these methods are not shown on the next page. Your code needs to manipulate directly the instance variables, elems, front, rear, and size. The execution of the Java program below displays "[2, 1, 0]". CircularQueue q; q = new CircularQueue(100); for (int i=0; i

Explanation / Answer

public List<T> dequeue(int n) {
List l=new LinkedList();

List rl=new LinkedList();


if(front==rear){//check if the Queue is empty
System.out.println("Queue is empty");
return null;
}else {
if(rear>front){//check if the Queue capacity is less than n
if(rear-front<n){
System.out.println("Queue capacity is less than specified");
return null;
}
}else{
if(size-front+rear<n){
System.out.println("Queue capacity is less than specified");
return null;
}
}
}

//retrieve the elements from Circular Queue and add to LinkedList
for(int i=0;i<n;i++){
l.add(i, elems[front]);
elems[front]=null;
front=(front+1)%size;
}

//Reverse the Linked list
for(int i=0;i<n;i++){
rl.add(i,l.get(l.size()-i-1));
}

return rl; //return reversed linked list
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote