1
1
package com .jwetherell .algorithms .data_structures ;
2
2
3
3
import java .util .Arrays ;
4
+ import java .util .Iterator ;
4
5
5
6
/**
6
7
* A list or sequence is an abstract data type that implements an ordered
10
11
*
11
12
* @author Justin Wetherell <phishman3579@gmail.com>
12
13
*/
13
- public abstract class List <T > {
14
+ public abstract class List <T > implements Iterable < T > {
14
15
15
16
public enum ListType {
16
17
LinkedList , ArrayList
@@ -161,6 +162,48 @@ public String toString() {
161
162
}
162
163
return builder .toString ();
163
164
}
165
+
166
+ /**
167
+ * {@inheritDoc}
168
+ */
169
+ @ Override
170
+ public Iterator <T > iterator () {
171
+ return (new ArrayListIterator <T >(this ));
172
+ }
173
+
174
+ private static class ArrayListIterator <T > implements Iterator <T > {
175
+
176
+ private ArrayList <T > list = null ;
177
+ private int index = 0 ;
178
+
179
+ private ArrayListIterator (ArrayList <T > list ) {
180
+ this .list = list ;
181
+ }
182
+ /**
183
+ * {@inheritDoc}
184
+ */
185
+ @ Override
186
+ public boolean hasNext () {
187
+ return (index +1 <=list .size );
188
+ }
189
+
190
+ /**
191
+ * {@inheritDoc}
192
+ */
193
+ @ Override
194
+ public T next () {
195
+ if (index >=list .size ) return null ;
196
+ return list .array [index ++];
197
+ }
198
+
199
+ /**
200
+ * {@inheritDoc}
201
+ */
202
+ @ Override
203
+ public void remove () {
204
+ System .err .println ("OperationNotSupported" );
205
+ }
206
+ }
164
207
}
165
208
166
209
/**
@@ -198,8 +241,8 @@ private void add(Node<T> node) {
198
241
tail = node ;
199
242
} else {
200
243
Node <T > prev = tail ;
201
- prev .nextNode = node ;
202
- node .previousNode = prev ;
244
+ prev .next = node ;
245
+ node .prev = prev ;
203
246
tail = node ;
204
247
}
205
248
size ++;
@@ -213,25 +256,25 @@ public boolean remove(T value) {
213
256
// Find the node
214
257
Node <T > node = head ;
215
258
while (node != null && (!node .value .equals (value ))) {
216
- node = node .nextNode ;
259
+ node = node .next ;
217
260
}
218
261
if (node == null )
219
262
return false ;
220
263
221
264
// Update the tail, if needed
222
265
if (node .equals (tail ))
223
- tail = node .previousNode ;
266
+ tail = node .prev ;
224
267
225
- Node <T > prev = node .previousNode ;
226
- Node <T > next = node .nextNode ;
268
+ Node <T > prev = node .prev ;
269
+ Node <T > next = node .next ;
227
270
if (prev != null && next != null ) {
228
- prev .nextNode = next ;
229
- next .previousNode = prev ;
271
+ prev .next = next ;
272
+ next .prev = prev ;
230
273
} else if (prev != null && next == null ) {
231
- prev .nextNode = null ;
274
+ prev .next = null ;
232
275
} else if (prev == null && next != null ) {
233
276
// Node is the head
234
- next .previousNode = null ;
277
+ next .prev = null ;
235
278
head = next ;
236
279
} else {
237
280
// prev==null && next==null
@@ -250,7 +293,7 @@ public boolean contains(T value) {
250
293
while (node != null ) {
251
294
if (node .value .equals (value ))
252
295
return true ;
253
- node = node .nextNode ;
296
+ node = node .next ;
254
297
}
255
298
return false ;
256
299
}
@@ -264,7 +307,7 @@ public T get(int index) {
264
307
Node <T > node = head ;
265
308
int i = 0 ;
266
309
while (node != null && i < index ) {
267
- node = node .nextNode ;
310
+ node = node .next ;
268
311
i ++;
269
312
}
270
313
if (node != null )
@@ -280,6 +323,26 @@ public int size() {
280
323
return size ;
281
324
}
282
325
326
+ private static class Node <T > {
327
+
328
+ private T value = null ;
329
+ private Node <T > prev = null ;
330
+ private Node <T > next = null ;
331
+
332
+ private Node (T value ) {
333
+ this .value = value ;
334
+ }
335
+
336
+ /**
337
+ * {@inheritDoc}
338
+ */
339
+ @ Override
340
+ public String toString () {
341
+ return "value=" + value + " previous=" + ((prev != null ) ? prev .value : "NULL" )
342
+ + " next=" + ((next != null ) ? next .value : "NULL" );
343
+ }
344
+ }
345
+
283
346
/**
284
347
* {@inheritDoc}
285
348
*/
@@ -289,28 +352,56 @@ public String toString() {
289
352
Node <T > node = head ;
290
353
while (node != null ) {
291
354
builder .append (node .value ).append (", " );
292
- node = node .nextNode ;
355
+ node = node .next ;
293
356
}
294
357
return builder .toString ();
295
358
}
296
359
297
- private static class Node <T > {
360
+ /**
361
+ * {@inheritDoc}
362
+ */
363
+ @ Override
364
+ public Iterator <T > iterator () {
365
+ return (new LinkedListIterator <T >(this .head ));
366
+ }
367
+
368
+ private static class LinkedListIterator <T > implements Iterator <T > {
298
369
299
- private T value = null ;
300
- private Node <T > previousNode = null ;
301
370
private Node <T > nextNode = null ;
302
371
303
- private Node ( T value ) {
304
- this .value = value ;
372
+ private LinkedListIterator ( Node < T > head ) {
373
+ this .nextNode = head ;
305
374
}
306
375
307
376
/**
308
377
* {@inheritDoc}
309
378
*/
310
379
@ Override
311
- public String toString () {
312
- return "value=" + value + " previous=" + ((previousNode != null ) ? previousNode .value : "NULL" )
313
- + " next=" + ((nextNode != null ) ? nextNode .value : "NULL" );
380
+ public boolean hasNext () {
381
+ return (nextNode !=null );
382
+ }
383
+
384
+ /**
385
+ * {@inheritDoc}
386
+ */
387
+ @ Override
388
+ public T next () {
389
+ Node <T > current = nextNode ;
390
+ if (current !=null ) {
391
+ nextNode = current .next ;
392
+ return current .value ;
393
+ } else {
394
+ nextNode = null ;
395
+ }
396
+ return null ;
397
+ }
398
+
399
+ /**
400
+ * {@inheritDoc}
401
+ */
402
+ @ Override
403
+ public void remove () {
404
+ System .err .println ("OperationNotSupported" );
314
405
}
315
406
}
316
407
}
0 commit comments