Python, Linked list: AttributeError: 'function' object has no attribute 'get_next'

Python, Linked list: AttributeError: 'function' object has no attribute 'get_next'

我正在尝试实现一个简单的链表,但我一直收到此异常:AttributeError:'function' 对象没有属性 'get_next'。 这是我的代码:

           class Lista:
    def __init__(self):
        self.root = None
        self.len_l = 0

    def __str__(self):
        if not self.len_l:
            return "::EMPTY::"
        lista_c = ""
        next_node = self.root
        while next_node:
            if next_node.get_next():
                if type(next_node.get_datos()) != str:
                    lista_c += "%s -> "%(next_node.get_datos())
                else:
                    lista_c += "'%s' -> "%(next_node.get_datos())
            else:
                if type(next_node.get_datos()) != str:
                    lista_c += " %s"%(next_node.get_datos())
                else:
                    lista_c += " '%s'"%(next_node.get_datos())

            next_node = next_node.get_next()
        return lista_c

    def add(self, dato):
        self.root = nodo(dato,self.root)
        self.len_l += 1

    def find(self,dato):
        if not self.len_l:
            raise LSLexception("empty list")
        this_node = self.root
        while this_node.get_next():
            if dato == this_node.get_datos():
                return this_node
            this_node = this_node.get_next()
        return -1

    def Valid_pos(self,pos):
        if self.len_l == 0:
            raise LSLexception("empty list")
        if pos not in range(0,self.len_l):
            raise LSLexception("data overflow")

    def remove(self,dato):
        if not self.len_l:
            raise LSLexception("empty list")
        if self.root.get_datos() == dato:
            self.root = self.root.get_next()
        previous_node = self.root
        this_node = previous_node.get_next()
        while this_node.get_next():
            if this_node.get_datos() == dato:
                previous_node.set_next(this_node.get_next())
                return this_node 
            else:
                previous_node = this_node
                this_node = previous_node.get_next()
        return -1

问题出现在函数删除这一行 while this_node.get_next(): 但我在函数 "find" 和 I 中使用同一行。工作正常,有什么想法吗?是为了上大学所以有些事情我无法改变。

您尝试调用 get_next 时忘记加上括号,因此一些节点变量被分配给函数而不是节点。

喜欢 def remove(self, dato) 中的 this_node = previous_node.get_next,您可能需要将其更改为 this_node = previous_node.get_next()

此外,previous_node.set_next = this_node.get_next 中的 set_next 对我来说也像是一个函数。你可能也想改变它。