Python Data Structure and Algorithms--One-way Chain List

Article Directory

linked 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).

Singly linked list

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):
    def __init__(self,elem):
        self.elem = elem = None

2. Operation of Single Chain List

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

3. Implementation of Single Chain List

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 =
        return count

    def travel(self):
        '''Traverse the entire list'''
        cur = self._head
        while cur != None:
            cur =

4. Head and tail and specified location add

    def add(self,item):
        '''Chain Table Head Add Element, Head Interpolation'''
        node = Node(item) = 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
            cur = self._head
            while != None:
                cur =
   = node

    def insert(self,pos,item):
        '''Add element at specified location
        :param pos Start from 0
        if pos <= 0:
        elif pos > (self.length()-1):
            pre = self._head
            count = 0
            while count < (pos - 1):
                count += 1
                pre =
            # When the loop exits, pre point to pos-1
            node = Node(item)
   = node

5. Find and delete elements

    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 =
                pre = cur
                cur =

    def search(self,item):
        '''Find if a node exists'''
        cur = self._head
        while cur != None:
            if cur.elem == item:
                return True
                cur =
        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