Skip to content

Commit d448a58

Browse files
committed
update lecture 3 code:
1 parent 1187e6d commit d448a58

File tree

2 files changed

+19
-49
lines changed

2 files changed

+19
-49
lines changed

lec3_intro3/IntList.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
11
package lec3_intro3;
22

3+
/* An infinitely long list of integers. */
34
public class IntList {
4-
public int first;
5-
public IntList rest;
5+
public int first; // first number in the list
6+
public IntList rest; // rest of the list
67

78
public IntList(int f, IntList r) {
89
first = f;
910
rest = r;
1011
}
1112

13+
/** return the number of items in the list. */
1214
public int size() {
13-
if (rest == null) {
15+
if (this.rest == null) {
16+
// professor what row am I in?
1417
return 1;
1518
}
16-
return 1 + rest.size();
19+
return 1 + this.rest.size();
1720
}
1821

22+
// get the size of the list with iteration
1923
public int iterativeSize() {
20-
IntList p = this;
2124
int totalSize = 0;
25+
IntList p = this;
2226
while (p != null) {
2327
totalSize += 1;
2428
p = p.rest;
2529
}
2630
return totalSize;
2731
}
2832

33+
public int get(int i) {
34+
if (i == 0) {
35+
return this.first;
36+
}
37+
return this.rest.get(i - 1);
38+
}
2939

3040
public static void main(String[] args) {
3141
IntList L = new IntList(15, null);
3242
L = new IntList(10, L);
3343
L = new IntList(5, L);
3444

45+
System.out.println(L.size());
3546
System.out.println(L.iterativeSize());
47+
48+
System.out.println(L.get(1));
3649
}
37-
}
50+
}
Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +0,0 @@
1-
package lec3_intro3.exercises;
2-
import lec3_intro3.IntList;
3-
4-
public class ExtraIntListPractice {
5-
/** Returns an IntList identical to L, but with
6-
* each element incremented by x. L is not allowed
7-
* to change. */
8-
public static IntList incrList(IntList L, int x) {
9-
//TODO fill in code
10-
return null;
11-
}
12-
13-
/** Returns an IntList identical to L, but with
14-
* each element incremented by x. Not allowed to use
15-
* the 'new' keyword. */
16-
public static IntList dincrList(IntList L, int x) {
17-
// TODO fill in code
18-
return null;
19-
}
20-
21-
public static void main(String[] args) {
22-
IntList L = new IntList(5, null);
23-
L.rest = new IntList(7, null);
24-
L.rest.rest = new IntList(9, null);
25-
26-
System.out.println(L.size());
27-
System.out.println(L.iterativeSize());
28-
29-
// Test your answers by uncommenting.
30-
// System.out.println(L.get(1));
31-
IntList listPlus3 = dincrList(L, 3);
32-
for (IntList p = listPlus3; p != null; p = p.rest) {
33-
System.out.print(p.first + ", ");
34-
}
35-
System.out.println();
36-
37-
incrList(L, 4);
38-
for (IntList p = L; p != null; p = p.rest) {
39-
System.out.println(p.first + ", ");
40-
}
41-
System.out.println();
42-
}
43-
}

0 commit comments

Comments
 (0)