以下是使用Python实现的链表:
class Node:
def __init__(self,data,next):
self.data = data
self.next = next
class List:
head=None
tail=None
def printlist(self):
print("list")
a=self.head
while a is not None:
print(a)
a=a.next
def append(self, data):
node = Node(data, None)
if self.head is None:
self.head = self.tail = node
else:
self.tail.next = node
self.tail = node
p=List()
p.append(15)
p.append(25)
p.printlist()
输出:
list
<__main__.Node object at 0x03A9F970>
<__main__.Node object at 0x03A9F990>
要检查您的答案,您需要编辑此内置方法 def __repr__
并重写它。
您还可以通过添加 __str__
方法来实现此目的
最佳答案
这不是一个错误。您看到的正是您所要求的输出:两个 Node 对象。
问题是你没有defined __repr__
or __str__
on your Node class ,因此没有直观的方法来打印节点对象的值。它所能做的就是下注并给你默认值,这毫无帮助。
关于python - 使用链接列表时出现错误 : <__main__. 位于 0x03A5F990> 的节点对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31595968/