-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
332 lines (269 loc) · 7.35 KB
/
DoublyLinkedList.java
File metadata and controls
332 lines (269 loc) · 7.35 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
import java.util.Iterator;
import java.util.NoSuchElementException;
import LinkedList.Node;
public class DoublyLinkedList<E> implements List<E> {
private class Node {
private E value;
private Node next, prev;
public Node(E value, Node next, Node prev) {
this.value = value;
this.next = next;
this.prev = prev;
}
public Node(E value) {
this(value, null, null); // Delegate to other constructor
}
public Node() {
this(null, null, null); // Delegate to other constructor
}
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node getPrev() {
return prev;
}
public void setPrev(Node prev) {
this.prev = prev;
}
public void clear() {
value = null;
next = prev = null;
}
} // End of Node class
private class ListIterator implements Iterator<E> {
private Node nextNode;
public ListIterator() {
nextNode = header.getNext();
}
@Override
public boolean hasNext() {
return nextNode != trailer;
}
@Override
public E next() {
if (hasNext()) {
E val = nextNode.getValue();
nextNode = nextNode.getNext();
return val;
}
else
throw new NoSuchElementException();
}
} // End of ListIterator class
/* private fields */
private Node header, trailer; // "dummy" nodes
private int currentSize;
public DoublyLinkedList() {
header = new Node();
trailer = new Node();
header.setNext(trailer);
trailer.setPrev(header);
currentSize = 0;
}
@Override
public Iterator<E> iterator() {
return new ListIterator();
}
@Override
public void add(E obj) {
Node newNode = new Node(obj);
Node curNode;
/* TODO With a Doubly Linked List (with header AND trailer), this is easy.
* The new node must be inserted before the trailer, and that's it.
* You could use a different constructor, or just add some statements below.
*/
for (curNode = header; curNode.getNext() != null; curNode = curNode.getNext());
if(curNode.getNext() == trailer) {
}
curNode.setNext(newNode);
currentSize++;
}
@Override
public void add(int index, E obj) {
Node curNode, newNode;
/* First confirm index is a valid position
We allow for index == size() and delegate to add(object). */
if (index < 0 || index > size())
throw new IndexOutOfBoundsException();
if (index == size())
add(obj); // Use our "append" method
else {
// Get predecessor node (at position index - 1)
curNode = get_node(index - 1);
/* The new node must be inserted between curNode and curNode's next
Note that if index = 0, curNode will be header node */
newNode = new Node(obj, curNode.getNext(), curNode);
// TODO For a DLL, what else needs to be done? Try a diagram; consider edge cases.
currentSize++;
}
}
@Override
public boolean remove(E obj) {
Node curNode = header;
Node nextNode = curNode.getNext();
// Traverse the list until we find the element or we reach the end
while (nextNode != trailer && !nextNode.getValue().equals(obj)) {
curNode = nextNode;
nextNode = nextNode.getNext();
}
// Need to check if we found it
if (nextNode != trailer) { // Found it!
// If we have A <-> B <-> C, need to get to A <-> C
curNode.setNext(nextNode.getNext());
// TODO For a DLL, what else needs to be done? See comment above for a hint.
nextNode.clear(); // free up resources
currentSize--;
return true;
}
else
return false;
}
@Override
public boolean remove(int index) {
Node rmNode;
// TODO These variables could be helpful: Node prevNode, nextNode;
// Feel free to declare and use them in any methods, but they're not required.
// First confirm index is a valid position
if (index < 0 || index >= size())
throw new IndexOutOfBoundsException();
// If we have A <-> B <-> C, need to get to A <-> C
rmNode = get_node(index); // Get the node that is to be removed
// TODO For a DLL, what needs to be done?
rmNode.clear();
currentSize--;
return true;
}
/* Private method to return the node at position index */
private Node get_node(int index) {
Node curNode;
/* First confirm index is a valid position
Allow -1 so that header node may be returned */
if (index < -1 || index >= size())
throw new IndexOutOfBoundsException();
curNode = header;
// Since first node is pos 0, let header be position -1
for (int curPos = -1; curPos < index; curPos++)
curNode = curNode.getNext();
// Perhaps we could traverse backwards instead if index > size/2...
return curNode;
}
@Override
public int removeAll(E obj) {
int counter = 0;
Node curNode = header;
Node nextNode = curNode.getNext();
/* We used the following in ArrayList, and it would also work here,
* but it would have running time of O(n^2).
*
* while (remove(obj))
* counter++;
*/
// Traverse the entire list
while (nextNode != trailer) {
if (nextNode.getValue().equals(obj)) {
// Remove nextNode
/* TODO For a DLL, what needs to be done?
* You can declare more Node variables if it helps make things clear.
*/
nextNode.clear();
currentSize--;
counter++;
/* Node that was pointed to by nextNode no longer exists
so reset it such that it's still the node after curNode */
nextNode = curNode.getNext();
}
else {
curNode = nextNode;
nextNode = nextNode.getNext();
}
}
return counter;
}
@Override
public E get(int index) {
// get_node allows for index to be -1, but we don't want get to allow that
if (index < 0 || index >= size())
throw new IndexOutOfBoundsException();
return get_node(index).getValue();
}
@Override
public E set(int index, E obj) {
// get_node allows for index to be -1, but we don't want set to allow that
if (index < 0 || index >= size())
throw new IndexOutOfBoundsException();
Node theNode = get_node(index);
E theValue = theNode.getValue();
theNode.setValue(obj);
return theValue;
}
@Override
public E first() {
return get(0);
}
@Override
public E last() {
return get(size()-1);
}
@Override
public int firstIndex(E obj) {
Node curNode = header.getNext();
int curPos = 0;
// Traverse the list until we find the element or we reach the end
while (curNode != trailer && !curNode.getValue().equals(obj)) {
curPos++;
curNode = curNode.getNext();
}
if (curNode != trailer)
return curPos;
else
return -1;
}
@Override
public int lastIndex(E obj) {
Node curNode = trailer.getPrev();
int curPos = size() - 1;
// Traverse the list (backwards) until we find the element or we reach the beginning
while (curNode != header && !curNode.getValue().equals(obj)) {
curPos--;
curNode = curNode.getPrev();
}
return curPos; // Will be -1 if we reached the header
}
@Override
public int size() {
return currentSize;
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public boolean contains(E obj) {
return firstIndex(obj) != -1;
}
@Override
public void clear() {
// Avoid throwing an exception if the list is already empty
while (size() > 0)
remove(0);
}
@Override
public int replaceAll(E e, E f) {
// TODO Auto-generated method stub
return 0;
}
@Override
public List<E> reverse() {
// TODO Auto-generated method stub
return null;
}
}