[英]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.