&quot'attributeError:' linkedlist'对象没有属性' next;&quot尝试使用Python中的链接列表在堆栈中弹出无值时

发布于 2025-02-08 15:06:03 字数 1343 浏览 2 评论 0原文

嘿,我正在尝试在堆栈中实现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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2025-02-15 15:06:03

问题在于,您在按/pop逻辑中省略了linkedlist图层 - 它假设llnode实例,但是它是linkedlist。您的代码中没有提到self.ll.head,而这是self.ll具有的唯一属性。

显然,这个额外的层只是一个滋扰,如果确实可以像您一样使用代码,那么它会更实用,而没有此额外的层。

作为解决方案,我建议您制作stack linkedlist的子类,以便ll的级别消失。现在,用head替换所有这些ll,它将起作用:

class Stack(LinkedList):  # subclass
    # No need for an explicit constructor now
    
    def push(self, new_element):
        # Reference `head` instead of `ll` (which no longer exists)
        if self.head is None:
            self.head = new_element
        else:
            current = new_element
            current.next = self.head
            self.head = new_element

    def pop(self):
        # ... Same here
        if self.head is None:
            return None
        else:
            popped = self.head
            self.head = self.head.next
            return popped

The problem is that you omit the LinkedList layer in your push/pop logic -- it assumes that ll is a Node instance, but it is a LinkedList. There is no reference to self.ll.head in your code, while that is the only attribute that self.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 of LinkedList, so that the level of ll disappears. Now replace all those ll with head and it will work:

class Stack(LinkedList):  # subclass
    # No need for an explicit constructor now
    
    def push(self, new_element):
        # Reference `head` instead of `ll` (which no longer exists)
        if self.head is None:
            self.head = new_element
        else:
            current = new_element
            current.next = self.head
            self.head = new_element

    def pop(self):
        # ... Same here
        if self.head is None:
            return None
        else:
            popped = self.head
            self.head = self.head.next
            return popped
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文