Question
Everything works except for list. I need help implementing it in LinkedQueue.java LinkedQueue.java package jsjf; import jsjf.exceptions.*; /** * LinkedQueue represents a linked implementation of
Everything works except for list. I need help implementing it in LinkedQueue.java
LinkedQueue.java
package jsjf;
import jsjf.exceptions.*;
/** * LinkedQueue represents a linked implementation of a queue. * * @author Java Foundations * @version 4.0 */ public class LinkedQueue
/** * Creates an empty queue. */ public LinkedQueue() { count = 0; head = tail = null; } /** * Adds the specified element to the tail of this queue. * @param element the element to be added to the tail of the queue */ public void enqueue(T element) { LinearNode
if (isEmpty()) head = node; else tail.setNext(node);
tail = node; count++; } /** * Removes the element at the head of this queue and returns a * reference to it. * @return the element at the head of this queue * @throws EmptyCollectionException if the queue is empty */ public T dequeue() throws EmptyCollectionException { if (isEmpty()) //if the queue is empty, the message is printed out. { System.out.println("Queue is empty. Please enqueue at least 1 element first."); tail = null; } else { T result = head.getElement(); head = head.getNext(); count--; return result; } return null; } /** * Returns a reference to the element at the head of this queue. * The element is not removed from the queue. * @return a reference to the first element in this queue * @throws EmptyCollectionsException if the queue is empty */ public T first() throws EmptyCollectionException { if (isEmpty()) { System.out.println("Queue is empty. Please enqueue at least 1 element first."); } else { T result = head.getElement();
return result; } return null; }
/** * Returns true if this queue is empty and false otherwise. * @return true if this queue is empty */ public boolean isEmpty() { return (count==0); }
/** * Returns the number of elements currently in this queue. * @return the number of elements in the queue */ public int size() { return count; }
/** * Returns a string representation of this queue. * @return the string representation of the queue */ public String toString() { String result = "";
LinearNode
while(x != null) { result += x.getElement().toString() + " "; x = x.getNext(); } return result; }
public void list() { //Need help here } }
QueueADT.java
package jsjf;
/** * QueueADT defines the interface to a queue collection. * * @author Java Foundation * @version 4.0 */ public interface QueueADT
/** * Removes and returns the element at the front of this queue. * @return the element at the front of the queue */ public T dequeue();
/** * Returns without removing the element at the front of this queue. * @return the first element in the queue */ public T first();
/** * Returns true if this queue contains no elements. * @return true if this queue is empty */ public boolean isEmpty();
/** * Returns the number of elements in this queue. * @return the integer representation of the size of the queue */ public int size();
/** * Returns a string representation of this queue. * @return the string representation of the queue */ public String toString(); public void list(); }
LinearNode.java
package jsjf;
/** * Represents a node in a linked list. * * @author Java Foundations * @version 4.0 */ public class LinearNode
/** * Creates an empty node. */ public LinearNode() { next = null; element = null; }
/** * Creates a node storing the specified element. * @param elem element to be stored */ public LinearNode(T elem) { next = null; element = elem; }
/** * Returns the node that follows this one. * @return reference to next node */ public LinearNode
/** * Sets the node that follows this one. * @param node node to follow this one */ public void setNext(LinearNode
/** * Returns the element stored in this node. * @return element stored at the node */ public T getElement() { return element; }
/** * Sets the element stored in this node. * @param elem element to be stored at this node */ public void setElement(T elem) { element = elem; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started