-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDLList.java
More file actions
502 lines (433 loc) · 11.8 KB
/
DLList.java
File metadata and controls
502 lines (433 loc) · 11.8 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
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
package doublylinkedlist;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* This provides implementation for some of the LList methods.
*
* @author Mark Wiggans (mmw125)
* @version 3/29/15
* @author Eric Williamson
* @version 10/30/15
* @author maellis1
* @version 11/1/15
* @param <E>
* The type of object the class will store
*/
public class DLList<E> {
/**
* This represents a node in a doubly linked list. This node stores data, a
* pointer to the node before it in the list, and a pointer to the node
* after it in the list
*
* @param <E>
* This is the type of object that this class will store
* @author Mark Wiggans (mmw125)
* @version 4/14/2015
*/
private static class Node<E> {
private Node<E> next;
private Node<E> previous;
private E data;
/**
* Creates a new node with the given data
*
* @param d
* the data to put inside the node
*/
public Node(E d) {
data = d;
}
/**
* Sets the node after this node
*
* @param n
* the node after this one
*/
public void setNext(Node<E> n) {
next = n;
}
/**
* Sets the node before this one
*
* @param n
* the node before this one
*/
public void setPrevious(Node<E> n) {
previous = n;
}
/**
* Gets the next node
*
* @return the next node
*/
public Node<E> next() {
return next;
}
/**
* Gets the node before this one
*
* @return the node before this one
*/
public Node<E> previous() {
return previous;
}
/**
* Gets the data in the node
*
* @return the data in the node
*/
public E getData() {
return data;
}
}
/**
* How many nodes are in the list
*/
private int size;
/**
* The first node in the list. THIS IS A SENTINEL NODE AND AS SUCH DOES NOT
* HOLD ANY DATA. REFER TO init()
*/
private Node<E> head;
/**
* The last node in the list. THIS IS A SENTINEL NODE AND AS SUCH DOES NOT
* HOLD ANY DATA. REFER TO init()
*/
private Node<E> tail;
/**
* Create a new DLList object.
*/
public DLList() {
init();
}
/**
* Initializes the object to have the head and tail nodes
*/
private void init() {
head = new DLList.Node<E>(null);
tail = new DLList.Node<E>(null);
head.setNext(tail);
tail.setPrevious(head);
size = 0;
}
/**
* Checks if the array is empty
*
* @return true if the array is empty
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Gets the number of elements in the list
*
* @return the number of elements
*/
public int size() {
return size;
}
/**
* Removes all of the elements from the list
*/
public void clear() {
init();
}
/**
* Checks if the list contains the given object
*
* @param obj
* the object to check for
* @return true if it contains the object
*/
public boolean contains(E obj) {
return lastIndexOf(obj) != -1;
}
/**
* Gets the object at the given position
*
* @param index
* where the object is located
* @return The object at the given position
* @throws IndexOutOfBoundsException
* if there no node at the given index
*/
public E get(int index) {
return getNodeAtIndex(index).getData();
}
/**
* Adds a element to the end of the list
*
* @param newEntry
* the element to add to the end
*/
public void add(E newEntry) {
add(size(), newEntry);
}
/**
* Adds the object to the position in the list
*
* @param index
* where to add the object
* @param obj
* the object to add
* @throws IndexOutOfBoundsException
* if index is less than zero or greater than size
* @throws IllegalArgumentException
* if obj is null
*/
public void add(int index, E obj) {
if (index < 0 || size < index) {
throw new IndexOutOfBoundsException();
}
if (obj == null) {
throw new IllegalArgumentException("Cannot add null "
+ "objects to a list");
}
Node<E> nodeAfter;
if (index == size) {
nodeAfter = tail;
}
else {
nodeAfter = getNodeAtIndex(index);
}
Node<E> addition = new Node<E>(obj);
addition.setPrevious(nodeAfter.previous());
addition.setNext(nodeAfter);
nodeAfter.previous().setNext(addition);
nodeAfter.setPrevious(addition);
size++;
}
/**
* gets the node at that index
*
* @param index
* @return node at index
*/
private Node<E> getNodeAtIndex(int index) {
if (index < 0 || size() <= index) {
throw new IndexOutOfBoundsException("No element exists at "
+ index);
}
Node<E> current = head.next(); // as we have a sentinel node
for (int i = 0; i < index; i++) {
current = current.next();
}
return current;
}
/**
* Gets the last time the given object is in the list
*
* @param obj
* the object to look for
* @return the last position of it. -1 If it is not in the list
*/
public int lastIndexOf(E obj) {
/*
* We should go from the end of the list as then we an stop once we find
* the first one
*/
Node<E> current = tail.previous();
for (int i = size() - 1; i >= 0; i--) {
if (current.getData().equals(obj)) {
return i;
}
current = current.previous();
}
return -1; // if we do not find it
}
/**
* Removes the element at the specified index from the list
*
* @param index
* where the object is located
* @throws IndexOutOfBoundsException
* if there is not an element at the index
* @return true if successful
*/
public boolean remove(int index) {
Node<E> nodeToBeRemoved = getNodeAtIndex(index);
nodeToBeRemoved.previous().setNext(nodeToBeRemoved.next());
nodeToBeRemoved.next().setPrevious(nodeToBeRemoved.previous());
size--;
return true;
}
/**
* Removes the first object in the list that .equals(obj)
*
* @param obj
* the object to remove
* @return true if the object was found and removed
*/
public boolean remove(E obj) {
Node<E> current = head.next();
while (!current.equals(tail)) {
if (current.getData().equals(obj)) {
current.previous().setNext(current.next());
current.next().setPrevious(current.previous());
size--;
return true;
}
current = current.next();
}
return false;
}
/**
* Returns a string representation of the list If a list contains A, B, and
* C, the following should be returned "{A, B, C}" (Without the quotations)
*
* @return a string representing the list
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder("{");
if (!isEmpty()) {
Node<E> currNode = head.next();
while (currNode != tail) {
E element = currNode.getData();
builder.append(element.toString());
if (currNode.next != tail) {
builder.append(", ");
}
currNode = currNode.next();
}
}
builder.append("}");
return builder.toString();
}
/**
* Iterator method creates Iterator object
*
* @return new Iterator object
*/
public Iterator<E> iterator() {
return new DLListIterator<E>();
}
/**
* Makes new iterator
*
* @return UR MOM
*/
public Iterator<E> reverseIterator() {
return new RDLListIterator<E>();
}
/**
* Class idk
*
* @author Microsoft
*
* @param <A>
*/
private class RDLListIterator<A> implements Iterator<E> {
private Node<E> next;
private boolean calledNext;
/**
* Creates a new DLListIterator
*/
/**
* Iterator method creates Iterator object
*
* @return new Iterator object
*/
public RDLListIterator() {
this.next = tail;
calledNext = false;
}
/**
* Doin
*/
@Override
public boolean hasNext() {
return (next.previous() != head);
}
/**
* Java
*/
@Override
public E next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
E value = next.previous().getData();
next = next.previous();
calledNext = true;
return value;
}
/**
* resmove
*/
public void remove() {
if (!calledNext) {
throw new IllegalStateException("next() hasn't been "
+ "called before remove");
}
else {
next.next().setPrevious(next.previous());
next.previous().setNext(next.next());
calledNext = false;
size--;
}
}
}
private class DLListIterator<A> implements Iterator<E> {
private Node<E> next;
private boolean calledNext;
/**
* Creates a new DLListIterator
*/
/**
* Iterator method creates Iterator object
*
* @return new Iterator object
*/
public DLListIterator() {
this.next = head;
calledNext = false;
}
/**
* Checks if there are more elements in the list
*
* @return true if there are more elements in the list
*/
@Override
public boolean hasNext() {
return (next.next() != tail);
}
/**
* Gets the next value in the list
*
* @return the next value
* @throws NoSuchElementException
* if there are no nodes left in the list
*/
@Override
public E next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
E value = next.next().getData();
next = next.next();
calledNext = true;
return value;
}
/**
* Removes the last object returned with next() from the list
*
* @throws IllegalStateException
* if next has not been called yet
* and if the element has already been removed
*/
@Override
public void remove() {
if (!calledNext) {
throw new IllegalStateException("next() hasn't been "
+ "called before remove");
}
else {
next.previous().setNext(next.next());
next.next().setPrevious(next.previous());
calledNext = false;
size--;
}
}
}
}