Java Reference
In-Depth Information
Display 15.25
A Doubly Linked List with an Iterator
(part 3 of 3)
86
private
TwoWayNode head;
87
public
DoublyLinkedIterator iterator( )
88 {
89
return new
DoublyLinkedIterator( );
90 }
91
public
DoublyLinkedList( )
92 {
93 head =
null
;
94 }
95
/**
96
The added node will be the first node in the list.
97
*/
98
public void
addToStart(String itemName)
99 {
100 TwoWayNode newHead =
new
TwoWayNode(itemName,
null
, head);
101
if
(head !=
null
)
102 {
103 head.previous = newHead;
104 }
105 head = newHead;
106 }
<The methods
hasNext
,
peek
,
clear
, and
isEmpty
are identical
to those in Display 15.17. Other methods would also normally
be defined here, such as
deleteHeadNode
,
size
,
outputList
,
equals
,
clone
,
find
, or
contains
. They have been left off to
simplify the example.>
107 }
// DoublyLinkedList
Display 15.26
Using a Doubly Linked List with an Iterator
(part 1 of 2)
1
public class
DoublyLinkedListDemo
2 {
3
public static void
main(String[] args)
4 {
5 DoublyLinkedList list =
new
DoublyLinkedList( );
6 DoublyLinkedList.DoublyLinkedIterator i = list.iterator( );
7 list.addToStart("shoes");
8 list.addToStart("orange juice");
9 list.addToStart("coat");
10 System.out.println("List contains:");
11 i.restart( );
(continued)