我正在尝试实现一个简单的链表,但我不断收到此异常: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():
中的函数remove中,但是我在函数“查找” I中使用同一行。工作正常,有什么想法吗?是上大学的,所以有些事情我无法改变。
答案 0 :(得分:0)
当您尝试调用get_next
时,您忘了加上括号,因此某些节点变量分配给了函数而不是节点。
就像this_node = previous_node.get_next
中的def remove(self, dato)
一样,您可能希望将其更改为
this_node = previous_node.get_next()
另外,set_next
中的previous_node.set_next = this_node.get_next
在我看来也像一个函数。您可能也想更改它。