Business web hosting - Chapter 23 Data Structures and Collections 1157 Fig.

Chapter 23 Data Structures and Collections 1157 Fig. 23.6 illustrates method InsertAtFront. Part (a) of the figure shows the list and the new node during the InsertAtFront operation and before the threading of the new node into the list. The dotted arrows in part (b) illustrate step 3 of the InsertAt- Front operation, which enables the node containing 12 to become the new list front. Method InsertAtBack (Fig. 23.4, lines 92 104) places a new node at the back of the list. The method consists of three steps (illustrated in Fig. 23.7): 1. Call IsEmpty to determine whether the list is empty (line 96). 2. If the list is empty, set both firstNode and lastNode to refer to a new ListNode initialized with insertItem (lines 97 98). The ListNode constructor at lines 16 19 (Fig. 23.4) calls the ListNode constructor at lines 23 27 (Fig. 23.4) to set instance variable data to refer to the object passed as the first argument and sets the next reference to null. 3. If the list is not empty, thread the new node into the list by setting lastNode and lastNode.next to refer to a new ListNode object initialized with insertItem (lines 101 102). When the ListNode constructor (lines 16 19 of Fig. 23.4) executes, it sets instance variable data to refer to the object passed as an argument and sets the next reference to null. Fig. 23.7 illustrates an InsertAtBack operation. Part a) of the figure shows the list and the new node during the InsertAtBackoperation and before the new node has been threaded into the list. The dotted arrows in part b) illustrate the steps of method Insert- AtBack that enable a new node to be added to the end of a list that is not empty. Method RemoveFromFront (Fig. 23.4, lines 107 127) removes the front node of the list and returns a reference to the removed data. The method throws an EmptyList- Exception (line 114) if the programmer tries to remove a node from an empty list. Otherwise, the method returns a reference to the removed data. The method consists of four steps (illustrated in Fig. 23.8): 1. Assign firstNode.Data (the data being removed from the list) to reference removeItem (line 116). 2. If the objects to which firstNode and lastNode refer are the same object, the list has only one element prior to the removal attempt. In this case, the method sets firstNode and lastNode to null (line 120) to dethread (remove) the node from the list (leaving the list empty). 3. If the list has more than one node prior to removal, then the method leaves reference lastNode as is and simply assigns firstNode.Next to reference firstNode (line 123). Thus, firstNodereferences the node that was the second node prior to the RemoveFromFront call. 4. Return the removeItem reference. Fig. 23.8 illustrates method RemoveFromFront. Part a) illustrates the list before the removal operation. Part b) shows actual reference manipulations. Method RemoveFromBack (Fig. 23.4, lines 130 160) removes the last node of a list and returns a reference to the removed data. The method throws an EmptyListException( line 137) if the program attempts to remove a node from an empty list. The method consists of several steps (illustrated in Fig. 23.9):
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services

Leave a Reply