Java Reference
In-Depth Information
1
/**
2
* This is the doubly linked list node.
3
*/
4
private static class Node<AnyType>
5
{
6
public Node( AnyType d, Node<AnyType> p, Node<AnyType> n )
7
{
8
data = d; prev = p; next = n;
9
}
10
11
public AnyType data;
12
public Node<AnyType> prev;
13
public Node<AnyType> next;
14
}
figure 17.21
Node nested class for
standard
LinkedList
class
private. It could matter if
Node
was to be extended inside of
LinkedList
, and this
would argue toward making the data private. On the other hand, since in our
implementation,
LinkedList
is accessing the
Node
's data fields directly, rather
than invoking methods, it seems more appropriate to mark the data as public.
The implementation of
LinkedList
begins in Figure 17.22, where we
have the constructors and
clear
. All in all, little is new here; we combined
1
/**
2
* Construct an empty LinkedList.
3
*/
4
public LinkedList( )
5
{
6
clear( );
7
}
8
9
/**
10
* Construct a LinkedList with same items as another Collection.
11
*/
12
public LinkedList( Collection<? extends AnyType> other )
13
{
14
clear( );
15
for( AnyType val : other )
16
figure 17.22
Constructors and
clear
method for
standard
LinkedList
class
add( val );
17
}
18
19
/**
20
* Change the size of this collection to zero.
21
*/
22
public void clear( )
23
{
24
beginMarker = new Node<AnyType>( null, null, null );
25
endMarker = new Node<AnyType>( null, beginMarker, null );
26
beginMarker.next = endMarker;
27
28
theSize = 0;
29
modCount++;
30
}
Search WWH ::
Custom Search