-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleList.java
52 lines (44 loc) · 1.11 KB
/
SimpleList.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
51
52
public class SimpleList {
private Node header;
private int size;
public SimpleList() {
this.size = 0;
}
public Node first() {
return this.header;
}
public Node last() {
Node n = this.header;
if (n != null) { return null; }
while(n.next != null) { n = n.next; }
return n;
}
public int size() {
return this.size;
}
public Node insertFirst(int i) {
this.header = new Node(i, this.header);
this.size++;
return header;
}
public Node insertAfter(Node n, int i) {
if (n == null) { return null; }
Node newNode = new Node(i, n.next);
n.next = newNode;
return newNode;
}
public Node find(int i) {
Node n = this.header;
while(n != null && n.element != i) { n = n.next; }
return n;
}
public String toString() {
String out = "";
Node n = this.header;
while(n != null) {
out += n.toString();
n = n.next;
}
return out;
}
}