CSC300 / CSC402: Linked List Accessors

Contents [0/6]

Starter code [1/6]
Loop [2/6]
Starter code with both array and linked list [3/6]
Another Problem [4/6]
Loop using current [5/6]
Loop using previous [6/6]

(Click here for one slide per page)


Starter code [1/6]

file:Playground.java [source]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package algs13;
import java.text.DecimalFormat;
import stdlib.*;
public class Playground {
  private Node first;
  static class Node { 
    public double item; 
    public Node next; 
    public Node (double item, Node next) { 
      this.item = item; 
      this.next = next; 
    }
  }

  /* A silly main program to show list creation */
  public static void main (String[] args) {
    Trace.showObjectIdsRedundantly(true);
    Trace.drawStepsOfMethod ("main");
    //Trace.drawStepsOfMethod ("makeExample");
    //Trace.drawSteps();
    Trace.run ();
    Playground example1 = Playground.makeExample (1);
    Playground example2 = Playground.makeExample (5);
    Playground example3 = Playground.makeExample (7);
    Node x = example1.first;
    StdOut.println (x.item);
  }
  public static Playground makeExample(int i) {
    Node x1 = new Node (i+10, null);
    Node x2 = new Node (i+20, null);
    Node x3 = new Node (i+30, null);
    Node x4 = new Node (i+40, null);
    Playground result = new Playground ();
    result.first = x1;
    x1.next = x2;
    x2.next = x3;
    x3.next = x4;
    return result;
  }
  
  public static void main2 (String[] args) {
    //Trace.showObjectIdsRedundantly(true);
    Trace.drawStepsOfMethod ("numFives");
    Trace.drawStepsOfMethod ("numFivesH");
    Trace.run ();
    Playground list1 = Playground.from ("5 11 5 5");
    Playground list2 = Playground.from ("24 35 67");
    int result1 = list1.numFives ();
    Trace.draw();
    StdOut.println ("result: " + result1);
  }
  public int numFives () {
    return StdRandom.uniform (100); //TODO: fix this
  }
  public static void main3 (String[] args) {
    testNumFives (2, "11 5 5 21");
    testNumFives (0, "11 21 31 41");
    testNumFives (1, "11 21 5 31 41");
    testNumFives (1, "5 11 21 31 41");
    testNumFives (1, "11 21 31 41 5");
    testNumFives (5, "5 11 21 5 5 31 5 41 5");
    testNumFives (3, "5 5 5");
    testNumFives (1, "5");
    testNumFives (0, "11");
    testNumFives (0, "");
    StdOut.println ("Finished tests");
  }

  private static void testNumFives (int expected, String sList) {
    Playground list = Playground.from (sList); 
    String sStart = list.toString ();
    int actual = list.numFives ();
    if (expected != actual) {
      StdOut.format ("Failed: Expecting [%d] Actual [%d] with argument %s\n", expected, actual, list);
    }
    String sEnd = list.toString ();
    if (! sStart.equals (sEnd)) {
      StdOut.format ("Failed %s.numFives(): List changed to %s\n", sStart, sEnd);
    }
  }

  /* ToString method to print */
  public String toString () { 
    // Use DecimalFormat #.### rather than String.format 0.3f to leave off trailing zeroes
    DecimalFormat format = new DecimalFormat ("#.###");
    StringBuilder result = new StringBuilder ("[ ");
    for (Node x = first; x != null; x = x.next) {
      result.append (format.format (x.item));
      result.append (" ");
    }
    result.append ("]");
    return result.toString ();
  }

  /* Method to create lists */
  public static Playground from(String s) {
    Playground result = new Playground ();
    if ("".equals (s)) return result;

    Node first = null;
    String[] nums = s.split (" ");
    for (int i = nums.length-1; i >= 0; i--) {
      try { 
        double num = Double.parseDouble (nums[i]); 
        first = new Node (num, first);      
      } catch (NumberFormatException e) {
        throw new IllegalArgumentException (String.format ("Bad argument \"%s\": could not parse \"%s\" as a double", s, nums[i]));
      }
    }
    result.first = first;
    return result;
  }
    private static Playground of(double... items) {
        Node next = null;
        for (int i = items.length - 1; i >= 0; i--) {
            next = new Node(items[i], next);
        }
        Playground result = new Playground();
        result.first = next;
        return result;
    }
}

