CSC300: Non-Nullable Recursion [4/6] Previous pageContentsNext page

11
12
13
14
15
16
17
18
19
20
21
22
23
24
  public void insert (double item) {  
    if (first == null || first.item >= item) {
      first = new Node (item, first);
    } else {
      insertH (first, item);
    }
  }
  private static void insertH (Node x, double item) { 
    if (x.next == null || x.next.item >= item) {
      x.next = new Node (item, x.next);
    } else {
      insertH (x.next, item);
    }
  }

Computes forwards

A single function call both:

  • creates the new node
  • puts it in it's place

Previous pageContentsNext page