* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Download CHAPTER 18 Linked Lists, Stacks, Queues, and Priority
Survey
Document related concepts
Transcript
tail tail = tail.next head "Chicago" "Denver" "Dallas" n ext next next: None (b) Figure 18.5 Append the third node to the list. To append the new node to the list, link the last node in the list with the new node, as shown in Figure 18.5a. The new node is now the tail node. So you should move tail to point to this new node, as shown in Figure 18.5b. Each node contains the element and a data field named next that points to the next element. If the node is the last in the list, its pointer data field next contains the value None. You can use this property to detect the last node. For example, you may write the following loop to traverse all the nodes in the list. current = head while current != None: print(current.element) current = current.next The variable current points initially to the first node in the list (line 1). In the loop, the element of the current node is retrieved (line 3), and then current points to the next node (line 4). The loop continues until the current node is None. 18.2.1 The LinkedList Class The LinkedList class can be defined in a UML diagram in Figure 18.6. The solid diamond indicates that LinkedList contains nodes. For references on the notations in the diagram, see Section 8.8. 6 © Copyright 2012 by Pearson Education, Inc. All Rights Reserved.