One-way linked list
One-way linked list, also known as a single-linked list, is the simplest form of a list, with each node containing two domains, an information field (element field), and a linked domain. This link points to the next node in the list, and the last node's link field points to a null value.
- The Table element field Elem is used to store specific data.
- Link domain next where the next node is stored (identity in Python)
- The variable p points to the position of the head node (the first node) of the linked list, and any node in the table can be found from P.
Node implementation
Class Node (object): "" " single-linked list node" " def __init__ (self,item): # Item holds data element Self.item = Item # Next is the identity of the next node self.next = None
Single-linked list operation
- Is_empty () linked list is empty
- Length () Chain table lengths
- Travel () traverse the entire list
- add element to list header
- Append add elements to the end of the list (item)
- Insert (POS, item) to specify the location to add elements
- Remove node (item)
- Search (item) to find out if the node exists
Implementation of single linked list
Class Singlelinklist (object): "" " single-linked list" "" def __init__ (self): Self.__head = None def is_empty (self): "" " determines whether the linked list is empty" " return self.__head = = None def length (self):" "" "" "" "", "" "", "" "" "" "" "" "". cur the head node initially cur = Self.__head count = 0 # Tail node points to None when tail is not reached while cur! = None: count + = 1 # moves cur back one node cur = Cur.next return Count def travel (self): "" " traverse the list" "" cur = self.__head while cur! = None: print (cur.item,end = ") cur = cur.next print (" ")
Adding elements to the head
def add (self, item): "" "" "add Element" "" # first create a node that holds the item value nodes = Node (item) # Links the new node domain next to the head node, where _head points to Node.next = sel F.__head # _head The head of a linked list to a new node Self.__head = nod
Trailing add element
def append (self, item): "" "" "" "" "" "" Node = node (item) # First to determine whether the linked list is empty, if the empty list, then _head point to the new node if Self.is_empty (): self.__he AD = node # If it is not empty, find the tail and point the tail node next to the new node Else:cur = Self.__head while cur.next! = None:cur = Cur.next CUR.N EXT = node
add element at specified location
def insert (self, POS, item): "" "add Element" "" at specified location if POS is specified before the first element, then the execution head is inserted if POS <= 0: self.add (item) # If the specified position exceeds the chain At the end of the table, the trailing insert is performed elif pos > (Self.length ()-1): self.append (item) # found at the specified location Else:node = node (item) Count = 0 # PRE used to point to the previous position of POS at the specified position pos-1, initial start from the head node to the specified position pre = Self.__head while Count < (pos-1): Count + = 1 pre = Pre.next # First point the new node to the node in the insertion position node.next = Pre.next # Points The previous node of the insertion position to the new node Pre.next = node
Delete a node
def remove (Self,item): "" "Delete Node" "" cur = self.__head pre = none while cur! = None: # found the specified element if Cur.item = Item: # If the first one is the deleted node if not pre: # point the head pointer to the last node of the head node Self.__head = Cur.next Else: # will delete the position of the previous node before the next point of the delete location Node Pre.next = Cur.next break Else: # continue to link list back to node pre = cur cur = cur.next
Find whether a node exists
def search (Self,item): the "" "List finds whether a node exists and returns TRUE or false" "" cur = self.__head while cur! = none:if Cur.item = Item: return True cur = cur.next return False
One-way linked list of Python data structure list