Java Reference
In-Depth Information
18
// constructor creates ListNode that refers to the specified
19
// object and to the next ListNode
20
ListNode(T object, ListNode<T> node)
21
{
22
data = object;
23
nextNode = node;
24
}
25
26
// return reference to data in node
27
T getData()
28
{
29
return data;
30
}
31
32
// return reference to next node in list
33
ListNode<T> getNext()
34
{
35
return nextNode;
36
}
37
} // end class ListNode<T>
38
39
// class List definition
40
public class List<T>
41
{
42
private ListNode<T> firstNode;
private ListNode<T> lastNode;
43
44
private String name; // string like "list" used in printing
45
46
// constructor creates empty List with "list" as the name
47
public List()
48
{
49
this ( "list" );
50
}
51
52
// constructor creates an empty List with a name
53
public List(String listName)
54
{
55
name = listName;
56
firstNode = lastNode = null ;
57
}
58
59
// insert item at front of List
60
public void insertAtFront(T insertItem)
61
{
62
if (isEmpty()) // firstNode and lastNode refer to same object
63
firstNode = lastNode = new ListNode<T>(insertItem);
64
else // firstNode refers to new node
65
firstNode = new ListNode<T>(insertItem, firstNode);
66
}
67
68
// insert item at end of List
69
public void insertAtBack(T insertItem)
70
{
Fig. 21.3 | ListNode and List class declarations. (Part 2 of 4.)
Search WWH ::




Custom Search