Java Reference
In-Depth Information
first
=
new
new
TNode
<
T
>(
null
null
);
last
=
first
;
}
/** Add one object to the end of the list. Update the "next"
* reference in the previous end, to refer to the new node.
* Update "last" to refer to the new node.
*/
public
public
boolean
boolean
add
(
T o
) {
last
.
next
=
new
new
TNode
<
T
>(
o
);
last
=
last
.
next
;
return
return true
true
;
}
public
public
void
int
where
,
T o
) {
TNode
<
T
>
t
=
first
;
for
void
add
(
int
for
(
int
int
i
=
0
;
i
<=
where
;
i
++) {
t
=
t
.
next
;
iif
(
t
==
null
null
) {
throw
throw new
new
IndexOutOfBoundsException
(
"'add(n,T) went off end of list"
);
}
iif
(
DIAGNOSTIC
) {
System
.
out
.
printf
(
"add(int,T): i = %d, t = %s%n"
,
i
,
t
);
}
}
TNode
<
T
>
t2
=
t
;
t
.
next
=
new
new
TNode
<
T
>(
o
);
t
.
next
=
t2
;
}
public
public
int
int
size
() {
TNode
<
T
>
t
=
first
;
int
int
i
;
for
for
(
i
=
0
; ;
i
++) {
iif
(
t
==
null
null
)
break
;
t
=
t
.
next
;
break
}
return
return
i
-
1
;
// subtract one for mandatory head node.
}
public
public
boolean
boolean
isEmpty
() {
return
return
first
==
last
;
}