first.next.next The reference variable named is not an internal part of the linked structure. The Node first purpose of is to find the beginning of the list so algorithms can find an insertion point for first a new element, for example. In a singly linked structure, the instance variable of each refers to an object in data Node memory, which could be of any type. The instance variable of each object references next Node another node containing the next element in the collection. One of these objects stores Node to mark the end of the linked structure. The value should only exist in the last node. null null Self-Check Use this linked structure to answer the questions that follow. 15-1 What is the value of first.data? 15-2 What is the value of first.next.data? 15-3 What is the value of first.next.next.next.data? 15-4 What is the value of first.next.next.next? Each node stores a reference to the element A linked structure would be pictured more accurately with the field shown to reference an data object somewhere else in memory. However, it is more convenient to show linked structures with the value of the element written in the node, especially if the elements are strings. This means that even though both parts store a reference value (exactly four bytes of memory to indicate a reference to an object), these structures are often pictured with a box dedicated to the data value, as will be done in the remainder of this chapter. The reference values, pictured as arrows, are important. If one of these links becomes misdirected, the program will not be able to find elements in the list. Traversing a Linked Structure Elements in a linked structure can be accessed in a sequential manner. Analogous to a changing subscript to reference all elements in an array, a changing variable can reference all int Node elements in a singly linked structure. In the following loop, the reference begins at the for Node first node and is updated with until it becomes next null. for (Node ref = first; ref != null; ref = ref.next) { System.out.println(ref.data.toString());
first.next.next The Node reference variable named first is not an internal part of the linked structure. The purpose of first is to find the beginning of the list so algorithms can find an insertion point for a new element, for example. In a singly linked structure, the instance variable data of each Node refers to an object in memory, which could be of any type. The instance variable next of each Node object references another node containing the next element in the collection. One of these Node objects stores null to mark the end of the linked structure. The null value should only exist in the last node. Self-Check Use this linked structure to answer the questions that follow. 15-1 What is the value of first.data? 15-2 What is the value of first.next.data? 15-3 What is the value of first.next.next.next.data? 15-4 What is the value of first.next.next.next? Each node stores a reference to the element A linked structure would be pictured more accurately with the data field shown to reference an object somewhere else in memory. However, it is more convenient to show linked structures with the value of the element written in the node, especially if the elements are strings. This means that even though both parts store a reference value (exactly four bytes of memory to indicate a reference to an object), these structures are often pictured with a box dedicated to the data value, as will be done in the remainder of this chapter. The reference values, pictured as arrows, are important. If one of these links becomes misdirected, the program will not be able to find elements in the list. Traversing a Linked Structure Elements in a linked structure can be accessed in a sequential manner. Analogous to a changing int subscript to reference all elements in an array, a changing Node variable can reference all elements in a singly linked structure. In the following for loop, the Node reference begins at the first node and is updated with next until it becomes null. for (Node ref = first; ref != null; ref = ref.next) { System.out.println(ref.data.toString());
227Sum15.docx