Java Reference
In-Depth Information
Display 15.8
A Generic Linked List Class
(part 1 of 3)
1
public class
LinkedList3<T>
2 {
3
private
class Node<T>
4 {
5
private
T data;
6
private
Node<T> link;
7
public
Node( )
8 {
9 data =
null
;
10 link =
null
;
This linked list holds objects of type
T
.
The type
T
should have well-defined
equals
and
toString
methods.
11 }
12
public
Node (T newData, Node<T> linkValue)
13 {
14 data = newData;
15 link = linkValue;
16 }
17 }
//End of Node<T> inner class
18
private
Node<T> head;
19
public
LinkedList3( )
20 {
21 head =
null
;
22 }
23
/**
24
Adds a node at the start of the list with the specified data.
25
The added node will be the first node in the list.
26
*/
27
public void
addToStart(T itemData)
28 {
29 head =
new
Node<T> (itemData, head);
30 }
31
/**
32
Removes the head node and returns true if the list contains at
33
least one node. Returns false if the list is empty.
34
*/
35
public boolean
deleteHeadNode( )
36 {
37
if
(head !=
null
)
38 {
39 head = head.link;
40
return true
;
41 }
42
else
43
return false
;
44 }
45
/**