错误:<__main__.Node object at 0x03A5F990> 使用链表时

Pav*_*ath 2 python linked-list object data-structures

以下是使用 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()
Run Code Online (Sandbox Code Playgroud)

输出:

list
<__main__.Node object at 0x03A9F970>
<__main__.Node object at 0x03A9F990>
Run Code Online (Sandbox Code Playgroud)

要检查您的答案,您需要编辑此内置方法def __repr__并重写它。

您也可以通过添加__str__方法来做到这一点

Mas*_*ler 5

这不是错误。您看到的正是您要求的输出:两个 Node 对象。

问题是你还没有定义__repr____str__在你的 Node 类上,所以没有直观的方法来打印出节点对象的值。它所能做的就是下注并为您提供默认值,这是相当无益的。