CSC300 / CSC402: The Beginning [5/6] |
Now figure out how to start. For this style of list,
we must have a special case, since we need to modify
first
rather than next
.
11 |
public void insert (double item) { if (first == null || first.item >= item) { Node y = new Node (item, null); Node f = first; first = y; y.next = f; } else { Node x = first; while (x.next != null && x.next.item < item) { x = x.next; } Node y = new Node (item, null); Node f = x.next; x.next = y; y.next = f; } } |