[英]Runestone add node to ordered linked list - error
因此,我正在关注Runestone Academy在线python数据结构和算法课程。
在他们的有序列表中,他们使用此代码添加节点
def add(self,item):
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.getData() > item:
stop = True
else:
previous = current
current = current.getNext()
temp = Node(item)
if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
previous.setNext(temp)
由于其顺序,它们包括stop变量,该变量告诉节点遍历在看到更大的节点时停止。
我的问题是:
while current != None and not stop:
没错吗?
应该不是“或”而不是“与”吗?
因为如果是“ and”,则表示:
current!=None
还必须满足条件,该条件仅发生在列表的末尾,因此即使达到停止条件,也不会因为该当前条件而停止遍历!=无条件。
希望我没有混淆太多...
这是布尔代数。 当其条件为真时运行。 AND
仅在双方都为真时为真,因此如果current != None
任何一个current != None
为false或not stop
为false则AND
将为false,但是如果您使用OR
而不是AND
则BOTH则必须为false停下来,那会把你弄错
我建议您进一步阅读逻辑门,特别是AND门
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.