Nachdem ich einen dritten Wert in die Warteschlange eingefügt habe, erhalte ich eine Endlosschleife.Unendliche Schleife in der Prioritätswarteschlange für verknüpfte Listen
-Code für Insert-Methode:
public boolean insert(E object) {
Node<E> Nody = new Node(object);
Node<E> current = head;
Node<E> previous = null;
if (isEmpty())
head = Nody;
while (current != null) {
if (((Comparable<E>) object).compareTo(current.data) >= 0) {
previous = current;
current = current.next;
} else {
if (((Comparable<E>) object).compareTo(current.data) < 0) {
previous = Nody;
Nody = current;
}
if (previous == null && ((Comparable<E>) object).compareTo(current.data) < 0) {
Nody.next = head;
head = Nody;
}
}
}
currentSize++;
return false;
}
Hier ist der Fahrer, den ich verwendet, und den Ort, wo ich die unendliche Schleife erhalten:
public P2Driver() {
list = new OrderedListPriorityQueue<TestInteger>();
randomInteger = new Random(8);
randomPriority = new Random(8);
print("");
print("/////////////////////////////////////////////////////////");
print("/// BEGIN TESTING LIST IMPLEMENTATION");
print("/////////////////////////////////////////////////////////");
print("");
// Test insert
printCurrentSize();
print("--- BEGIN TESTING INSERT() --- ");
print("Checking if the list is empty BEFORE insertion...");
start = System.currentTimeMillis();
for (int i = 0; i < MAX_SIZE; i++) {
print(i + ". Is it empty? [ " + list.isEmpty() + " ]");
list.insert(new TestInteger(randomInteger.nextInt(10)));
} //GETS STUCK HERE<<<<<<<<<<<<<<<<<
Ausgang:
/////////////////////////////////////////////////////////
/// BEGIN TESTING LIST IMPLEMENTATION
/////////////////////////////////////////////////////////
Current Size: 0
--- BEGIN TESTING INSERT() ---
Checking if the list is empty BEFORE insertion...
0. Is it empty? [ true ]
1. Is it empty? [ false ]
2. Is it empty? [ false ]
dort seit Ist kein Code vorhanden, bedeutet dies, dass der Treiber festgefahren ist. Ich bin mir nicht sicher, was ich falsch mache.
sollten Sie wirklich den Typparameter in der Klassendeklaration angeben wie'E E extends Comparable ' –
oldrinb