Java Reference
In-Depth Information
Display 15.14
A Generic Linked List with a Deep Copy
clone
Method
(part 1 of 3)
1
public
class
LinkedList<T
extends
PubliclyCloneable>
2
implements
PubliclyCloneable
3{
4
private
class Node<T>
5
{
6
private
T data;
7
private
Node<T> link;
8
public
Node( )
9
{
10
data =
null
;
11
link =
null
;
12
}
13
public
Node(T newData, Node<T> linkValue)
14
{
15
data = newData;
16
link = linkValue;
17
}
18
}
//End of Node<T> inner class
19
private
Node<T> head;
20
public
LinkedList( )
21
{
22
head =
null
;
23
}
24
/**
25
Produces a new linked list, but it is not a true deep copy.
26
Throws a NullPointerException if other is null.
27
*/
28
public
LinkedList(LinkedList<T> otherList)
29
{
30
if
(otherList ==
null
)
31
throw
new
NullPointerException( );
32
if
(otherList.head ==
null
)
33
head =
null
;
34
else
35
head = copyOf(otherList.head);
36
}
37
38
public
LinkedList<T> clone( )
39
{
40
try
41
{
42
LinkedList<T> copy =
43
(LinkedList<T>)super.clone( );