I am getting a null pointer exception on the two while loops. I don't know why I am getting that error, can someone please look at the code and let me know why I am getting that error. I have posted my code below, along with the test code I am using. Any help would be greatly appreciated.
private MyNode partitionHelper(MyNode inHeader, MyNode inTrailer) {
// Assume the numbers array is global
T pivot = inHeader.data;
MyNode leftPos = inHeader;
MyNode rightPos = inTrailer;
while (true) {
// Increment l while numbers[l] < pivot
while ((leftPos.data).compareTo(pivot) > 0) {
leftPos = leftPos.next;
}
// Decrement h while pivot < numbers[h]
while ((pivot).compareTo(rightPos.data) > 0) {
rightPos = rightPos.prev;
} // If there are zero or one elements remaining,
// all numbers are partitioned. Return h
if (leftPos == rightPos || rightPos.next == leftPos) {
return rightPos;
} // Swap numbers[l] and numbers[h],
// update l and h
else {
leftPos.next = rightPos.next;
rightPos.prev = leftPos.prev;
if (leftPos.prev == rightPos) {
leftPos.prev = rightPos;
rightPos.next = leftPos;
} else {
leftPos.prev = rightPos.prev;
rightPos.next = leftPos.next;
}
leftPos = leftPos.next;
rightPos = rightPos.prev;
}
}
}
ll.add(21016);
ll.add(25326);
ll.add(9026);
ll.add(1297);
ll.add(17432);
ll.add(30599);
ll.add(21367);
System.out.println(ll);
int j = ll.partition();
// partition returns the data, not the index, but you can
// probably still see the partitions.
System.out.println("j=" + j);
System.out.println(ll);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…