Linked list is a common underlying data structure. It is a linear table, but it does not store data as sequential tables. Instead, it stores the location information (address) of the next node in each node (data storage unit).
A one-way chain table, also known as a single-chain table, is the simplest form of a chain table. Each node of the list contains two fields, one information field (element field) and one link field.This link points to the next node in the list, while the link field of the last node points to an empty value.
The nature of variable identification in Python
class Node(object): '''node''' def __init__(self,elem): self.elem = elem self.next = None
Is_Is the empty () list empty
length() chain length
travel() traverses the entire list of chains
add(item) chain header add element
append(item) chain list tail add element
insert(pos, item) specifies the location to add elements
remove(item) delete node
search(item) to find if a node exists
class SingleLinkList(object): '''Singly Linked List''' def __init__(self,node=None): self._head = node ##Private Property_ def is_empty(self): '''Is the list empty''' return self._head == None def length(self): '''Chain List Length''' # Cursor, used to move traversed nodes cur = self._head # Number of count records count = 0 while cur != None: count += 1 cur = cur.next return count def travel(self): '''Traverse the entire list''' cur = self._head while cur != None: print(cur.elem) cur = cur.next
def add(self,item): '''Chain Table Head Add Element, Head Interpolation''' node = Node(item) node.next = self._head self._head = node def append(self,item): '''Add element at end of list,Tail interpolation''' node = Node(item) if self.is_empty(): self._head = node else: cur = self._head while cur.next != None: cur = cur.next cur.next = node def insert(self,pos,item): '''Add element at specified location :param pos Start from 0 ''' if pos <= 0: self.add(item) elif pos > (self.length()-1): self.append(item) else: pre = self._head count = 0 while count < (pos - 1): count += 1 pre = pre.next # When the loop exits, pre point to pos-1 node = Node(item) node.next = pre.next pre.next = node
def remove(self,item): '''Delete Node''' cur = self._head pre = None while cur != None: if cur.elem == item: # Determine if this node is the head node first # Head Node if cur == self._head: self._head = cur.next else: pre.next = cur.next break else: pre = cur cur = cur.next def search(self,item): '''Find if a node exists''' cur = self._head while cur != None: if cur.elem == item: return True else: cur = cur.next return False
Chain lists lose the advantage of random reading of sequential tables, while they are more expensive in space due to the addition of pointer domains for nodes, but they are more flexible in the use of storage space.
The complexity of various operations for a chain table and a sequential table is as follows:
Note that although the surface appears to be O(n), the chain and order tables perform completely different operations when inserting and deleting.The main time-consuming operation of a linked list is to traverse the lookup, and the complexity of the delete and insert operations themselves is O(1).Sequential table lookups are fast, and the main time-consuming operation is copy overwriting.Because inserting and deleting a sequence table requires front-and-back shifting of all elements after the operation point, except for the special case where the target element is at the tail, it can only be done by copying and overwriting.