21.4.6 List Method insertAtBack

Method insertAtBack (lines 69–75 of Fig. 21.3) places a new node at the back of the list. The steps are:

1. Call isEmpty to determine whether the list is empty (line 71).

2. If the list is empty, assign to firstNode and lastNode the new ListNode that was initialized with insertItem (line 72). The ListNode constructor at lines 13–16 calls the constructor at lines 20–24 to set instance variable data to refer to the insertItem passed as an argument and to set reference nextNode to null.

3. If the list is not empty, line 74 links the new node into the list by assigning to lastNode and lastNode.nextNode the reference to the new ListNode that was initialized with insertItem. ListNode’s constructor (lines 13–16) sets ...

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.