Java Reference
In-Depth Information
Display 15.7
A Linked List Class with a Node Inner Class
(part 3 of 3)
55 position = position.link;
56 }
57
Note that the outer class
has direct access to the
inner class's instance
variables, such as
link
.
return
count;
58 }
59
public boolean
contains(String item)
60 {
61
return
(find(item) !=
null
);
62 }
63
/**
64
Finds the first node containing the target item, and returns a
65
reference to that node. If target is not in the list, null is
returned.
66
*/
67
private
Node find(String target)
68 {
69 Node position = head;
70 String itemAtPosition;
71
while
(position !=
null
)
72 {
73 itemAtPosition = position.item;
74
if
(itemAtPosition.equals(target))
75
return
position;
76 position = position.link;
77 }
78
return null
;
//target was not found
79 }
80
public void
outputList( )
81 {
82 Node position = head;
83
while
(position !=
null
)
84 {
85 System.out.println(position.item );
86 position = position.link;
87 }
88 }
89
public boolean
isEmpty( )
90 {
91
return
(head ==
null
);
92 }
93
public void
clear( )
94 {
95 head =
null
;
96 }
97 }