... method, the method throws an NoSuchElementException
(lines 67–69). Otherwise, the method returns a reference to the removed data. The steps are:
-
Assign
firstNode.data
(the data being removed) toremovedItem
(line 71). -
If
firstNode
andlastNode
refer to the same object (line 74), the list has only one element at this time. So, the method setsfirstNode
andlastNode
tonull
(line 75) to remove the node from the list (leaving the list empty). -
If the list has more than one node, then the method leaves reference
lastNode
as is and assigns the value offirstNode.nextNode
tofirstNode
(line 78). Thus,firstNode
references the node that was previously the second node in the list. -
Return the
removedItem
reference (line 81).
In Fig. 21.7, part (a) illustrates ...
Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.