我有一个关于在python中实现链表的问题。
class Node:
def __init__(self, data):
self.data = data
self.next = None
class SLL:
head = None
def insert_at_end(self, data):
node = Node(data)
if self.head == None:
self.head = node
else:
t = self.head
while t.next is not None:
t = t.next
t.next = node
def display_all(self):
if self.head == None:
print(None)
else:
t = self.head
while t is not None:
print(t.data, end = ' ')
t = t.next
if __name__ == '__main__':
a = SLL()
b = SLL()
a.insert_at_end(2)
b.insert_at_end(1)
a.insert_at_end(4)
b.insert_at_end(3)
a.insert_at_end(6)
b.insert_at_end(5)
a.display_all()
print()
b.display_all()
代码与输出配合良好。。。
2 4 6
1 3 5
我的问题是,这与我在网上看到的实现有什么区别:
class SLL:
def __init__(self):
self.head = None
def insert_at_end(self, data):
node = Node(data)
if self.head == None:
self.head = node
else:
t = self.head
while t.next is not None:
t = t.next
t.next = node
def display_all(self):
if self.head == None:
print(None)
else:
t = self.head
while t is not None:
print(t.data, end = ' ')
t = t.next
具体来说,我使用类var(即head
(的实现与使用它作为构造函数内实例var的其他实现之间有什么区别?
head = None` vs. `self.head = None
附言:很抱歉,如果这个问题已经被回答了,我看了看,却找不到答案(甚至找不到如何找到答案(。
第一个版本定义了一个类属性。这意味着,当实例读取self.head
时,它实际上读取的是类属性,而不是实例属性(它还不存在(。结果是一样的:它是None
。
通过在插入任何节点之前输出a.__dict__
,可以在空列表上看到差异。注意区别:第一个版本显示一个空字典,另一个显示一个head
键。
但是,当代码继续并且将节点实例分配给self.head
时,实例属性在这两个代码版本中都是目标。这意味着一旦插入了第一个节点,第一个版本就开始表现为第二个版本。对self.head
的所有后续引用都是对实例属性的引用,而不再是对类属性的引用。
在将第一个节点添加到列表后,尝试输出a.__dict__
,现在在使用第一个实现时,您还会看到head
键。
因此,在这个具体的例子中,实际差异很小。类变量永远不会更改值:无论您创建了多少个列表以及它们有多少个节点,它都保持为None
。一旦一个实例将其第一个节点添加到其中,它就开始使用自己的head
属性。