Question: JAVA Creating a Double-Linked List Circular Queue There are 4 java classes in the package which are: DLLCircularQueue.java, DLLNode.java, QueueADT.java, and TestCircularLinkedList. In the DLLNode:
JAVACreating a Double-Linked List Circular Queue
There are 4 java classes in the package which are:DLLCircularQueue.java, DLLNode.java, QueueADT.java, and TestCircularLinkedList.
In the DLLNode:
* Add a previous node reference
In the DLLCircularQueue.java
* Alter enqueue so that the previous node reference gets set appropriately.
* Add a createCircle method that completes the "wrapping around" (note that after we do this, we really only need the "head" reference to the start of the list).
* Adjust the toString method to accommodate the circular nature of the queue.
* Write a traverse() method that can find a specific target in the circularQueue.
* Write a remove()... or 're-write' dequeue() so that it can remove() any current node (not necessarily the head, note if you remove the head, you need to reset head to head.next).
This is the code I'd provided to work on it. Please help!!!
public class DLLCircularQueueimplements QueueADT { private int count; private DLLNode head, tail;
/** * C O N S T R U C T O R */ public DLLCircularQueue() { count = 0; head = tail = null; }
/** * Mutator: enqueue() Add an element to the end of the queue (tail) * */ public void enqueue(Type elem) { DLLNode node = new DLLNode(elem); if (isEmpty()) // no item waiting in the queue currently { head = node; } else { tail.setNext(node); } tail = node; count++; } /** * Mutator: dequeue() Removes the first item from the head of the queue */ public Type dequeue() throws RuntimeException { if (isEmpty()) { throw new RuntimeException("Empty queue -- cannot dequeue"); } Type result = head.getElement(); head = head.getNext(); count--;
if (count == 0) // empty queue { tail = null; }
return result; }
/** * Accessor first() -- returns a copy of the item at the front of the queue; * no remove * * @return copy of the element */ public Type first() throws RuntimeException { if (isEmpty()) { throw new RuntimeException("Empty queue -- no front element"); }
Type result = head.getElement(); return result; }
/** * Accessor: isEmpty() indicates whether or not the queue has no elements */ public boolean isEmpty() { if (head == null) { return true; } else { return false; } }
/** * Accessor size() reports the number of elements in the queue * * @return count of items in the queue */ public int size() { return count; }
/** * Accessor: toString() displays the contents of the queue:
* one element after the other from front to rear */ public String toString() { String out = ""; DLLNode current = new DLLNode(); current = head; if (current == null) { out += "Empty queue "; } else { while (current != null) { out += current.getElement(); out += " "; current = current.getNext(); } } return out; } }
* CLASS DESCRIPTION: A double linked node is a container for any class type * with two links (references) 1) to the next Node and 2) the previous Node */ public class DLLNode{ private T element; private DLLNode next; private DLLNode previous;
/** * C O N S T R U C T O R * default set node and next to null */ public DLLNode() { element = null; next = null; }
/** * C O N S T R U C T O R * conversion set node to input element and and next to null */ public DLLNode(T inElem) { element = inElem; next = null; }
/** * Accessor: getElement() * * @return this DLLNode */ public T getElement() { return element; }
/** * Accessor: getNext() * * @return reference to next DLLNode */
public DLLNode getNext() { return next; }
/** * Mutator: setElement(T inputElement) allow user/client to change the value * of the Node * */ public void setNode(T inElem) { element = inElem; }
/** * Mutator: setNext( DLLNode ptr) * * @return reference to next DLLNode */
public void setNext(DLLNode nodePtr) { next = nodePtr; } }
public class TestCircularLinkedList{
/** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
