2.(图)-邻接表链表表示

 

class list_node:
    def __init__(self):
        self.val = 0
        self.next = None


head = [list_node] * 6
newnode = list_node()
data = [[0, 5], [1, 2], [2, 1], [1, 5], [5, 1], [2, 3], [3, 2], [2, 4], [4, 2], [3, 4], [4, 3]]
for i in range(6):
    head[i].val = i
    head[i].next = None
    ptr = head[i]
    print('顶点%d=>' % i, end=' ')
    for j in range(len(data)):
        if data[j][0] == i:
            newnode.val = data[j][1]
            while ptr != None:
                ptr = ptr.next
            ptr = newnode
            print('[%d] ' % newnode.val, end=' ')
    print()

output

顶点0=> [5]  
顶点1=> [2]  [5]  
顶点2=> [1]  [3]  [4]  
顶点3=> [2]  [4]  
顶点4=> [2]  [3]  
顶点5=> [1]  

posted @ 2019-03-03 19:33  jj千寻  阅读(145)  评论(0编辑  收藏  举报