Java Reference
In-Depth Information
At the end of this section is the complete implementation of our
LinkedList
class.
You now know how to use the
LinkedList
class in the Java library, and you have
had a peek Ȓunder the hoodȓ to see how linked lists are implemented.
ch15/impllist/LinkedList.java
1
import
java.util.NoSuchElementException;
2
3 /**
4
A linked list is a sequence of nodes with efficient
5
element insertion and removal. This class
6
contains a subset of the methods of the standard
7
java.util.LinkedList
class.
8 */
9
public class
LinkedList
10 {
11 /**
12
Constructs an empty linked list.
13 */
14
public
LinkedList()
15 {
16 first =
null
;
17 }
18
19 /**
20
Returns the first element in the linked list.
21 @return
the first element in the linked
list
22 */
23
public
Object getFirst()
24 {
25
if
(first ==
null
)
26
throw new
NoSuchElementException();
27
return
first.data;
28 }
29
30 /**
31
Removes the first element in the linked list.
32 @return
the removed element
677
678