def remove(lst,value):
curr = lst.head
while not isinstance(curr, EmptyNode):
if curr.data == value:
curr.data=curr.nxt.data
curr.nxt =curr.data.nxt
lst.size -= 1
return lst
curr = curr.nxt
return True
return False
该函数应该删除给定的值,并返回一个布尔值(True)。例如,如果我输入:Python链接列表,删除函数iter
remove(lstA, 'b')
它应该返回True,但是当我运行我的程序,它给我这个错误消息:
curr.nxt =curr.data.nxt
AttributeError: 'str' object has no attribute 'nxt'
任何一个小提示,将不胜感激
以及我尝试你说的是否cure.next.data ==值:,似乎是工作是什么,但它不是<在0x30e3930 __ __为主。MYLIST对象>输出布尔值,它被输出。 – user3408174
'<__main __。0x30e3930的MyList对象>'意味着正在执行'if'块中的'return lst'。检查我的观点1,为什么你的代码不会返回'True' – shaktimaan
ohh,我看到所以只需将lst更改为True即可,但如果我想从列表中删除一个元素,那么该元素不存在?它应该返回False,但对于我的代码,它不会返回任何内容 – user3408174