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

JAVA Creating a Double-Linked List Circular Queue There are 4 java classes in th

ID: 3756631 • Letter: J

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:

* 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 DLLCircularQueue implements 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
}

}

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

struct Node
{
int info;
struct Node* lnk;
};
  
struct Queue
{
struct Node *Fr, *re;
};
  
void enQueue(Queue *qu, int val)
{
struct Node *tmp = new Node;
tmp->info = val;
if (qu->Fr == NULL)
qu->Fr = tmp;
else
qu->re->lnk = tmp;
  
qu->re = tmp;
qu->re->lnk = qu->Fr;
}
int deQueue(Queue *qu)
{
if (qu->Fr == NULL)
{
printf ("Queue is empty");
return INT_MIN;
}
  
int val;
if (qu->Fr == qu->re)
{
val = qu->Fr->info;
free(qu->Fr);
qu->Fr = NULL;
qu->re = NULL;
}
else
{
struct Node *tmp = qu->Fr;
val = tmp->info;
qu->Fr = qu->Fr->lnk;
qu->re->lnk= qu->Fr;
free(tmp);
}
  
return val ;
}
void displayQueue(struct Queue *qu)
{
struct Node *tmp = qu->Fr;
printf(" Elements in Circular Queue are: ");
while (tmp->lnk != qu->Fr)
{
printf("%d ", tmp->info);
tmp = tmp->lnk;
}
printf("%d", tmp->info);
}
int main()
{
Queue *qu = new Queue;
qu->Fr = qu->re = NULL;
enQueue(qu, 14);
enQueue(qu, 22);
enQueue(qu, 6);
displayQueue(qu);
printf(" Deleted val = %d", deQueue(qu));
printf(" Deleted val = %d", deQueue(qu));
displayQueue(qu);
enQueue(qu, 9);
enQueue(qu, 20);
displayQueue(qu);
return 0;
}