-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntList.java
More file actions
39 lines (31 loc) · 789 Bytes
/
IntList.java
File metadata and controls
39 lines (31 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
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
public class IntList {
private ConsCell start; // first in the list or null
public IntList(ConsCell s){
start = s;
}
public IntList cons(int h){
return new IntList(new ConsCell(h, start));
}
public ConsCell getStart(){
return start;
}
public int length(){
int len = 0;
ConsCell current = start;
while (current != null){
len++;
current = current.getTail();
}
return len;
}
public void print(){
System.out.print("[");
ConsCell a = start;
while (a != null) {
System.out.print(a.getHead());
a = a.getTail();
if (a != null) System.out.print(",");
}
System.out.println("]");
}
}