Java Reference
In-Depth Information
Display 15.21
A Doubly Linked List
"shoes"
null
"socks"
"coat"
"gloves"
null
Next, we must set the previous link on the old head node to reference the new head.
We can do this by setting head.previous = newHead , but we must take care to ensure
that head is not null (i.e., the list is not empty). Finally, we can set head to newHead .
if (head != null )
{
head.previous = newHead;
}
head = newHead;
To delete a node from the doubly linked list also requires updating the references
on both sides of the node to delete. Thanks to the backward link, there is no need for
an instance variable to keep track of the previous node in the list, as was required for
the singly linked list. The general process of deleting a node referenced by position
is shown in Display 15.23. Note that some cases must be handled separately, such as
deleting a node from the beginning or the end of the list.
 
 
Search WWH ::




Custom Search