CSC300: Linked List Accessor Mistakes

Contents [0/10]

Get your loops straight! [1/10]
Get your loops straight! [2/10]
Common mistakes: Does this work? [3/10]
Common mistakes: Does this work? [4/10]
Common mistakes: Does this work? [5/10]
Common mistakes: Does this work? [6/10]
Common mistakes: Does this work? [7/10]
Common mistakes: Does this work? [8/10]
Common mistakes: Does this work? [9/10]
Common mistakes: Does this work? [10/10]

(Click here for one slide per page)


Get your loops straight! [1/10]

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class LinkedList {
  class Node { double item; Node next; }
  Node first;
  
  public double sum1 () {
    double result = 0;    
    Node current = first; // may be null
    while (current != null) {
      result = result + current.item;
      current = current.next;
    }
  }

  public double sum2 () {
    if (first == null) return 0;
    double result = first.item;    
    Node prev = first; // never null
    while (prev.next != null) {
      result = result + prev.next.item;
      prev = prev.next;
    }
  }

  public double sum3 () {
    if (first == null) return 0;
    double result = first.item;
    Node prev = first;         // never null
    Node current = first.next; // may be null
    while (current != null) {
      result = result + current.item;
      prev = prev.next;
      current = current.next;
    }
  }
}

public static double sum (double[] a) {
  double result = 0;
  int i = 0;
  while (i<a.length) {
     result = result + a[i];
     i = i + 1;
  }
}

Get your loops straight! [2/10]

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class LinkedList {
  class Node { double item; Node next; }
  Node first;
  
  public double numFives1 () {
    double result = 0;    
    Node current = first; // may be null
    while (current != null) {
      if (current.item == 5.0) result = result + 1;
      current = current.next;
    }
  }

  public double numFives2 () {
    if (first == null) return 0;
    double result = 0;
    if (first.item == 5.0) result = 1;
    Node prev = first; // never null
    while (prev.next != null) {
      if (prev.next.item == 5.0) result = result + 1;
      prev = prev.next;
    }
  }

  public double numFives3 () {
    if (first == null) return 0;
    double result = 0;
    if (first.item == 5.0) result = 1;
    Node prev = first;         // never null
    Node current = first.next; // may be null
    while (current != null) {
      if (current.item == 5.0) result = result + 1;
      prev = prev.next;
      current = current.next;
    }
  }
}

public static double numFives (double[] a) {
  double result = 0;
  int i = 0;
  while (i<a.length) {
      if (a[i] == 5.0) result = result + 1;
     i = i + 1;
  }
}

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

01
02
03
04
05
06
07
  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? [4/10]

01
02
03
04
05
06
07
08
09
10
  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? [5/10]

01
02
03
04
05
06
07
08
09
10
  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? [6/10]

01
02
03
04
05
06
07
08
09
10
11
12
13
  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? [7/10]

01
02
03
04
05
06
07
08
09
10
11
12
13
  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? [8/10]

01
02
03
04
05
06
07
08
09
10
11
12
13
  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? [9/10]

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
  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? [10/10]

01
02
03
04
05
06
07
08
09
10
11
  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