Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions

Question

Are robots going to displace all workers? Explain your answer.

Answered: 1 week ago