/
SLList.java
executable file
·50 lines (40 loc) · 968 Bytes
/
SLList.java
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
/** An SLList is a list of integers, which hides the terrible truth
* of the nakedness within. */
public class SLList<LochNess> {
private class StuffNode {
public LochNess item;
public StuffNode next;
public StuffNode(LochNess i, StuffNode n) {
item = i;
next = n;
}
}
private StuffNode first;
private int size;
public SLList(LochNess x) {
first = new StuffNode(x, null);
size = 1;
}
/** Adds x to the front of the list. */
public void addFirst(LochNess x) {
first = new StuffNode(x, first);
size += 1;
}
/** Returns the first item in the list. */
public LochNess getFirst() {
return first.item;
}
/** Adds an item to the end of the list. */
public void addLast(LochNess x) {
size += 1;
StuffNode p = first;
/* Move p until it reaches the end of the list. */
while (p.next != null) {
p = p.next;
}
p.next = new StuffNode(x, null);
}
public int size() {
return size;
}
}