Skip to content

Commit b4cf730

Browse files
author
phishman3579
committed
Made List, Queue, and Stack Iterable
git-svn-id: https://java-algorithms-implementation.googlecode.com/svn/trunk@400 032fbc0f-8cab-eb90-e552-f08422b9a96a
1 parent 6e9e2d0 commit b4cf730

File tree

3 files changed

+308
-37
lines changed

3 files changed

+308
-37
lines changed

src/com/jwetherell/algorithms/data_structures/List.java

Lines changed: 113 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jwetherell.algorithms.data_structures;
22

33
import java.util.Arrays;
4+
import java.util.Iterator;
45

56
/**
67
* A list or sequence is an abstract data type that implements an ordered
@@ -10,7 +11,7 @@
1011
*
1112
* @author Justin Wetherell <phishman3579@gmail.com>
1213
*/
13-
public abstract class List<T> {
14+
public abstract class List<T> implements Iterable<T> {
1415

1516
public enum ListType {
1617
LinkedList, ArrayList
@@ -161,6 +162,48 @@ public String toString() {
161162
}
162163
return builder.toString();
163164
}
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+
}
164207
}
165208

166209
/**
@@ -198,8 +241,8 @@ private void add(Node<T> node) {
198241
tail = node;
199242
} else {
200243
Node<T> prev = tail;
201-
prev.nextNode = node;
202-
node.previousNode = prev;
244+
prev.next = node;
245+
node.prev = prev;
203246
tail = node;
204247
}
205248
size++;
@@ -213,25 +256,25 @@ public boolean remove(T value) {
213256
// Find the node
214257
Node<T> node = head;
215258
while (node != null && (!node.value.equals(value))) {
216-
node = node.nextNode;
259+
node = node.next;
217260
}
218261
if (node == null)
219262
return false;
220263

221264
// Update the tail, if needed
222265
if (node.equals(tail))
223-
tail = node.previousNode;
266+
tail = node.prev;
224267

225-
Node<T> prev = node.previousNode;
226-
Node<T> next = node.nextNode;
268+
Node<T> prev = node.prev;
269+
Node<T> next = node.next;
227270
if (prev != null && next != null) {
228-
prev.nextNode = next;
229-
next.previousNode = prev;
271+
prev.next = next;
272+
next.prev = prev;
230273
} else if (prev != null && next == null) {
231-
prev.nextNode = null;
274+
prev.next = null;
232275
} else if (prev == null && next != null) {
233276
// Node is the head
234-
next.previousNode = null;
277+
next.prev = null;
235278
head = next;
236279
} else {
237280
// prev==null && next==null
@@ -250,7 +293,7 @@ public boolean contains(T value) {
250293
while (node != null) {
251294
if (node.value.equals(value))
252295
return true;
253-
node = node.nextNode;
296+
node = node.next;
254297
}
255298
return false;
256299
}
@@ -264,7 +307,7 @@ public T get(int index) {
264307
Node<T> node = head;
265308
int i = 0;
266309
while (node != null && i < index) {
267-
node = node.nextNode;
310+
node = node.next;
268311
i++;
269312
}
270313
if (node != null)
@@ -280,6 +323,26 @@ public int size() {
280323
return size;
281324
}
282325

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+
283346
/**
284347
* {@inheritDoc}
285348
*/
@@ -289,28 +352,56 @@ public String toString() {
289352
Node<T> node = head;
290353
while (node != null) {
291354
builder.append(node.value).append(", ");
292-
node = node.nextNode;
355+
node = node.next;
293356
}
294357
return builder.toString();
295358
}
296359

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> {
298369

299-
private T value = null;
300-
private Node<T> previousNode = null;
301370
private Node<T> nextNode = null;
302371

303-
private Node(T value) {
304-
this.value = value;
372+
private LinkedListIterator(Node<T> head) {
373+
this.nextNode = head;
305374
}
306375

307376
/**
308377
* {@inheritDoc}
309378
*/
310379
@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");
314405
}
315406
}
316407
}

0 commit comments

Comments
 (0)