Java Reference
In-Depth Information
Display 15.25 A Doubly Linked List with an Iterator (part 1 of 3)
1
import java.util.NoSuchElementException;
2 public class DoublyLinkedList
3{
4
private class TwoWayNode
5
{
6
private String item;
7
private TwoWayNode previous;
8
private TwoWayNode next;
9
public TwoWayNode( )
10
{
11
item = null ;
12
next = null ;
13
previous = null ;
14
}
15
public TwoWayNode(String newItem, TwoWayNode previousNode, TwoWayNode nextNode)
16
{
17
item = newItem;
18
next = nextNode;
19
previous = previousNode;
20
}
21
} //End of TwoWayNode inner class
22
public class DoublyLinkedIterator
23
{
24
// We do not need a previous node when using a doubly linked list
25
private TwoWayNode position = null;
26
public DoublyLinkedIterator( )
27
{
28
position = head;
29
}
30
public void restart( )
31
{
32
position = head;
33
}
34
public String next( )
35
{
36
if (!hasNext( ))
37
throw new IllegalStateException( );
38
String toReturn = position.item;
39
position = position.next;
40
return toReturn;
41
}
Search WWH ::




Custom Search