Loop [2/6]

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

For [5,11,5,5], the values of x (line 14) are

x==[5,11,5,5], result==0
  x==[11,5,5], result==1
     x==[5,5], result==1
       x==[5], result==2
        x==[], result==3 (x==null)

There is an image to the right of the code.
Click to animate in single-slide mode.
To enter single-slide mode, click the slide number in the title bar.

numFivesLIO-

Starter code with both array and linked list [3/6]

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package algs13;
import java.text.DecimalFormat;
import stdlib.*;
public class Playground {
  private double a[]; 
  private Node first;
  static class Node { 
    public double item; 
    public Node next; 
    public Node (double item, Node next) { 
      this.item = item; 
      this.next = next; 
    }
  }

  public int numFives () {
    return StdRandom.uniform (100); //TODO: fix this
  }

  /* ToString method to print */
  public String toString () { 
    // Use DecimalFormat #.### rather than String.format 0.3f to leave off trailing zeroes
    DecimalFormat format = new DecimalFormat ("#.###");
    StringBuilder result = new StringBuilder ("[ ");
    for (Node x = first; x != null; x = x.next) {
      result.append (format.format (x.item));
      result.append (" ");
    }
    result.append ("]");
    return result.toString ();
  }

  /* Method to create lists */
  public static Playground of(String s) {
    Playground result = new Playground ();
    if ("".equals (s)) {
      // special case for the empty array
      result.first = null;
      result.a = new double [0];
    } else {
      String[] nums = s.split (" ");
      Node first = null;
      double[] a = new double[nums.length];
      for (int i = nums.length-1; i >= 0; i--) {
        double num = Double.NaN;
        try { 
          num = Double.parseDouble (nums[i]); 
        } catch (NumberFormatException e) { 
          throw new IllegalArgumentException (String.format ("Bad argument \"%s\": could not parse \"nums[i]\" as a double", s, nums[i]));
        }
        a[i] = num;
        first = new Node (num, first);  
      }
      result.a = a;
      result.first = first;
    } 
    return result;
  }

  public static void testNumFives (int expected, String sList) {
    Playground list = Playground.of (sList); 
    int actual = list.numFives ();
    if (expected != actual) {
      StdOut.format ("Failed: Expecting [%d] Actual [%d] with argument %s\n", expected, actual, list);
    }
  }
  public static void main (String[] args) {
    testNumFives (0, "");
    testNumFives (1, "5");
    testNumFives (0, "11");
    testNumFives (3, "5 5 5");
    testNumFives (0, "11 21 31 41");
    testNumFives (1, "5 11 21 31 41");
    testNumFives (1, "11 21 31 41 5");
    testNumFives (1, "11 21 5 31 41");
    testNumFives (5, "5 11 21 5 5 31 5 41 5");
    StdOut.println ("Finished tests");
  }
  public static void main1 (String[] args) {
    Trace.drawStepsOfMethod ("numFives");
    Trace.drawStepsOfMethod ("numFivesH");
    Trace.run ();
    Playground list0 = Playground.of ("5");
    Playground list1 = Playground.of ("5 11 5 5");
    int result = list1.numFives ();
    StdOut.println ("result: " + result);
  }
}

Another Problem [4/6]

file:PlaygroundNumUnique.java [source]
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package algs13;
import java.text.DecimalFormat;
import stdlib.*;
public class PlaygroundNumUnique {
  private Node first;
  static class Node { 
    public double item; 
    public Node next; 
    public Node (double item, Node next) { 
      this.item = item; 
      this.next = next; 
    }
  }
  
  /** Number of unique items, assuming the list is sorted */
  public int numUnique() {
    return StdRandom.uniform (100); //TODO: fix this
  }

