Convert LinkedQueue into a generic class that can store objects of any type. Use
ID: 3733132 • Letter: C
Question
Convert LinkedQueue into a generic class that can store objects of any type. Use LinkedQueue class provided with the assignment
/**
This class implements a queue based
on linked lists.
*/
public class LinkedQueue
{
private class Node
{
String value;
Node next;
Node(String val, Node n)
{
value = val;
next = n;
}
}
private Node front = null;
private Node rear = null;
/**
The method enqueue adds a value
to the queue.
@param s The value to be added
to the queue.
*/
public void enqueue(String s)
{
if (rear != null)
{
rear.next = new Node(s, null);
rear = rear.next;
}
else
{
rear = new Node(s, null);
front = rear;
}
}
/**
The empty method checks to see if
the queue is empty.
@return true if and only if queue
is empty.
*/
public boolean empty()
{
return front == null;
}
/**
The method peek returns value at the
front of the queue.
@return item at front of queue.
@excepton EmptyQueueException When the
queue is empty.
*/
public String peek()
{
if (empty())
throw new EmptyQueueException();
else
return front.value;
}
/**
The dequeue method removes and returns
the item at the front of the queue.
@return item at front of queue.
@exception EmptyQueueException When
the queue is empty.
*/
public String dequeue()
{
if (empty())
throw new EmptyQueueException();
else
{
String value = front.value;
front = front.next;
if (front == null) rear = null;
return value;
}
}
/**
The toString method concatenates all strings
in the queue to give a string representation
of the contents of the queue.
@return string representation of this queue.
*/
public String toString()
{
StringBuilder sBuilder = new StringBuilder();
// Walk down the list and append all values
Node p = front;
while (p != null)
{
sBuilder.append(p.value + " ");
p = p.next;
}
return sBuilder.toString();
}
}
Explanation / Answer
/** This class implements a queue based on linked lists. */ public class LinkedQueue { private class Node { T value; Node next; Node(T val, Node n) { value = val; next = n; } } private Node front = null; private Node rear = null; /** The method enqueue adds a value to the queue. @param s The value to be added to the queue. */ public void enqueue(T s) { if (rear != null) { rear.next = new Node(s, null); rear = rear.next; } else { rear = new Node(s, null); front = rear; } } /** The empty method checks to see if the queue is empty. @return true if and only if queue is empty. */ public boolean empty() { return front == null; } /** The method peek returns value at the front of the queue. @return item at front of queue. @excepton EmptyQueueException When the queue is empty. */ public T peek() { if (empty()) throw new EmptyQueueException(); else return front.value; } /** The dequeue method removes and returns the item at the front of the queue. @return item at front of queue. @exception EmptyQueueException When the queue is empty. */ public T dequeue() { if (empty()) throw new EmptyQueueException(); else { T value = front.value; front = front.next; if (front == null) rear = null; return value; } } /** The toString method concatenates all strings in the queue to give a string representation of the contents of the queue. @return string representation of this queue. */ public String toString() { StringBuilder sBuilder = new StringBuilder(); // Walk down the list and append all values Node p = front; while (p != null) { sBuilder.append(p.value + " "); p = p.next; } return sBuilder.toString(); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.