只有简单的插入操作:
class Node: def __init__(self, n, pos=-1): self.n = n self.next_ = pos linear_list = [] def insert(n, index=None): if index: i = 0 k = 0 while index < len(linear_list): if linear_list[i].next_ == index: k = linear_list[i].next_ linear_list[i].next_ = len(linear_list) linear_list.append(Node(n, k)) else: for _ in linear_list: if _.next_ == -1: _.next_ = len(linear_list) linear_list.append(Node(n, -1)) def travl(): n = 0 while n != -1: print(linear_list[n].n) n = linear_list[n].next_ insert(9) insert(1) insert(3) insert(4) travl()