21.4.7 List Method removeFromFront
Method removeFromFront
(lines 78–92 of Fig. 21.3) removes the first node of the list and returns a reference to the removed data. If the list is empty when the program calls this method, the method throws an EmptyListException
(lines 80–81). Otherwise, the method returns a reference to the removed data. The steps are:
1. Assign firstNode.data
(the data being removed) to removedItem
(line 83).
2. If firstNode
and lastNode
refer to the same object (line 86), the list has only one element at this time. So, the method sets firstNode
and lastNode
to null
(line 87) to remove the node from the list (leaving the list empty).
3. If the list has more than one node, then the method leaves reference lastNode
as is and ...
Get Java™ How To Program (Early Objects), Tenth 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.