以下是在python.Only從頭創建一個鏈表的代碼追加功能都是在這裏完成:下面的鏈表如何在Python中工作?
class Element(object):
def __init__(self, value):
self.value = value
self.next = None
class LinkedList(object):
def __init__(self, head=None):
self.head = head
def append(self, new_element):
current = self.head
if self.head:
while current.next:
current = current.next
current.next = new_element
else:
self.head = new_element
我完全不懂行是如何工作的
current = current.next
current.next = new_element
我認識到,第一行是沿着鏈表移動(就像增量一樣),第二行是將前一個節點的地址鏈接到鏈表的下一個元素。但是有人可以解釋一下如何在更深層次上實際工作如何分配什麼(它們是什麼類型的變量,賦值地址或值)
你問如何Python的變量/分配工作? –
只是閱讀這個:https://nedbatchelder.com/text/names.html –
你在找什麼樣的答案? LinkedList對象只保留對第一個元素的引用。爲了追加,while循環需要遵循所有'next'鏈接才能找到最後一個。 –