basically what i need to do is firstly:
to modify the existing ListQueue class to implement a priority queue. This will involve changing the QueueNode class to store a priority value (an integer) with each item of data. You will need to change the add method to handle the priority values. You will also need to provide a method to retrieve the priority value of the item at the head of the queue. Note that the class you develop will no longer conform to the Queue interface, but should rather have the following methods:
public void add (T item, int pri);
// Add new item to a queue with given priority
public T remove ();
// Remove item from head of queue
public T head ();
// Return a copy of item at head of queue
public int getPriority ();
// Return priority of item at head of queue
public boolean isEmpty ();
// Return TRUE if no items in queue
In this context, lower priority values are associated with larger integer values, so, for example, an item with priority value of 1 has a higher priority than an item with a value of 5, and should come before it in the queue.
The ListQueue class is as follows...if any1 can help ill greatly appreciate it, pls also explain what the code is doing.
public class ListQueue<T> implements Queue<T>
{ private class QueueNode
{ public T data;
public QueueNode next;
} // inner class QueueNode
/** Reference to the head (front) of the queue.
*/
private QueueNode hd;
/** Reference to the tail (back) of the queue.
*/
private QueueNode tl;
/** Create an empty queue.
* <BR><I>Postcondition:</I> The queue is empty.
*/
public ListQueue ()
{ hd = tl = null; }
/** Add an item to the tail of a queue.
* <BR><I>Postcondition:</I> The queue is not empty.
* @param item The item to be added to the queue.
*/
public void add (T item)
{ QueueNode newNode = new QueueNode();
newNode.data = item;
newNode.next = null;
if (tl != null)
tl.next = newNode;
tl = newNode;
if (hd == null) // First item in queue
hd = tl;
} // add
/** Remove an item from the head of a queue.
* <BR><I>Precondition:</I> The queue is not empty.
* <BR><I>Postcondition:</I> The item at the head of the queue is removed and returned.
* @return The item from the head of the queue.
* @throws Assertionerror If the queue is empty.
*/
public T remove ()
{ assert hd != null;
T tmpData = hd.data;
hd = hd.next;
if (hd == null) // Was last element
tl = null;
return tmpData;
} // remove
/** Return a copy of the item at the head of a queue.
* <BR><I>Precondition:</I> The queue is not empty.
* @return The value of the item at the head of the queue.
* @throws Assertionerror If the queue is empty.
*/
public T head ()
{ assert hd != null;
return hd.data;
} // head
/** Tell if a queue contains any items.
* @return <CODE>true</CODE> if there are no items in the queue, otherwise <CODE>false</CODE>.
*/
public boolean isEmpty ()
{ return hd == null;
} // isEmpty
} // class ListQueue

New Topic/Question
Reply
MultiQuote











|