-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsCell.java
More file actions
70 lines (55 loc) · 1.53 KB
/
ConsCell.java
File metadata and controls
70 lines (55 loc) · 1.53 KB
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
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
/**
* A conscell is an element in a linked list of INTs
*/
//if class is public, the file needs to have the same name
public class ConsCell {
private int head; // first item in the list
private ConsCell tail; // rest of the list or null
/**
* Construct a new ConsCell given its head and tail
* @param h the int contents of this cell
* @param t the next ConsCell in the list or null
*/
public ConsCell(int h, ConsCell t){
head = h;
tail = t;
}
/**
* accessor for the head of this conscell
* @return the int contents of this cell
*/
public int getHead(){
return head;
}
public ConsCell getTail(){
return tail;
}
public void setTail(ConsCell t){
tail = t;
}
/**
* Get the length of a list of ConsCells
* @param a the first ConsCell in the list of null
* @return the int length
*/
public static int length(ConsCell a){
int len = 0;
while (a != null){
len++;
a = a.getTail();
}
return len;
}
/**
* Retun the result of consing an int onto a list of ConsCells
* @param a the int to cons onto the list
* @param b the fist ConsCell in the list or null
* @return the first ConsCell in the new list
*/
public static ConsCell cons(int a, ConsCell b){
return new ConsCell(a,b);
}
public static void main(String[] args) {
System.out.println("print line");
}
}