Your are creating something similar to java.util.LinkedList
java.util.LinkedList
, thus it is best practice to use same or similar method names.Your list is sorted. Whenever you search for an element, change this loop:
while (tmpNode != null && tmpNode.data != d) { tmpNode = tmpNode.next; }
while (tmpNode != null && tmpNode.data != d) { tmpNode = tmpNode.next; } if (tmpNode == null) { System.out.println("That node does not exist in the list"); return; }
to this:
while (tmpNode != null && tmpNode.data < d) {
tmpNode = tmpNode.next;
}
if (tmpNode == null|| tmpNode.data != d) {
System.out.println("That node does not exist in the list");
return;
}
It stops, whenever the element can't be found.
- Instead of System.out.println
System.out.println
, use a booleanboolean
return value to indicate success.