Python链接列表在python中使用链接列表的最简单方法是什么?在方案中,链接列表仅由'(1 2 3 4 5)..Python的列表[1, 2, 3, 4, 5]和元组,(1, 2, 3, 4, 5)实际上,它们并不是链接列表,而链接列表具有一些很好的属性,例如固定时间的连接,并且能够引用它们的不同部分。使他们不变,他们真的很容易使用
3 回答

Smart猫小萌
TA贡献1911条经验 获得超7个赞
#! /usr/bin/env pythonclass Node(object): def __init__(self): self.data = None # contains the data self.next = None # contains the reference to the next nodeclass LinkedList: def __init__(self): self.cur_node = None def add_node(self, data): new_node = Node() # create a new node new_node.data = data new_node.next = self.cur_node # link the new node to the 'previous' node. self.cur_node = new_node # set the current node to the new one. def list_print(self): node = self.cur_node # cant point to ll! while node: print node.data node = node.next ll = LinkedList()ll.add_node(1)ll.add_node(2)ll.add_node(3)ll.list_print()
添加回答
举报
0/150
提交
取消