Chapter 23 Data Structures and Collections 1159 3. (Cheap web hosting)

Chapter 23 Data Structures and Collections 1159 3. If the list has more than one node prior to removal, create the ListNode reference currentand assign it firstNode(line 147). 4. Now walk the list with current until it references the node before the last node. The while loop (lines 150 151) assigns current.Next to reference currentas long as current.Nextis not equal to lastNode. 5. After locating the second-to-last node, assign currentto lastNode(line 154) to dethread the last node from the list. 6. Set current.Nextto null(line 155) in the new last node of the list to ensure proper list termination. 7. Return the removeItemreference (line 140). Fig. 23.9 illustrates method RemoveFromBack. Part a) illustrates the list before the removal operation. Part b) shows the actual reference manipulations. Method Print (Fig. 23.4, lines 172 195) first determines whether the list is empty (line 176). If so, Print displays a string consisting of the string “Empty ” and the list s name, then returns control to the calling method. Otherwise, Printoutputs the data in the list. The method prints a string consisting of the string “The “, the name and the string ” is:”. Then, line 184 creates ListNodereference currentand initializes it with firstNode. While currentis not null, there are more items in the list. Therefore, the method prints current.Data (line 189), then assigns current.Next to current (line 190) to move to the next node in the list. Note that, if the link in the last node of the list is not null, the printing algorithm will erroneously attempt to print past the end of the list. The printing algorithm is identical for linked lists, stacks and queues. (a) firstNode lastNode 7 1112 5 (b) firstNode lastNode 7 1112 5 removeItem Fig. 23.8 Fig. 23.Fig. 23.FF8ig. 23.8ig.8A graphical representation of the RemoveFromFrontoperation. 23.
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision servlet hosting services

Leave a Reply