领结链表法
- 时间复杂度n(n+e)
- 缺点:增加和删除比较复杂;
代码
class Node:
def __init__(self, value):
self.value = value
self.next = None
def Create_List(graph_list, list_size):
head_list = [Node] * list_size
for i in range(1, len(head_list)):
head_list[i].value = i
ptr = head_list[i]
print("顶点:", i, end="\t==>\t")
for j in range(len(graph_list)):
if graph_list[j][0] == i:
new_node = Node(graph_list[j][1])
while not ptr:
ptr = ptr.next
ptr = new_node
print(f"[{new_node.value}]",end="\t")
print()
return head_list
if __name__ == '__main__':
graph = [[1, 2], [2, 1], [2, 5], [5, 2], [2, 3], [3, 2], [2, 4], [4, 2], [3, 4], [4, 3], [3, 5], [5, 3], [4, 5],
[5, 4]]
neibour_list = Create_List(graph, 6)