Java Reference
In-Depth Information
handle these cases. Thus we remove an element from the list by deconnecting
its cell from the other chained cells as depicted by Figure 7.2.
v
w=v.next
name (v)
name ( w.next )
name (w)
Container element matching the query s :
(w.name).equals(s) is true
v
name ( w.next )
name (v)
Reference of the next field of v
becomes the reference of the next field of w
v.next=w.next
Figure 7.2 Removing an element to the list by deconnecting its cell from the
other chained cells
Program 7.9 Removing an element from a list
static ListString Delete( String s , ListString
list )
// Case: list is empty
if (list== null )
return null ;
// Case: element is at the head
if (list.name.equals(s))
return list .next;
// Otherwise
ListString v=list ;
ListString w=list .next; //tail
while (w!= null && ! ( ( w . n a m e ) . e q u a l s ( s ) )
)
{ v=w ; w=v . n e x t ; }
// A bit of list surgery here
if (w!= null )
v.next=w.next;
return list ;
}
 
 
Search WWH ::




Custom Search