# Python Data Structure and Algorithms--One-way Chain List 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 ### 1. Node implementation

```class Node(object):
'''node'''
def __init__(self,elem):
self.elem = elem
self.next = None
```

### 2. Operation of Single Chain List

Is_Is the empty () list empty
length() chain length
travel() traverses the entire list of chains
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

### 3. Implementation of Single Chain List ```class SingleLinkList(object):
def __init__(self,node=None):

def is_empty(self):
'''Is the list empty'''

def length(self):
'''Chain List Length'''
# Cursor, used to move traversed nodes
# Number of count records
count = 0
while cur != None:
count += 1
cur = cur.next
return count

def travel(self):
'''Traverse the entire list'''
while cur != None:
print(cur.elem)
cur = cur.next
``` ```    def add(self,item):
node = Node(item)

def append(self,item):
'''Add element at end of list,Tail interpolation'''
node = Node(item)
if self.is_empty():
else:
while cur.next != None:
cur = cur.next
cur.next = node

def insert(self,pos,item):
:param pos Start from 0
'''
if pos <= 0:
elif pos > (self.length()-1):
self.append(item)
else:
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
```

### 5. Find and delete elements

```    def remove(self,item):
'''Delete Node'''
pre = None
while cur != None:
if cur.elem == item:
# Determine if this node is the head node first
else:
pre.next = cur.next
break
else:
pre = cur
cur = cur.next

def search(self,item):
'''Find if a node exists'''
while cur != None:
if cur.elem == item:
return True
else:
cur = cur.next
return False
```

### 6. Comparison of Chain and Sequence Tables

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.

Tags: Python

Posted on Mon, 15 Jun 2020 21:36:52 -0400 by ianhull