python - 链表队列

标签 python python-3.x algorithm data-structures linked-list

我的疑问是当我设置 A.next=None 时,kam 变量不应该也存储 None 吗?为什么还是指向节点6?

class Node:

  def __init__(self, data):   # data -> value stored in node
      self.data = data
      self.next = None



a=Node(5)
b=Node(6)
c=Node(7)
d=Node(8)
a.next=b 
b.next=c 
c.next=d 
kam=a.next
a.next=None


while kam is not None:
    print(kam.data)
    kam=kam.next

最佳答案

那是因为你把a实例的next属性设为None,而不是c节点。当 Python 运行 kam=a.next 时,真正发生的是 kam 变成 a.next 指向的任何值。如果您稍后更改 a.next,则并不意味着 kam 会更改。

关于python - 链表队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57745368/

相关文章:

Python面向对象: how to stop procedure flow in an entire class with `return` statement?

python - Python中如何处理异常?

python-3.x - 无法根据条件获取索引

algorithm - 寻找分区问题算法返回 true 的最大值子集

algorithm - 人们可以在圆 table 上坐下多少种不同的可能方式?

python - 在 Python 中,如何生成每列和每行只有一个元素的数组的排列?

python - Pycharm python 控制台 socket.gaierror

Python magic * 或 ** 等效方法

python - 如何从滚动网络表格中抓取特定单词?

python-3.x - 在 pandas 数据框中创建子索引