Java Reference
In-Depth Information
Program 8.13
Linked list the object-oriented style: Methods
int
length ()
{
List u=
this
;
int
res=0;
while
(u!=
null
)
{
r e s ++;u=u . next ;
}
return
res
−
1;
}
boolean
belongsTo(
int
el)
{
List u=
this
.next;
while
(u!=
null
)
if
(el==u.element)
return true
;
u=u . n e x t ;
}
return false
;
}
void
add(
int
el)
{
List u=
this
.next;
this
.next=
new
List(el ,u) ;
}
void
delete(
int
el)
{
List v=
this
;
List w=
this
.next;
while
(w!=
null
&& w. element !=el )
v=w ;
w=w . n e x t ;
}
if
(w!=
null
)v.next=w.next;
}
void
display ()
{
List u=
this
.next;
while
(u!=
null
)
{
System . out . print (u . element+
"->"
);
u=u . n e x t ;
}
System . out . println (
"null"
);
}
static
List FromArray(
int
[]
array)
{
List u=EmptyList() ;
for
(
int
i=array . length
−
1; i
>
=0; i
−−
)
Search WWH ::
Custom Search