can you explain why the code shown below does not output: pineapple lime and how to fix it when using the driver program shown in screen shot to test. i also attach of the LLNode class // Note: It is important to implement this file as a circular queue without 'front' pointer public class CircularLinkedQueue implements QueueInterface { protected LLNode rear; // reference to the rear of this queue protected int numElements = 0; public CircularLinkedQueue() { rear = null; } public void enqueue(T element) { // Adds element to the rear of this queue. LLNode newNode = new LLNode(element); if (isEmpty()) { rear = newNode; newNode.setLink(newNode); numElements++; } else { rear = newNode; newNode.setLink(rear.getLink()); rear.setLink(newNode); numElements++; } } public T dequeue() { // Throws QueueUnderflowException if this queue is empty; // otherwise, removes front element from this queue and returns it if (isEmpty()) throw new QueueUnderflowException("Dequeue attempted on empty queue"); else { T element; rear = rear.getLink(); element = rear.getInfo(); rear.setInfo(null); if (rear.getLink() == null) rear = null; numElements--; return element; } } public String toString() { String result = "\n"; LLNode cursor = rear; while (cursor != null){ result+=(cursor.getInfo()); cursor = cursor.getLink(); } return result; } public boolean isEmpty() { // Returns true if this queue is empty; otherwise, returns false. return (numElements == 0); } public boolean isFull() { // Returns false - a linked queue is never full. return false; } public int size() { // Returns the number of elements in this queue. return numElements; } }
can you explain why the code shown below does not
output:
pineapple
lime
and how to fix it when using the driver
i also attach of the LLNode class
// Note: It is important to implement this file as a circular queue without 'front' pointer
public class CircularLinkedQueue<T> implements QueueInterface<T> {
protected LLNode<T> rear; // reference to the rear of this queue
protected int numElements = 0;
public CircularLinkedQueue() {
rear = null;
}
public void enqueue(T element) {
// Adds element to the rear of this queue.
LLNode<T> newNode = new LLNode<T>(element);
if (isEmpty()) {
rear = newNode;
newNode.setLink(newNode);
numElements++;
}
else {
rear = newNode;
newNode.setLink(rear.getLink());
rear.setLink(newNode);
numElements++;
}
}
public T dequeue() {
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes front element from this queue and returns it
if (isEmpty())
throw new QueueUnderflowException("Dequeue attempted on empty queue");
else {
T element;
rear = rear.getLink();
element = rear.getInfo();
rear.setInfo(null);
if (rear.getLink() == null)
rear = null;
numElements--;
return element;
}
}
public String toString() {
String result = "\n";
LLNode<T> cursor = rear;
while (cursor != null){
result+=(cursor.getInfo());
cursor = cursor.getLink();
}
return result;
}
public boolean isEmpty() {
// Returns true if this queue is empty; otherwise, returns false.
return (numElements == 0);
}
public boolean isFull() {
// Returns false - a linked queue is never full.
return false;
}
public int size() {
// Returns the number of elements in this queue.
return numElements;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images