提问者:小点点

Python链接列表-属性错误:'NoneType'对象没有属性'get_data'与删除函数


我是python新手,一直在尝试学习简单的数据结构。我已经能够为一个链表拼凑出一些函数,并且我的delete函数遇到了问题。下面列出了有问题的函数和测试用例:类节点:definit(self,initial_data):self。数据=初始数据自身。下一个=无

def get_data(self):
    return self.data

def get_next(self):
    return self.next

def set_data(self, new_data):
    self.data = new_data

def set_next(self, new_next):
    self.next = new_next

类LinkedList:def init(self):self。头=无

def __str__(self):
    output_string = ''

    current = self.head
    while current is not None:
        output_string += str(current.get_data())
        next_node = current.get_next()
        #gives the pointer to the next node i.e. the next node is that which is next to the current

        if next_node is not None:
            output_string += "->"

        current = next_node

    return output_string
#does not need to be changed for ordered linked list
def is_empty(self):
    if self.head is None:
        return True
    else:
        return False
def insert(self, data):
    current = self.head
    previous = None
    stop = False
    while current != None and not stop:
        if current.get_data() > data:
            stop = True
        else:
            previous = current
            current = current.get_next()
    temp = Node(data)
    if previous == None:
        temp.set_next(self.head)
        self.head = temp
    else:
        temp.set_next(current)
        previous.set_next(temp)



#does not need to be changed for ordered linked list
def size(self):
    current = self.head
    count = 0
    while current != None:
        count += 1
        current = current.get_next()
    return count
def search(self, item):
    current = self.head
    found = False
    stop = False

    while current is not None and not found and not stop:
        if current.get_data() == item:
            found = True

        else:
            current = current.get_next()
    return found

def delete(self, item):     
    current = self.head
    previous = None
    found = False

    while not found:
        if current.get_data() == item:
            found = True
        else:
            previous = current
            current = current.get_next()

    if previous is None:
        self.head = current.get_next()
    else:
        previous.set_next(current.get_next())

def test_nonexistent():
    my_list = LinkedList()
    my_list.insert(31)
    my_list.insert(77)
    my_list.insert(17)
    my_list.insert(93)
    my_list.insert(26)
    my_list.insert(54)
    assert my_list.size() == 6
    my_list.delete(77)
    my_list.delete(1)
    assert my_list.size() == 5

我收到了错误信息

"属性错误:'NoneType'对象没有属性'get_data'和删除函数"

我认为delete函数有点问题,因为它不能处理列表中没有的值,但是我现在很困惑如何让它工作。感谢您的帮助!


共2个答案

匿名用户

您正在使用来指示列表中没有其他节点,即您在最后,但是在搜索列表时您从来没有测试过,所以当您到达最后时,您尝试调用N<-_data代码。解决办法:不要那样做。

返工循环的一个好方法可能是将while循环更改为testcurrent,并在找到项目时使用break退出循环。

while current is not None:
    if current.get_data() == item:
        break
    else:
        previous = current
        current = current.get_next()

您需要重做一些后面的逻辑来解释这一点。您不需要单独的找到标志;您可以简单地检查当前。如果是,则到达列表末尾时没有找到要查找的项。否则,你找到了它,而当前就是它。

匿名用户

这有点难理解,因为您还没有发布实际的LinkedList,但我假设您正在调用current。get_next()次数太多,可能会到达列表的末尾。也许像这样的东西对你有魔力(更新):

def delete(self, item):     
    current = self.head # we don't need the previous variable
    previous = None
    found = False
    while current is not None and not found:
        if current.get_data() == item:
            found = True
        else:
            previous = current
            current = current.get_next()
    # at this point we should have either reached to end of the list
    # and current is None
    # or we have found the node we were looking for
    if found and previous is not None:
        # delete it
        previous.set_next(current.get_next())
    elif found and previous is None:
        # removing head element
        self.head = None
    else:
        # nothing found
        print("Do whatever you want here")