Java Reference
In-Depth Information
Display 15.3
A Linked List Class
(part 1 of 2)
1
public class
LinkedList1
2 {
3
private
Node1 head;
4
5
public
LinkedList1( )
6 {
7 head =
null
;
8 }
We will define a letter linked list class later in
this chapter.
9
/**
10
Adds a node at the start of the list with the specified data.
11
The added node will be the first node in the list.
12
*/
13
public void
addToStart(String itemName,
int
itemCount)
14 {
15 head =
new
Node1(itemName, itemCount, head);
16 }
17
/**
18
Removes the head node and returns true if the list contains at
19
least one node. Returns false if the list is empty.
20
*/
21
public boolean
deleteHeadNode( )
22 {
23
if
(head !=
null
)
24 {
25 head = head.getLink( );
26
return true
;
27 }
28
else
29
return false
;
30 }
31
/**
32
Returns the number of nodes in the list.
33
*/
34
public int
size( )
35 {
36
int
count = 0;
37 Node1 position = head;
38