CSC300 / CSC402: The Beginning [5/6] Previous pageContentsNext page

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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  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;
    }
  }

Previous pageContentsNext page