CSC300: Linked List Accessor Mistakes

Contents [0/8]

Common mistakes: Does this work? [1/8]
Common mistakes: Does this work? [2/8]
Common mistakes: Does this work? [3/8]
Common mistakes: Does this work? [4/8]
Common mistakes: Does this work? [5/8]
Common mistakes: Does this work? [6/8]
Common mistakes: Does this work? [7/8]
Common mistakes: Does this work? [8/8]

(Click here for one slide per page)


Common mistakes: Does this work? [1/8]

11
12
13
14
15
16
17
  public int numFives () {
    int result = 0;
    for (Node x = first; x != null; x = x.next) 
      if (x.item == 5.0)
        result = result + 1;
    return result;
  }

Common mistakes: Does this work? [2/8]

11
12
13
14
15
16
17
18
19
20
  public int numFives () {
    Node x = first;
    int result = 0;
    while (x != null) {
      if (x.item == 5.0)
        result = result + 1;
      x = x.next; 
    }
    return result;
  }

Common mistakes: Does this work? [3/8]

11
12
13
14
15
16
17
18
19
20
  public int numFives () {
    Node x = first;
    int result = 0;
    while (x != null) {
      if (x.item == 5.0)
        result = result + 1;
      x = x.next.next; 
    }
    return result;
  }

Common mistakes: Does this work? [4/8]

11
12
13
14
15
16
17
18
19
20
21
22
23
  public int numFives () {
    if (first == null) return 0;
    int result = 0;
    if (first.item == 5.0) 
      result = result + 1;
    Node x = first;
    while (x.next != null) {
      if (x.next.item == 5.0)
        result = result + 1;
      x.next = x.next.next; 
    }
    return result;
  }

Common mistakes: Does this work? [5/8]

11
12
13
14
15
16
17
18
19
20
21
22
23
  public int numFives () {
    if (first == null) return 0;
    int result = 0;
    if (first.item == 5.0) 
      result = result + 1;
    Node x = first;
    while (x.next != null) {
      if (x.next.item == 5.0)
        result = result + 1;
      x = x.next; 
    }
    return result;
  }

Common mistakes: Does this work? [6/8]

11
12
13
14
15
16
17
18
19
20
21
22
23
  public int numFives () {
    if (first == null) return 0;
    int result = 0;
    if (first.item == 5.0) 
      result = result + 1;
    Node x = first;
    while (x.next != null) {
      if (x.item == 5.0)
        result = result + 1;
      x = x.next; 
    }
    return result;
  }

Common mistakes: Does this work? [7/8]

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  public int numFives () {
    if (first.next == null) return 0;
    Node x = first;
    int result = 0;
    while (x.next != null) {
      if (x.item != 5.0) x = x.next;
      if (x.item == 5.0) {
        result = result + 1;
        x = x.next; 
      }
    }
    if (x.next == null && x.item == 5.0) {
      result = result + 1;
    }
    return result;
  }

Common mistakes: Does this work? [8/8]

11
12
13
14
15
16
17
18
19
20
21
  public int numFives () {
    if (first == null) return 0;
    Node x = first;
    Node y = first.next;
    int result = 0;
    while (x != null) {
      if (x.item == 5.0) result = result + 1;
      x = y;
    }
    return result;
  }

Revised: 2008/03/17 13:01