Question
import java.util.NoSuchElementException; /** * A classic first-in first-out queue data structure implemented * as a linked sequence of nodes. * * @param * the type
import java.util.NoSuchElementException;
/** * A classic first-in first-out queue data structure implemented * as a linked sequence of nodes. * * @param
public class Queue
/** * Queue uses a node based implementation, where the nodes form a singly * linked-list like structure. The queue must maintain a reference to the * node holding the element at the front of the queue, and a reference to * the node holding the element at the end of the queue. */
/** * The node at the front of the queue (the first node, or head, of the * linked list). */ private Node
/** * The node at the back of the queue (the last node of the linked list) */ private Node
/** * The number of elements stored in this queue (or the number of nodes) */ private int size;
/** * Creates an empty queue (size is zero). */ public Queue() { } /** * Enqueue an element. This operation adds the element to the back of the * queue. * * @param element * the element to add to the back of the queue */ public void enqueue(E element) { }
/** * Dequeue an element. This operation removes and returns the element at the * front of the queue if the queue is not empty. * * @return the element at the front of the queue if the queue is not empty * @throws NoSuchElementException * if the queue is empty */ public E dequeue() { }
/** * Return the element at the front of the queue without dequeuing the * element. * * @return the element at the front of the queue if the queue is not empty * @throws NoSuchElementException * if the queue is empty */ public E peek() { }
/** * Returns the number of elements in this queue. * * @return the number of elements in this queue */ public int size() { }
/** * Returns true if this queue is empty, and false otherwise. * * @return true if this queue is empty, and false otherwise */ public boolean isEmpty() { }
/** * Returns the node at the front of the queue. This method is here for * debugging and testing purposes. * * @return the node at the front of the queue */ Node
/** * Returns the node at the back of the queue. This method is here for * debugging and testing purposes. * * @return the node at the back of the queue */ Node
/** * Returns a hash code for this queue. The hash code is computed using the * elements of this stack. * * @return a hash code for this queue */ @Override public int hashCode() { final int prime = 31; int result = 1; Node
/** * Compares the specified object to this queue for equality. Two queues are * equal if they contain the same number of elements in the same order. * * @param obj * the object to compare to this queue for equality * @return true if the specified object is equal to this queue */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (this.getClass() != obj.getClass()) { return false; } Queue
return true; }
/** * Returns a string representation of this queue. The string representation * consists of a list of the queue's elements from the front of the queue to * the back of the queue, enclosed in square brackets ("[]"). Adjacent * elements are separated by the characters ", " (comma and space). Elements * are converted to strings as by String.valueOf(Object). * * @return a string representation of this queue */ @Override public String toString() { // see equals for an example of iterating over the nodes of the queue } }
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