In: Computer Science
Complete the following program:LinkedQueue.zip
package jsjf;
/**
* Represents a node in a linked list.
*/
public class LinearNode<T>
{
private LinearNode<T> next;
private T element;
/**
* 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<T> getNext()
{
return next;
}
/**
* Sets the node that follows this one.
* @param node node to follow this one
*/
public void setNext(LinearNode<T> node)
{
next = node;
}
/**
* 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;
}
}
----------------------------------------------------
package jsjf;
import jsjf.exceptions.*;
/**
* LinkedQueue represents a linked implementation of a queue.
*/
public class LinkedQueue<T> implements
QueueADT<T>
{
private int count;
private LinearNode<T> head, tail;
/**
* 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<T> node = new
LinearNode<T>(element);
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())
throw new
EmptyCollectionException("queue");
T result =
head.getElement();
head = head.getNext();
count--;
if (isEmpty())
tail = null;
return result;
}
/**
* 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
{
// To be completed as a Programming
Project
return null; // temp
}
/**
* Returns true if this queue is empty and false
otherwise.
* @return true if this queue is empty
*/
public boolean isEmpty()
{
// To be completed as a Programming
Project
return true; // temp
}
/**
* Returns the number of elements currently in this
queue.
* @return the number of elements in the queue
*/
public int size()
{
// To be completed as a Programming
Project
return 0; // temp
}
/**
* Returns a string representation of this queue.
* @return the string representation of the queue
*/
public String toString()
{
// To be completed as a Programming
Project
return ""; // temp
}
}
------------------------------------------------------------------------
package jsjf;
/**
* QueueADT defines the interface to a queue collection.
*/
public interface QueueADT<T>
{
/**
* Adds one element to the rear of this queue.
* @param element the element to be added to the rear
of the queue
*/
public void enqueue(T element);
/**
* 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();
}
/**
* 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()) // check Queue is empty or not.
throw new
EmptyCollectionException("queue");
return head.getElement(); // return reference of head
}
/**
* Returns true if this queue is empty and false
otherwise.
* @return true if this queue is empty
*/
public boolean isEmpty()
{
return (count == 0); // return true or false.
}
/**
* Returns the number of elements currently in this
queue.
* @return the number of elements in the queue
*/
public int size()
{
return count; // return number of elements }
/**
* Returns a string representation of this queue.
* @return the string representation of the queue
*/
public String toString()
{
String result = "";
LinearNode<T> temp1 = head;
while (temp1 != null) // terminate when queue is at tail
{
result = result + (temp1.getElement()).toString() + "\n"; // store queue elements in string
temp1 = temp1.getNext();
}
return result; // return string
}