Java Reference
In-Depth Information
public Node(Entry newItem, Node linkValue)
{
item = newItem;
link = linkValue;
}
} //End of Node inner class
private Node head;
<Other definitions from LinkedList2 go here>
} // End of LinkedList2 class
The rest of the definition of LinkedList2 is essentially the same as in Display
15.7, but with the type String replaced by Entry . A complete definition is given
in the subdirectory named “Exercise 8” on the CD that accompanies this text.
9.
No, T is not guaranteed to have a copy constructor. Even if T has a copy con-
structor, it is illegal to use T with new like this.
10.
No, you can use any descendent class of Object (which means any class type) as
the returned type, because the value returned will still be of type Object .
11.
The delete method must now search through the list to find the previous node
and then change the link to bypass the current position. This is less efficient than
the code in Display 15.17 since the reference to the previous node is already set.
public void delete( )
{
if (position == null)
{
throw new IllegalStateException( );
}
else
{
Node current = head;
Node previous = null ;
while (current != null )
{
if (current == position)
{
// Found the node to delete
// Check if we're at the head
if (previous == null )
{
head = head.link;
position = head;
}
else// Delete in middle of list
{
Search WWH ::




Custom Search