Question: You will need to complete the enqueue(), dequeue(), and peek() methods to add an element, remove the element at the front, and return (without removing)
You will need to complete the enqueue(), dequeue(), and peek() methods to add an element, remove the element at the front, and return (without removing) the element at the front. Your methods will need to use the Entry and EmptyQueueException classes I supply.
Need to finish:
package edu.buffalo.cse116; /** * Class which implements a proper {@code Queue} (including using the CORRECT method names) using a set of singly linked * nodes. Besides in the naming convention, this does not include the methods that are inappropriate for a Queue to * have, * * @author Matthew Hertz * @param Type of data stored within this Queue. */ public class ProperQueue { /** * The first node in the linked list (or null if the list is empty). This is the end of the list where we remove * elements from. */ private Entry head; /** * The first node in the linked list (or null if the list is empty). This is the end of the list where we add elements * to include them on the queue. */ private Entry tail; /** * The number of elements currently in the queue. */ private int size; /** Create a new (empty) instance of this class. */ public ProperQueue() { head = null; tail = null; size = 0; } /** * Adds an item to the end of the queue. Traditionally, this is the only * method available to add data to a Queue. * * @param item Element to be added to the end of the queue. * @return Element added to the Queue (e.g., {@code item}). */ public E enqueue(E item) { } /** * Removes and returns the element at the front of this queue. Traditionally, * this is the only method available to remove data from the Queue. * * @return Element that was removed from the front of the Queue. * @throws EmptyQueueException Thrown when the Queue does not have any * elements to remove. */ public E dequeue() { } /** * Like {@link #dequeue()}, this returns the element at the front of the * queue, but unlike {@link #dequeue} this method DOES NOT remove it from the * queue. * * @return Element that is at the front of the queue. * @throws EmptyQueueException Thrown when the Queuedoes not have any elements * to remove. */ public E peek() { } /** * Returns the number of elements in this Queue. * * @return Items available in the Queue. */ public int size() { return size; } /** * Returns whether there are any elements in this Queue. * * @return True if the Queue does not have any elements; false otherwise. */ public boolean isEmpty() { return size == 0; } }
Entry class:
package edu.buffalo.cse116;
/** * Class which defines an Entry in a singly-linked list. Because these are used relatively often, this is a standalone * (e.g. non-inner) class. * * @author Matthew Hertz * @param
/** Data stored within this node. */ private E element;
/** Reference to the node following this one in the linked list. */ private Entry
/** * Creates an empty node. */ public Entry() { this(null); }
/** * Creates a node storing the specified element. * * @param elem Element to which the new node should refer */ public Entry(E elem) { next = null; element = elem; }
/** * Get the next node in the linked list. * * @return Reference to the node following this one */ public Entry
/** * Specify that the given node should follow the current one in the linked list. * * @param node node which should follow the current one */ public void setNext(Entry
/** * Get the element stored within this node. * * @return Element referred to by the node */ public E getElement() { return element; }
/** * Direct the node to store a new element. * * @param elem New element which this node should store */ public void setElement(E elem) { element = elem; } }
EmptyQueueException:
package edu.buffalo.cse116;
/** * Because the original developers of Java had "design skills" in the same way * that a 3-legged rhino has grace and agility, they created specific exceptions * for Stacks and not Queues. This is designed to fix this. * * @author Matthew Hertz */ public class EmptyQueueException extends RuntimeException { public EmptyQueueException() { super(); }
public EmptyQueueException(String message) { super(message); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
