-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglyLinkedList.java
324 lines (264 loc) · 6.09 KB
/
SinglyLinkedList.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package ch.hslu.AD.SW02.SinglyLinkedList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
public class SinglyLinkedList<T> implements List<T> {
private int size = 0;
private Node head;
public SinglyLinkedList() {
head = new Node(null);
}
public int size() {
return this.size;
}
@Override
public boolean add(T element) {
add(0, element);
return true;
}
@Override
public void add(int index, T element) {
int currentIndex = 0;
Node currentNode = head;
while(currentNode.hasNext() && currentIndex < index) {
currentNode = currentNode.next();
currentIndex++;
}
Node node = new Node(element);
node.link(currentNode.next());
currentNode.link(node);
size++;
}
@Override
public boolean addAll(Collection<? extends T> c) {
for(T element : c) {
add(element);
}
return true;
}
@Override
public boolean addAll(int index, Collection<? extends T> c) {
return false;
}
@Override
public void clear() {
size = 0;
head = null;
}
@Override
public boolean contains(Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean containsAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
private Node getNode(int index) {
if(index < 0 || index >= size()) {
return null;
}
int currentIndex = 0;
Node current = head;
while(currentIndex < index && current.hasNext()) {
current = current.next();
currentIndex++;
}
return current.next();
}
@Override
public T get(int index) {
Node node = getNode(index);
if(node == null) {
throw new IndexOutOfBoundsException(String.format("Index: %d, Size: %d", index, size()));
}
return node.getElement();
}
@Override
public int indexOf(Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean isEmpty() {
return size == 0 && head == null;
}
@Override
public Iterator<T> iterator() {
return new SinglyLinkedListIterator(head);
}
@Override
public int lastIndexOf(Object o) {
throw new UnsupportedOperationException();
}
@Override
public ListIterator<T> listIterator() {
return new SinglyLinkedListListIterator(null, head, 0, 0);
}
@Override
public ListIterator<T> listIterator(int index) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
Node currentNode = head;
Node previousNode = head;
while(currentNode.hasNext()) {
currentNode = currentNode.next();
if(o.equals(currentNode.getElement())) {
previousNode.link(currentNode.next());
size--;
return true;
}
previousNode = currentNode;
}
return false;
}
@Override
public T remove(int index) {
if(index < 0 || index >= size()) {
throw new IndexOutOfBoundsException(String.format("Index: %d, Size: %d", index, size()));
}
Node currentNode = head;
Node previousNode = head;
int currentIndex = 0;
while(currentNode.hasNext() && currentIndex <= index) {
previousNode = currentNode;
currentNode = currentNode.next();
currentIndex++;
}
T element = currentNode.getElement();
previousNode.link(currentNode.next());
size--;
return element;
}
@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public T set(int index, T element) {
throw new UnsupportedOperationException();
}
@Override
public List<T> subList(int fromIndex, int toIndex) {
throw new UnsupportedOperationException();
}
@Override
public Object[] toArray() {
Object[] o = new Object[size];
int currentIndex = 0;
for(T element : this) {
o[currentIndex++] = element;
}
return o;
}
@Override
public <T> T[] toArray(T[] a) {
throw new UnsupportedOperationException();
}
private class Node {
private T element;
private Node next;
public Node(T element) {
this.element = element;
}
public T getElement() {
return element;
}
public void link(Node next) {
this.next = next;
}
public boolean hasNext() {
return next != null;
}
public Node next() {
return next;
}
}
private class SinglyLinkedListIterator implements Iterator<T> {
private Node current;
public SinglyLinkedListIterator(Node current) {
this.current = current;
}
@Override
public boolean hasNext() {
return current.hasNext();
}
@Override
public T next() {
if(!hasNext()) {
throw new NoSuchElementException();
}
current = current.next();
return current.getElement();
}
}
private class SinglyLinkedListListIterator implements ListIterator<T> {
private Node previous;
private Node current;
private int previousIndex = 0;
private int currentIndex = 0;
public SinglyLinkedListListIterator(Node previous, Node current, int previousIndex, int currentIndex) {
this.previous = previous;
this.current = current;
this.previousIndex = previousIndex;
this.currentIndex = currentIndex;
}
@Override
public void add(T arg0) {
throw new UnsupportedOperationException();
}
@Override
public boolean hasNext() {
return current.hasNext();
}
@Override
public boolean hasPrevious() {
return previous != null;
}
@Override
public T next() {
if(!hasNext()) {
throw new NoSuchElementException();
}
previous = current;
previousIndex = currentIndex++;
current = current.next();
return current.getElement();
}
@Override
public int nextIndex() {
if(!hasNext()) {
throw new NoSuchElementException();
}
return currentIndex + 1;
}
@Override
public T previous() {
throw new UnsupportedOperationException();
}
@Override
public int previousIndex() {
if(!hasPrevious()) {
throw new NoSuchElementException();
}
return previousIndex;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public void set(T arg0) {
Node node = new Node(arg0);
node.link(current.next());
previous.link(node);
current = node;
}
}
}