Java Reference
In-Depth Information
next
, and
remove
(lines 134-149). This implementation uses
current
to point to the current
position of the element being traversed (line 132). Initially,
current
points to the head of
the list.
L
ISTING
24.6
MyLinkedList.java
1
public class
MyLinkedList<E>
extends
MyAbstractList<E> {
2
private
Node<E> head, tail;
head, tail
3
4
/** Create a default list */
5
public
MyLinkedList() {
6 }
7
8
no-arg constructor
/** Create a list from an array of objects */
9
public
MyLinkedList(E[] objects) {
constructor
10
super
(objects);
11 }
12
13
/** Return the head element in the list */
14
public
E getFirst() {
getFirst
15
if
(size ==
0
) {
16
return null
;
17 }
18
else
{
19
return
head.element;
20 }
21 }
22
23
/** Return the last element in the list */
24
public
E getLast() {
getLast
25
if
(size ==
0
) {
26
return null
;
27 }
28
else
{
29
return
tail.element;
30 }
31 }
32
33
/** Add an element to the beginning of the list */
34
public void
addFirst(E e) {
addFirst
35
// Implemented in Section 24.4.3.1, so omitted here
36 }
37
38
/** Add an element to the end of the list */
39
public void
addLast(E e) {
addLast
40
// Implemented in Section 24.4.3.2, so omitted here
41 }
42
43 @Override
/** Add a new element at the specified index
44
* in this list. The index of the head element is 0 */
45
public void
add(
int
index, E e) {
add
46
// Implemented in Section 24.4.3.3, so omitted here
47 }
48
49
/** Remove the head node and
50
* return the object that is contained in the removed node. */
51
public
E removeFirst() {
removeFirst
52
// Implemented in Section 24.4.3.4, so omitted here
53 }
54
Search WWH ::
Custom Search