![]() Listing 3: The SingleLinkedList class (part one) class SingleLinkedList: def _init_( self): "constructor to initiate this object" Initially, both head and tail have the value None as long as the list is empty. They represent the beginning and the end nodes of the list. The _init_() method defines two internal class variables named head and tail. We will go through each of these methods step by step. remove_list_item_by_id(): remove the node according to its id.unordered_search(): search the list for the nodes with a specified value.add_list_item(): add a node at the end of the list.list_length(): return the number of nodes.Step 2: Creating a Class for a Single-Linked ListĪs the second step we define a class named SingleLinkedList that covers the methods needed to manage our list nodes. ![]() These instances represent three independent nodes that contain the values 15 (integer), 8.2 (float), and "Berlin" (string). Having done that we have available three instances of the ListNode class. Listing 2: Instantiation of nodes node1 = ListNode( 15) next = None return def has_value( self, value): "method to compare the value with the node data" if self.data = value:Ĭreating a node is as simple as that, and instantiates an object of class ListNode: Listing 1: The ListNode class class ListNode: def _init_( self, data): "constructor to initiate this object" # store data The method has_value() allows us to compare the node value with the value of a different node. These methods ensure that we can initialize a node properly with our data ( _init_()), and cover both the data extraction and storage (via the self.data property) as well getting the reference to the connected node (via the self.next property).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |