-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySinglyLinkedList.java
More file actions
452 lines (380 loc) · 11.2 KB
/
MySinglyLinkedList.java
File metadata and controls
452 lines (380 loc) · 11.2 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package kb.data_structures.list;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
/**
* Implementation of custom Singly Linked List data structure. Because of the
* recursive nature of the Linked List this implementation uses a recursion as
* match as possible.
*
* @author dimcho.nedev
*
* @param <T>
*/
public class MySinglyLinkedList<T> implements List<T> {
private final String NOT_SUPPORTED_MSG = "Not supported yet.";
public Node head = null;
int size = 0;
private class Node {
Node() {
}
Node(T value) {
this.value = value;
}
private Node next;
private T value;
}
public MySinglyLinkedList() {
head = new Node();
}
@Override
public boolean add(T e) {
size = add(head, e);
return true;
}
/**
* Appends the specified element to the end of this list.
*
* @param currNode the current {@link Node}
* @param e the value to add
* @return the size of the list after adding the new value
*/
private int add(Node currNode, T e) {
if (currNode.next == null) {
currNode.next = new Node(e);
return 1;
}
return add(currNode.next, e) + 1;
}
@Override
public void add(int index, T element) {
add(head, 0, index, element);
}
// head -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6
/**
*
* @param currNode
* @param currIndex
* @param index
* @param element
*/
private void add(Node currNode, int currIndex, int index, T element) {
if (currIndex == index) {
Node tail = currNode.next;
currNode.next = new Node(element);
currNode.next.next = tail;
size++;
return;
}
add(currNode.next, currIndex + 1, index, element);
}
@Override
public boolean addAll(Collection<? extends T> c) {
for (T element : c)
if (!add(element))
return false;
return c.size() != 0;
}
@Override
public boolean addAll(int index, Collection<? extends T> c) {
for (T element : c)
add(index, element);
return c.size() > 0;
}
@Override
public T get(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException();
return get(index, -1, head);
}
/**
* Returns the element at the specified position in this list.
*
* @param index index of the element to return
* @param currIndex index of the current element
* @param the current {@link Node}
* @return the element at the specified position in this list
*/
private T get(int index, int currIndex, Node currNode) {
if (currIndex == index)
return currNode.value;
return get(index, currIndex + 1, currNode.next);
}
@Override
public boolean remove(Object o) {
return removeByValueRecursively(o, head);
}
/**
* Recursive implementation of the removal operation.
*
* @param o an element to remove
* @param currNode the current {@link Node}
* @return whether this list contained the specified element
*/
public boolean removeByValueRecursively(Object o, Node currNode) {
if (currNode == null)
return false;
if (currNode.next != null && currNode.next.value.equals(o)) {
currNode.next = currNode.next.next;
size--;
return true;
}
return removeByValueRecursively(o, currNode.next);
}
@Override
public T remove(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException();
return removeByIndexRecursively(index, -1, head);
}
public T removeByIndexRecursively(int index, int currIndex, Node currNode) {
if (currNode == null)
return null;
if (index - 1 == currIndex) {
T returnValue = currNode.next.value;
currNode.next = currNode.next.next;
size--;
return returnValue;
}
return removeByIndexRecursively(index, currIndex + 1, currNode.next);
}
@Override
public boolean contains(Object o) {
return contains(o, head.next);
}
/**
* Recursive implementation of the "contains" operation.
*
* @param o element whose presence in this list is to be tested
* @param currNode the current {@link Node}
* @return whether this list contains the specified element
*/
private boolean contains(Object o, Node currNode) {
if (currNode == null)
return false;
if (currNode.value.equals(o))
return true;
return contains(o, currNode.next);
}
@Override
public boolean containsAll(Collection<?> c) {
for (Object element : c)
if (!contains(element))
return false;
return true;
}
@Override
public int indexOf(Object o) {
return findIndexOfRecursively(o, 0, head.next);
}
private int findIndexOfRecursively(Object o, int currIndex, Node currNode) {
if (currNode == null)
return -1;
if (currNode.value.equals(o))
return currIndex;
return findIndexOfRecursively(o, currIndex + 1, currNode.next);
}
@Override
public int lastIndexOf(Object o) {
int lastIndex = -1;
int currIndex = 0;
for (T element : this) {
if (element.equals(o))
lastIndex = currIndex;
currIndex++;
}
return lastIndex;
}
@Override
public T set(int index, T element) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException();
return setRecursively(index, 0, element, head.next);
}
/**
* TODO: Documentation
*
* @param index
* @param currIndex
* @param element
* @param currNode
* @return
*/
public T setRecursively(int index, int currIndex, T element, Node currNode) {
if (currNode == null)
return null;
if (index == currIndex) {
T returnValue = currNode.value;
currNode.value = element;
return returnValue;
}
return setRecursively(index, currIndex + 1, element, currNode.next);
}
@Override
public List<T> subList(int fromIndex, int toIndex) {
if (fromIndex < 0)
throw new IndexOutOfBoundsException();
if (toIndex > size)
throw new IndexOutOfBoundsException();
if (fromIndex > toIndex)
throw new IllegalArgumentException();
return subListRecursive(fromIndex, toIndex, 0, head.next);
}
// Recursive
// TODO:
public List<T> subListRecursive(int fromIndex, int toIndex, int currIndex, Node currNode) {
if (currNode == null)
return new MySinglyLinkedList<T>();
if (currIndex >= fromIndex && currIndex < toIndex) {
List<T> l = subListRecursive(fromIndex, toIndex, currIndex + 1, currNode.next);
l.add(0, currNode.value);
return l;
}
return subListRecursive(fromIndex, toIndex, currIndex + 1, currNode.next);
}
@Override
public boolean removeAll(Collection<?> c) {
for (Object obj : c) {
List<Integer> idxsToRemove = new MySinglyLinkedList<>();
for (int i = 0; i < size; i++)
if (obj.equals(get(i)))
idxsToRemove.add(i);
int rem = 0;
for (Integer idx : idxsToRemove)
remove(idx - rem++);
idxsToRemove = null;
}
return false;
}
// TODO: do it n^2 time. Now it is ~n^3
@Override
public boolean retainAll(Collection<?> c) {
int len = size;
boolean modification = false;
for (int i = 0; i < len; i++) {
if (!c.contains(get(i))) {
remove(i--);
len--;
modification = true;
}
}
return modification;
}
@Override
public Object[] toArray() {
Object[] result = new Object[size];
for (int i = 0; i < size; i++)
result[i] = get(i);
return result;
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public void clear() {
head.next = null;
}
@Override
public Iterator<T> iterator() {
return new Itr();
}
public class Itr implements Iterator<T> {
private int currIdx = 0;
private Node dummyHead = head;
private Node prev = head;
@Override
public boolean hasNext() {
return currIdx < size;
}
@Override
public T next() {
prev = dummyHead;
dummyHead = dummyHead.next;
T next = (T) dummyHead.value;
currIdx++;
return next;
}
@Override
public void remove() {
// TODO: implement it!
prev.next = prev.next.next;
size--;
}
}
@Override
public String toString() {
return Arrays.toString(this.toArray());
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (!(obj instanceof List))
return false;
@SuppressWarnings("unchecked")
final List<T> l1 = (List<T>) obj;
if (size != l1.size()) {
return false;
}
Iterator<T> itr0 = iterator();
Iterator<T> itr1 = l1.iterator();
while (itr0.hasNext()) {
if (!itr0.next().equals(itr1.next()))
return false;
}
return true;
}
public static void main(String[] args) {
MySinglyLinkedList<Integer> l = new MySinglyLinkedList<>();
l.add(0);
l.add(1);
l.add(2);
l.add(3);
l.add(4);
l.add(5);
l.add(6);
l.add(3, 8);
for (int i = 0; i < 8; i++) {
System.out.println(l.get(i));
}
System.out.println(l);
System.out.println(l.head);
System.out.println(l.size);
for (Integer el : l) {
System.out.println(el);
}
LinkedList<Integer> ll = new LinkedList<Integer>();
ll.removeAll(List.of(1, 2, 3));
}
// @Override
// public String toString() {
// head = head.next;
// while (head != null) {
// System.out.println(head.value);
// head = head.next;
// }
//
// return null;
// }
@SuppressWarnings("hiding")
@Override
public <T> T[] toArray(T[] a) {
throw new UnsupportedOperationException(NOT_SUPPORTED_MSG);
}
@Override
public ListIterator<T> listIterator() {
throw new UnsupportedOperationException(NOT_SUPPORTED_MSG);
}
@Override
public ListIterator<T> listIterator(int index) {
throw new UnsupportedOperationException(NOT_SUPPORTED_MSG);
}
}