&quot'attributeError:' linkedlist'对象没有属性' next;&quot尝试使用Python中的链接列表在堆栈中弹出无值时
嘿,我正在尝试在堆栈中实现POP功能。它的工作时间很长,列表中有元素。之后,此错误“ attributeError:'linkedlist'对象没有属性'next'”出现。
我试图在调试器的帮助下找到问题,但是我发现的唯一一件事是,问题出现在第三个流行功能。因此,在它是空的。
class Element(object):
def __init__(self, value):
self.value = value
self.next = None
class LinkedList(object):
def __init__(self, head=None):
self.head = head
class Stack(object):
def __init__(self,top=None):
self.ll = LinkedList(top)
def push(self, new_element):
"Push (add) a new element onto the top of the stack"
if self.ll is None:
self.ll = new_element
else:
current = new_element
current.next = self.ll
self.ll = new_element
def pop(self):
"Pop (remove) the first element off the top of the stack and return it"
if self.ll is None:
return None
else:
popped = self.ll
self.ll = self.ll.next
return popped
# Test cases
# Set up some Elements
e1 = Element(1)
e2 = Element(2)
e3 = Element(3)
e4 = Element(4)
# Start setting up a Stack
stack = Stack(e1)
# Test stack functionality
stack.push(e2)
stack.push(e3)
print stack.pop().value
print stack.pop().value
print stack.pop().value
print stack.pop()
stack.push(e4)
print stack.pop().value
Hey I'm trying to implement a pop function in my Stack. It works as long there are elements in the list. After that this error "AttributeError: 'LinkedList' object has no attribute 'next'" comes up.
I tried to find the issue with the help of the debugger, but the only thing i found out is that the issue comes up at the third the pop function gets called. So at the time when it is empty.
class Element(object):
def __init__(self, value):
self.value = value
self.next = None
class LinkedList(object):
def __init__(self, head=None):
self.head = head
class Stack(object):
def __init__(self,top=None):
self.ll = LinkedList(top)
def push(self, new_element):
"Push (add) a new element onto the top of the stack"
if self.ll is None:
self.ll = new_element
else:
current = new_element
current.next = self.ll
self.ll = new_element
def pop(self):
"Pop (remove) the first element off the top of the stack and return it"
if self.ll is None:
return None
else:
popped = self.ll
self.ll = self.ll.next
return popped
# Test cases
# Set up some Elements
e1 = Element(1)
e2 = Element(2)
e3 = Element(3)
e4 = Element(4)
# Start setting up a Stack
stack = Stack(e1)
# Test stack functionality
stack.push(e2)
stack.push(e3)
print stack.pop().value
print stack.pop().value
print stack.pop().value
print stack.pop()
stack.push(e4)
print stack.pop().value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,您在按/pop逻辑中省略了
linkedlist
图层 - 它假设ll
是node
实例,但是它是linkedlist
。您的代码中没有提到self.ll.head
,而这是self.ll
具有的唯一属性。显然,这个额外的层只是一个滋扰,如果确实可以像您一样使用代码,那么它会更实用,而没有此额外的层。
作为解决方案,我建议您制作
stack
linkedlist
的子类,以便ll
的级别消失。现在,用head
替换所有这些ll
,它将起作用:The problem is that you omit the
LinkedList
layer in your push/pop logic -- it assumes thatll
is aNode
instance, but it is aLinkedList
. There is no reference toself.ll.head
in your code, while that is the only attribute thatself.ll
has.It becomes apparent that this extra layer is just a nuisance, and it would have been more practical if indeed you could use the code as you did -- without this extra layer.
As a solution I would suggest that you make
Stack
a subclass ofLinkedList
, so that the level ofll
disappears. Now replace all thosell
withhead
and it will work: