```python '''Create a singly linked list''' class Node(object): '''Node class''' def __init__(self, elem): self.elem = elem self.next = None class SingleLinkList(): def __init__(self, node=None): '''''' self.__head = node # Define the first node attribute of the instance, which is a private attribute of the instance # The linked list can be defined as empty, but the first node P must exist in the empty linked list, # node=None, the default first node is empty, but the first node node passed in shall prevail def is_empty(self): """Is the linked list empty?""" """If self._head==None, it means that the linked list has no node element""" return self.__head == None """Returns a boolean value, if it is empty, it returns True, otherwise it returns False""" def length(self): """Length of linked list""" cur = self.__head '''Create a cursor to count''' count = 0 '''Create count starting value is 0''' while cur != None: count += 1 '''If the node where cur is located is not an empty node, add one to count''' cur = cur.next '''Move the cursor back one place''' return count '''Return the value of count''' '''important point: 1. What if the linked list is empty? 2. The problem of the judgment conditions of cur == None and cur.next == None ''' def tarvel (self): """traverse the entire linked list""" cur = self.__head '''Create a cursor to count''' while cur != None: print(cur.elem, end=" ") '''If the node where cur is located is not an empty node, print the value of the elem of the current node''' cur = cur.next '''Move the cursor back one place''' # important point: # 1. What if the linked list is empty? # 2. The problem of the judgment condition of cur == None and cur.next == None # If the judgment condition is cur.next == None, the last element will be discarded print("") def add(self, item): """Add elements to the head of the linked list, first insertion method""" # 1. The default linked list has many nodes, of course, including the first node pointed to by self.__head # 2. Now create a new node of Node(item), which is to be inserted into the head of the linked list, that is, as the head node of the current linked list # 3. Suppose the linked list itself has many nodes # 4. If you directly point the self.__head of the linked list to the new node, all the original nodes will be lost # 5. For this, we first point the next field of the new node to the original head node, and then point self.__head to the current new node node = Node(item) node.next = self.__head self.__head = node # When the linked list is empty, the default first node points to None # The above three lines of code can completely deal with the situation that the linked list is empty def append(self, item): """Add an element to the end of the linked list""" node = Node(item) # Create a new node whose elem is item if self.is_empty(): # Check if the linked list is empty self.__head = node # Make node node the first node of the linked list else: cur = self.__head # Use the first node to create the cursor while cur.next != None: cur = cur.next # If the next attribute of the node pointed to by the cursor is not None, move backward cur.next = node # When the cursor points to the last node (the next attribute of the last node is None), set the next attribute of the current node to node (the next attribute of the current node points to the newly created node node) def insert(self, pos, item): """Insert element at specified position""" if pos <= 0: # If the given subscript is less than or equal to 0, add the element directly to the head of the linked list self.add(item) elif pos > (self.length()-1): # If the given subscript is greater than the tail subscript of the linked list, add elements directly to the tail # Note: If the given subscript is equal to the tail subscript, it is equivalent to adding an element at the tail subscript, and the original tail element needs to be moved one bit backward, and we classify it into the code for inserting the element self.append(item) else: node = Node(item) # create a new node pro = self.__head # create cursor count = 0 # We have counted ways to control the cursor while count < (pos-1): # End the loop when the subscript reaches the previous element of pos count += 1 pro = pro.next # Move the cursor back one place node.next = pro.next # The next field of the new node points to the pointer to the next field at the previous position of pos pro.next = node # Point the next field at the previous position of pos to the new node def remove(self, item): """Delete node""" cur = self.__head pro = None while cur != None: # Traverse all elements, the condition is that cur is not equal to None if cur.elem == item: # If the elem of cur is equal to item (if the element of the node pointed to by the cursor is equal to the element to be found), perform the delete operation if cur == self.__head: # First determine whether the node of the item is the first node self.__head = cur.next # Let the first node point to the successor node of cur # print(cur) else: pro.next = cur.next break else: pro = cur # pro is the cursor of the previous element of cur cur = cur.next # Note: The positions of pro and cur cannot be reversed """ important point: 1. First determine whether the linked list is empty If the linked list is empty, do nothing 2. If there is only one node The above code can complete the task completely (ie: self.__head == cur.next) 3. The first node If it is the first node, point the first node of the linked list self.__head to the successor node of the current node of cur 4. Tail node The above code can complete the task completely (ie: pro.next = cur.next) """ def search(self, item): """Find if the node exists""" cur = self.__head # Create a cursor to control subscripting while cur != None: # Traverse all nodes, that is, only the current element is not None, this loop is always executed if cur.elem == item: # Return True if the node's elem is equal to item return True else: cur = cur.next # Move the cursor back one place return False # When no item element is found after traversing all nodes, return False if __name__ == "__main__": ll = SingleLinkList() print(ll.is_empty()) print(ll.length()) ll.append(1) print(ll.is_empty()) print(ll.length()) ll.append(2) ll.append(3) ll.append(4) ll.append(5) ll.add(100) ll.insert(-1, 600) ll.tarvel () ll.insert(3, 500) ll.tarvel () ll.insert(100, 900) ll.tarvel () print(ll.search(900)) # 600 100 1 500 2 3 4 5 900 ll.remove(900) # delete the tail node ll.tarvel () ll.remove(600) # delete the first node ll.tarvel () ll.remove(1) # remove the tail node ll.tarvel () ```