Java Reference
In-Depth Information
Display 15.37
Set<T>
Class
(part 2 of 4)
9
public
Node( )
10
{
11
data =
null
;
12
link =
null
;
13
}
14
public
Node(T newData, Node<T> linkValue)
15
{
16
data = newData;
17
link = linkValue;
18
}
19
}
//End of Node<T> inner class
20
private
Node<T> head;
21
public
Set( )
22
{
23
head =
null
;
24
}
25
/**
26
Add a new item to the set. If the item
27
is already in the set, false is returned;
28
otherwise, true is returned.
29
*/
30
public
boolean
add(T newItem)
31
{
32
if
(!contains(newItem))
33
{
34
head =
new
Node<T>(newItem, head);
35
return
true
;
36
}
37
return
false
;
38
}
39
public
boolean
contains(T item)
40
{
41
Node<T> position = head;
42
T itemAtPosition;
43
while
(position !=
null
)
44
{
45
itemAtPosition = position.data;
46
if
(itemAtPosition.equals(item))
47
return
true
;
48
position = position.link;
49
}
50
return
false
;
//target was not found
51
}
(continued)