首页 > 编程语言 > 详细

python - 数组链表

时间:2020-05-21 14:53:03      阅读:41      评论:0      收藏:0      [点我收藏+]

  只有简单的插入操作:

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()

  

python - 数组链表

原文:https://www.cnblogs.com/darkchii/p/12930448.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!