-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegerList.java
146 lines (120 loc) · 3.21 KB
/
IntegerList.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class IntegerList
implements Collection<Integer> {
private class ListNode {
private ListNode next;
private Integer val;
private ListNode(Integer val) {
this.val = val;
this.next = null;
}
}
private class IntegerListIterator implements Iterator<Integer> {
private ListNode cursor; // next node returned by next()
private IntegerListIterator(ListNode head) {
cursor = head;
}
@Override
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
ListNode current = cursor;
cursor = cursor.next;
return current.val;
}
@Override
public boolean hasNext() {
return cursor != null;
}
}
private ListNode head = null;
@Override
public Iterator<Integer> iterator() {
return new IntegerListIterator(head);
}
@Override
public void clear() {
head = null;
}
@Override
public boolean isEmpty() {
return null == head;
}
@Override
public int size() {
int size = 0;
for (ListNode cursor = head;
cursor != null;
cursor = cursor.next) {
++size;
}
return size;
}
@Override
public boolean add(Integer val) {
ListNode cursor = null, prev = null;
for (cursor = head; cursor != null; cursor = cursor.next) {
prev = cursor;
}
if (null == prev) {
head = new ListNode(val);
} else {
prev.next = new ListNode(val);
}
return true;
}
@Override
public boolean remove(Object o) {
ListNode cursor = null, prev = null;
for (cursor = head;
!cursor.val.equals(o) && cursor != null;
cursor = cursor.next) {
prev = cursor;
}
if (null != cursor) {
prev.next = cursor.next;
return true;
} else {
return false;
}
}
@Override
public boolean contains(Object o) {
ListNode cursor = head;
while (!cursor.val.equals(o) && cursor != null) {
cursor = cursor.next;
}
return cursor != null;
}
@Override
public boolean containsAll(Collection<?> c) {
boolean contains = true;
for (Object o: c) {
contains &= this.contains(o);
}
return contains;
}
@Override
public boolean addAll(Collection<? extends Integer> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public Integer[] toArray() {
throw new UnsupportedOperationException();
}
@Override
public <T> T[] toArray(T[] a) {
throw new UnsupportedOperationException();
}
}