Ich implementiere eine Generic Queue mit Linked List. Ich habe folgende Methoden: Enqueue, Dequeue und Peek. Ich brauche Hilfe beim Schreiben einer Methode, die den Minimalwert in der Warteschlange findet und eine Ausnahme auslöst, wenn die Warteschlange leer ist.Generische Queue, Methode, um den Minimalwert zu finden
Der Code, den ich bisher habe, ist:
class Queue <T>
{
private Node front, rear; //begin and end nodes
private int size; // number of items
//nested class to define node
private class Node
{
T item;
Node next;
}
//Zero argument constructor
public Queue()
{
front = null;
rear = null;
size = 0;
}
public boolean isEmpty()
{
return (size == 0);
}
//peek method
public T peek(){
T item = front.item;
return item;
}
//Remove item from the beginning of the list.
public T dequeue()
{
T item = front.item;
front = front.next;
if (isEmpty())
{
rear = null;
}
size--;
return item;
}
//Add T to the end of the list.
public void enqueue(T item)
{
Node oldRear = rear;
rear = new Node();
rear.item = item;
rear.next = null;
if (isEmpty())
{
front = rear;
}
else
{
oldRear.next = rear;
}
size++;
}
public int size()
{
return size;
}
//finds the maximum and minimum in the list
//assumes that head pointer is defined elsewhere
public static void main (String a[])
{
Queue <Integer> q = new Queue<Integer>();
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.enqueue(60);
q.enqueue(70);
System.out.println("Delete an item from queue: " + q.dequeue());
System.out.println("Size of the queue: " + q.size());
System.out.println("Size of the queue: " + q.peek());
}
}
Add bin werfen der Stacktrace und markieren Sie die Zeile, wo es geworfen wird – Jens
Was ist Ihre Frage? _Ich brauche Hilfe_ ist nicht spezifisch. Bitte lesen Sie [Wie stelle ich eine gute Frage?] (Http://stackoverflow.com/help/how-to-ask) –