  public static void main1 (String[] args) {
    Trace.showBuiltInObjects(true);
    Trace.drawStepsOfMethod ("numUnique");
    Trace.run ();
    PlaygroundNumUnique list1 = PlaygroundNumUnique.from ("11 21 21 21 31");
    int result1 = list1.numUnique ();
    Trace.draw();
    StdOut.println ("result: " + result1);
  }

  public static void main (String[] args) {
    testNumUnique(4, "11 21 21 21 31 41 41 41 41");
    testNumUnique(1, "11 11 11 11");
    testNumUnique(4, "11 21 31 41");
    testNumUnique(4, "11 11 11 21 31 31 31 31 41");
    testNumUnique(4, "11 11 21 21 21 31 31 41 41 41 41");
    testNumUnique(8, "11 11 11 11 21 31 41 41 41 41 41 51 51 61 71 81 81");
    testNumUnique(8, "11 21 31 41 41 41 41 41 51 51 61 71 81");
    testNumUnique(7, "11 11 11 11 21 31 41 41 41 41 41 51 51 61 71");
    testNumUnique(7, "11 21 31 41 41 41 41 41 51 51 61 71");
    testNumUnique(8, "-81 -81 -81 -81 -71 -61 -51 -51 -51 -51 -41 -41 -31 -21 -11 -11 -11");
    testNumUnique(3, "-11 -11 -11 0 0 11 11 11");
    testNumUnique(2, "0 11 11 11");
    testNumUnique(2, "-Infinity 11 11 11");
    testNumUnique(2, "11 11 11 Infinity");
    testNumUnique(1, "11 11");
    testNumUnique(1, "11");
    testNumUnique(0, "");
    
    StdOut.println ("Finished tests");
  }

  private static void testNumUnique (int expected, String sList) {
    PlaygroundNumUnique list = PlaygroundNumUnique.from (sList); 
    String sStart = list.toString ();
    int actual = list.numUnique ();
    if (expected != actual) {
      StdOut.format ("Failed: Expecting [%d] Actual [%d] with argument %s\n", expected, actual, list);
    }
    String sEnd = list.toString ();
    if (! sStart.equals (sEnd)) {
      StdOut.format ("Failed %s.numUnique(): List changed to %s\n", sStart, sEnd);
    }
  }

  /* ToString method to print */
  public String toString () { 
    // Use DecimalFormat #.### rather than String.format 0.3f to leave off trailing zeroes
    DecimalFormat format = new DecimalFormat ("#.###");
    StringBuilder result = new StringBuilder ("[ ");
    for (Node x = first; x != null; x = x.next) {
      result.append (format.format (x.item));
      result.append (" ");
    }
    result.append ("]");
    return result.toString ();
  }

  /* Method to create lists */
  public static PlaygroundNumUnique from(String s) {
    PlaygroundNumUnique result = new PlaygroundNumUnique ();
    if ("".equals (s)) return result;

    Node first = null;
    String[] nums = s.split (" ");
    for (int i = nums.length-1; i >= 0; i--) {
      try { 
        double num = Double.parseDouble (nums[i]); 
        first = new Node (num, first);      
      } catch (NumberFormatException e) {
        throw new IllegalArgumentException (String.format ("Bad argument \"%s\": could not parse \"%s\" as a double", s, nums[i]));
      }
    }
    result.first = first;
    return result;
  }
}

Loop using current [5/6]

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  public int numUnique() {
    if (first == null) return 0;
    int result = 1;
    Node curr = first.next;
    double prevVal = first.item;
    while (curr != null) {

      double currVal = curr.item;
      if (currVal != prevVal) {
        result += 1;
        prevVal = currVal;
      }
      curr = curr.next;
    }
    return result;
  }
NumUniqueCurr-

Loop using previous [6/6]

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  public int numUnique() {
    if (first == null) return 0;
    int result = 1;
    
    Node prev = first;
    while (prev.next != null) {     
      double prevVal = prev.item;
      double currVal = prev.next.item;
      if (currVal != prevVal) {
        result += 1;

      }
      prev = prev.next;
    }
    return result;
  }
NumUniquePrev-