-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCircularList.java
68 lines (56 loc) · 1.33 KB
/
CircularList.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.Iterator;
public class CircularList<Item> implements Iterable<Item> {
private Node last;
private int N;
public Item getItem() {
return last.item;
}
private void linkEnds() {
Node current = last;
for(int i = N; i > 0; i--) {
if (i == 1) current.next = last;
current = current.next;
}
}
public void add(Item item) {
Node node = new Node();
node.item = item;
if (last == null) {
last = node;
node.next = last;
} else {
node.next = last;
last = node;
}
N++;
linkEnds();
}
public int size() {
return N;
}
public boolean isEmpty() {
return N != 0;
}
private class Node {
Item item;
Node next;
}
public Iterator<Item> iterator() {
return new ListIterator();
}
private class ListIterator implements Iterator<Item> {
private Node current = last;
private int count = 0;
public boolean hasNext() {
return count < 3 * N;
}
public Item next() {
count++;
Item item = current.item;
current = current.next;
return item;
}
public void remove() {
}
}
}