【问题标题】:Python Linked List, Unsure what is causing this errorPython Linked List,不确定是什么导致了这个错误
【发布时间】:2020-02-29 14:11:49
【问题描述】:

我正在尝试创建一个链接列表,但不确定我是如何得到这个错误的: 编辑代码和错误跟踪:

第 61 行,在模块中 llist.output()

第 56 行,在输出中 llist.delete("阿拉巴马州")

第 50 行,在删除中 previous_node.pointer = current_node.pointer

AttributeError: 'NoneType' 对象没有属性 'pointer'

这是我的代码:

class linkedlist:
    data = None
    class node:        
        data = None    
    start = None
    pointer = None
    def add(self, item):
            #check memory overflow
            try:
                new_node = linkedlist.node()
                new_node.data = item
                current_node = self.start
                #list is empty
                if current_node == None:
                    new_node.pointer = None
                    self.start = new_node
                else:
                    #item becomes the new start item
                    if item > current_node.data:
                        self.start = new_node
                        new_node.pointer = current_node
                    else:
                        #Find correct position in the list
                        while current_node != None and current_node.data < item:
                            previous_node = current_node
                            current_node = current_node.pointer               
                        new_node.pointer = previous_node.pointer
                        previous_node.pointer = new_node
                return True
                return self               
            except:
                return False        
    def delete(self,item) :
                current_node = self.start
                if current_node == None:
                    print("list is empty")
                    return False                
                #check the list is not empty
                if current_node != None:
                    #item is the start node
                    if item == current_node.data:
                        self.start = current_node.pointer
                    else:
                        #Find item in the list
                        while current_node != None and item != current_node.data:
                            previous_node = current_node
                            current_node = previous_node.pointer
                        previous_node.pointer = current_node.pointer                
    def output(self) :
        items = ["Florida", "Georgia","Delaware","Alabama","California"]
        for index in range(0, len(items)):
            llist.add(items[index])
        llist.delete("Alabama")
        for index in range(0, len(items)):
            print(items[index])                
llist = linkedlist()            
llist.output()

【问题讨论】:

    标签: python python-3.x linked-list


    【解决方案1】:

    在第 47 行,您使用了变量previous_node,而没有先声明它。你声明了一个叫previous,所以我假设你打算把那个命名为previous_node

    while current_node != None and item != current_node.data: 
        # this line used to say
        # previous = current_node
        previous_node = current_node               
        current_node = previous_node.pointer     
        previous_node.pointer = current_node.pointer
    

    编辑: 另外,我认为您的删除功能中的这一行是错误的:

    current_node = self
    

    您正在将 current_node 分配给整个列表。你可能打算做的是current_node = self.start

    编辑#2: 一个忠告,当你遇到错误时,请阅读它所说的内容并尝试解释它并弄清楚它想告诉你什么。

    这是你的错误:

    line 61, in module llist.output()
    
    line 56, in output llist.delete("Alabama")
    
    line 50, in delete previous_node.pointer = current_node.pointer
    
    AttributeError: 'NoneType' object has no attribute 'pointer'
    

    这称为堆栈跟踪。它显示了发生错误时堆栈上的函数。你调用了lllist.output(),在那个函数中你调用了llist.delete('Alabama'),在delete函数中你在第50行得到了错误,那行写着previous_node.pointer = current_node.pointer

    上面写着'Nonetype' object has no attribute 'pointer'.,这意味着previous_nodecurrent_node 在第50 行是None

    这里的问题是,一旦current_nodeNone,你就会跳出循环。

    您应该做的是在您跳出 while 循环后检查 current_node 是否为 None。如果是这种情况,您还没有找到您要删除的项目(它不在列表中)。

    同样,当您遇到错误时,请尝试弄清楚它试图告诉您什么。这就是调试:单步调试代码并找出问题的原因,然后修复它。我已经帮助你解决了三四个不同的错误,太棒了!但是你很聪明,你比我更了解你的代码。有时将所有内容立即带到 Stack Overflow 并不是最好的学习方式。

    我希望这会有所帮助。我不是要说教,我只是想让你知道,通过实际阅读错误消息,你可以擅长调试自己的代码。

    【讨论】:

    • 第 46 行,在删除 current_node = previous_node.pointer AttributeError: 'linkedlist' object has no attribute 'pointer'
    • 听起来节点设置为None,而您正在调用node.pointer。您应该事先检查节点是否为None
    • 您会使用整个错误堆栈跟踪以及您当前更新的代码来编辑您的问题吗?
    【解决方案2】:

    当你到达这条线时:

    previous_node.pointer = new_node
    

    Python 不知道“previous_node”是什么,因为您从未定义过它。 我的猜测是这永远不会被执行:

    while current_node != None and current_node.data < item:
      previous_node = current_node
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-24
      • 2011-08-13
      • 2012-10-06
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2012-09-21
      • 2010-10-12
      相关资源
      最近更新 更多