Question: Implement the Queue ADT using a circular linked list. You will finish the implementation of the ch05.queues. CircularLinkedUnbndQueue class that already provided partial implementation of

Implement the Queue ADT using a circular linked list. You will finish the implementation of the ch05.queues. CircularLinkedUnbndQueue class that already provided partial implementation of the methods. You should complete enqueue (most part of this method is done), dequeue, isFull, isEmpty and toString method. Enhance main methods to test the functionality.

Here is the code that needs to be completed:

package ch05.queues; import support.LLNode; public class CircularLinkedUnbndQueue implements UnboundedQueueInterface { protected LLNode rear; // reference to the rear of this queue public CircularLinkedUnbndQueue() { rear = null; } public void enqueue(T element) // Adds element to the rear of this queue. { if (rear == null) { /* empty queue */ /* step 1 */ LLNode newNode = new LLNode(element); newNode.setLink(newNode); /* ignore step 2 */ /* step 3 */ rear = newNode; } else { /* non-empty queue */ /* step 1: create new object and let it link to the current front */ LLNode newNode = new LLNode(element); newNode.setLink(rear.getLink()); /* step 2: let the current rear node point to the new node*/ rear.setLink(newNode); /* step 3: update the rear reference of the queue */ rear = newNode; } } 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 = null; return element; } } public T front() { // Throws QueueUnderflowException if this queue is empty; // otherwise, return front element from this queue. return null; } public boolean isEmpty() // Returns true if this queue is empty; otherwise, returns false. { if (rear == null) return true; else return false; } public boolean isFull() { return false; } public String toString() { return null; } /* test the toString method */ public static void main(String[] args) { CircularLinkedUnbndQueue stringQueue = new CircularLinkedUnbndQueue(); stringQueue.enqueue("A"); stringQueue.enqueue("B"); stringQueue.enqueue("C"); System.out.println(stringQueue.toString()); } } 

Also, here is the code that was imported.

LLNode Class

package support; public class LLNode { private LLNode link; private T info; public LLNode(T info) { this.info = info; link = null; } public void setInfo(T info) // Sets info of this LLNode. { this.info = info; } public T getInfo() // Returns info of this LLONode. { return info; } public void setLink(LLNode link) // Sets link of this LLNode. { this.link = link; } public LLNode getLink() // Returns link of this LLNode. { return link; } } 

QueueOverflowException Class

public class QueueOverflowException extends RuntimeException { public QueueOverflowException() { super(); }

public QueueOverflowException(String message) { super(message); } }

UnboundedQueueInterface Class

package ch05.queues;

public interface UnboundedQueueInterface extends QueueInterface

{ void enqueue(T element); // Adds element to the rear of this queue. }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!