-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.java
556 lines (507 loc) · 15.1 KB
/
LinkedList.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.ConcurrentModificationException;
/**
* Class LinkedList<E> is a doubly linked list of LinkedListNode
* Such a node contains a reference to some object of type E.
* @param <E>
*/
public class LinkedList<E> implements Iterable<E>
{
private LinkedListNode _head;
private LinkedListNode _tail;
private int _size;
private long _modcount;
/**
* Constructs an empty list
*/
public LinkedList()
{
_head = null;
_tail = null;
_size = 0;
_modcount = 0;
}
/**
* Inserts the specified element at the specified position in this list.
* Shifts the element currently at that position (if any) and any
* subsequent elements to the right (adds one to their indices).
* @param index Param index is the index at which the specified element is to be inserted.
* @param element Param element is the element that is to be inserted.
* @throws IndexOutOfBoundsException Throws IndexOutOfBoundException if index is out of bounds
*/
public void add(int index, E element)
{
if (index < 0 || index > _size)
{
throw new IndexOutOfBoundsException("Invalid Index");
}
// Appending to the end of the list if the condition is true.
if (index == _size)
{
addLast(element);
}
// If the condition above is false we need to insert the element at a specified position.
else if (index ==0)
{
addFirst(element);
}
else
{
LinkedListNode currentNode = _head;
// linear searching the list to find the node at specified index.
for (int i = 0; i < index; i++)
{
currentNode = currentNode.getNext();
}
// Making a new node.
LinkedListNode newNode = new LinkedListNode(element, currentNode.getPrevious(), currentNode);
// Updating the previous node to point to the new node.
if (currentNode.getNext() != null)
{
currentNode.getPrevious().setNext(newNode);
}
else
{
_head = newNode;
}
// Updating the current node to point to the new node.
currentNode.setPrevious(newNode);
_size++;
_modcount++;
}
}
/**
* This method appends the specified element to the end of this list.
* @param element element is the specified element that is to be appended to this list.
* @return returns true
*/
public boolean add(E element)
{
addLast(element);
return true;
}
/**
* Removes all the elements from this list. The list will be empty after this call returns.
*/
public void clear()
{
LinkedListNode currentNode = _head;
while (currentNode != null) {
LinkedListNode nextNode = currentNode.getNext();
currentNode.setNext(null);
currentNode.setPrevious(null);
currentNode = nextNode;
}
_head = null;
_tail = null;
_size = 0;
_modcount ++; // TODO: do I increment modcount in clear?
}
/**
* This is returning the element at the specified position in this list.
* @param index index is the specified index of the element to return.
* @return returns the element at the specified position in this list.
*/
public E get(int index) {
if (index < 0 || index >= _size) {
throw new IndexOutOfBoundsException("Index out of bounds");
}
if (index == _size - 1) {
// Special case: Get the last element (index == _size - 1)
return _tail.getValue();
}
LinkedListNode currentNode = _head;
// Linear searching through the list to find the node at the specified index.
for (int i = 0; i < index; i++) {
currentNode = currentNode.getNext();
}
return currentNode.getValue();
}
/**
* Returns the index of the first occurrence of the specified element in this list,
* or -1 if this list does not contain the element. More formally, returns the
* lowest index i or -1 if there is no such index.
* @param element element is the specified element to search for.
* @return
*/
public int indexOf(E element)
{
int index = 0;
LinkedListNode current = _head;
//Linear searching through the whole list
while (current != null) {
if ((element == null && current.getValue() == null) || (element != null && element.equals(current.getValue()))) {
return index;
}
current = current.getNext();
index++;
}
return -1;
}
/**
* returns true if this list contains no elements.
* @return returns true if this list contains no elements.
*/
public boolean isEmpty()
{
return _size == 0;
}
/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left. Returns the element
* that was removed from the list.
* @param index index is the index of the element to be removed.
* @return returning the element previously at the specified position.
* @Throws throws index out of bounds exception
*/
public E remove(int index)
{
if (index < 0 || index >= _size)
{
throw new IndexOutOfBoundsException("Index out of bounds");
}
// If index = 0 call my remove first method and same for last
if (index == 0)
{
return removeFirst();
}
else if (index == _size - 1)
{
return removeLast();
}
else
{
// If the index is not found in the first or last this is how I
// find the node at the specified index.
LinkedListNode currentNode = _head;
for(int i = 0; i < index; i++)
{
currentNode = currentNode.getNext();
}
// Updates the reverence for previous and next.
LinkedListNode previous = currentNode.getPrevious();
LinkedListNode next = currentNode.getNext();
previous.setNext(next);
next.setPrevious(previous);
_size--;
_modcount++;
// Returning the element that was removed.
return currentNode.getValue();
}
}
public E set(int index, E element) {
if (index < 0 || index >= _size) {
throw new IndexOutOfBoundsException("Index out of bounds");
}
if (index == _size - 1) {
// Special case: Set the last element (index == _size - 1)
if (_size == 1) {
// If theres only one element in the list, update both head and tail.
E removedElement = _head.getValue();
_head.setValue(element);
_tail.setValue(element);
return removedElement;
} else {
// If theres more than one element, update the tail.
E removedElement = _tail.getValue();
_tail.setValue(element);
return removedElement;
}
}
LinkedListNode currentNode = _head;
// Linear searching through the list to find the node at the specified index.
for (int i = 0; i < index; i++) {
currentNode = currentNode.getNext();
}
// Storing the value to be replaced
E removedElement = currentNode.getValue();
// Updating the value of the node at the specified index with the new element.
currentNode.setValue(element);
return removedElement;
}
/**
* Returns the number of elements in this list.
* @return returns the number of elements in this list.
*/
public int size()
{
return _size;
}
/**
* Method that returns a new LinkedListIterator.
* @return Returns a new LinkedListIterator.
*/
public Iterator<E> iterator()
{
return new LinkedListIterator(false);
}
/**
* Returns an iterator over the elements in this deque in reverse sequential order.
* the elements will be returned in order from last to first.
* @return Return the elements from tail to head.
*/
public Iterator<E> reverseIterator()
{
return new LinkedListIterator(true);
}
/**
* Helper method that is used for simplicity and exception handling within
* the add methods.
* @param element Element to be added first.
*/
private void addFirst(E element)
{
if (isEmpty())
{
// If the list is empty, create a new node with the provided element
// and make it both the head and the tail of the list.
_head = new LinkedListNode(element);
_tail = _head;
}
else
{
// If the list is not empty, create a new node with the provided element,
// and set its "next" reference to the current head.
LinkedListNode newNode = new LinkedListNode(element, null, _head);
// Update the previous reference of the current head to point to the new node.
_head.setPrevious(newNode);
// Update the head of the list to be the new node.
_head = newNode;
}
_size++;
}
/**
* Helper method that is used for simplicity and exception handling within
* the add methods.
* @param element Element to be added.
*/
private void addLast(E element)
{
if (isEmpty())
{
_head = new LinkedListNode(element);
_tail = _head;
}
else
{
LinkedListNode newNode = new LinkedListNode(element, _tail, null);
_tail.setNext(newNode);
_tail = newNode;
}
_size++;
}
/**
* Helper method used when removing element. Specifically the first node.
* @return Returning the element to be removed.
*/
private E removeFirst()
{
if (_head == null)
{
throw new NoSuchElementException("List is empty");
}
// Getting the value of the first element.
E elementRemoved = _head.getValue();
if (_head == _tail)
{
// If theirs only one element, set both the head and tail
// to null making the lit empty
_head = null;
_tail = null;
}
// Updating the head to be the next element.
else
{
_head = _head.getNext();
if (_head != null)
{
_head.setPrevious(null);
}
}
_size--;
return elementRemoved;
}
/**
* Helper method used when removing element. Specifically the last node.
* @return Returning the element to be removed.
*/
private E removeLast()
{
if (_tail == null)
{
throw new NoSuchElementException("List is empty");
}
E elementRemoved = _tail.getValue();
if (_head == _tail)
{
_head = null;
_tail = null;
}
else
{
_tail = _tail.getPrevious();
_tail.setNext(null);
}
_size--;
return elementRemoved;
}
/**
* Class LinkedListNode is used for initializing each individual node.
* and assigning it a previous, next, and its value.
*/
public class LinkedListNode
{
private E _value;
private LinkedListNode _previous;
private LinkedListNode _next;
/**
* Constructor LinkedListNode used to a null value.
*/
public LinkedListNode()
{
this(null);
}
/**
* Constructor LinkedListNode is used to assign a value given to each node.
* @param value Value is the value given to be stored.
*/
public LinkedListNode(E value)
{
this(value, null, null);
}
/**
* Constructor LinkedlistNode is used to initialize the value, previous
* and next for each node.
* @param value Value is being assigned to _value.
* @param prev Prev is being assigned to _prev.
* @param next Next is being assigned to _next.
*/
public LinkedListNode(E value, LinkedListNode prev, LinkedListNode next)
{
_value = value;
_previous = prev;
_next = next;
}
/**
* getValue method returns the _value of node.
* @return Returning value of the node.
*/
public E getValue()
{
return _value;
}
/**
* getPrevious returns previous node.
* @return Returns previous node.
*/
public LinkedListNode getPrevious()
{
return _previous;
}
/**
* getNext returns the next value in the list.
* @return returns next node.
*/
public LinkedListNode getNext()
{
return _next;
}
/**
* setValue is assigning _value to value.
* @param value Value is the given value for the node.
*/
public void setValue(E value)
{
this._value = value;
}
/**
* setPrevious is assigning _previous to prev.
* @param prev Prev is the setting _previous to prev.
*/
public void setPrevious(LinkedListNode prev)
{
this._previous = prev;
}
/**
* setNext method is assigning the next LinkedListNode to next.
* @param next _next is being set to next
*/
public void setNext(LinkedListNode next)
{
this._next = next;
}
}
/**
* Inner class that implements Iterator interface, and it uses the LinkedListNode
* and new variable cursor to keep track of where the iterator is through hasNext
* method.
*/
public class LinkedListIterator implements Iterator<E>
{
private LinkedListNode _cursor;
private boolean _reverse;
private long _expectedModCount;
/**
*
* @param reverse
*/
public LinkedListIterator(boolean reverse)
{
this._reverse = reverse;
// Get the current mod count.
this._expectedModCount = _modcount;
if (reverse)
{
_cursor = _tail;
}
else
{
_cursor = _head;
}
}
/**
* hasNext method checks for a concurrent modification exception throughout the iteration.
* and throws an error if the list has been modified during the iteraton.
* @return Returning _cursor != null.
*/
public boolean hasNext()
{
if (_expectedModCount != _modcount)
{
throw new ConcurrentModificationException("List has been modified");
}
return _cursor != null;
}
/**
* next() is a method that checks for concurrent modification exceptions
* and throws an error if the list was modified through the iteration.
* @return
*/
public E next()
{
if (_expectedModCount != _modcount)
{
throw new ConcurrentModificationException("List has been modified");
}
if (!hasNext())
{
throw new NoSuchElementException();
}
E element = _cursor.getValue();
if (_reverse)
{
_cursor = _cursor.getPrevious();
}
else
{
_cursor = _cursor.getNext();
}
if (_expectedModCount != _modcount)
{
throw new ConcurrentModificationException("List has been modified");
}
return element;
}
}
}