简体   繁体   English

检查元素是否在链表中

[英]checking if element is in linked list

I'm trying to implement a method that returns the index of a node at a particular index.我正在尝试实现一种方法,该方法返回特定索引处的节点索引。 I can successfully retrieve the index when the element exists;当元素存在时,我可以成功检索索引; however when it doesn't I get an error 'NoneType' object has no attribute 'data' I can't quite get why that happens.但是,当我没有收到错误'NoneType' object has no attribute 'data' 我不太明白为什么会发生这种情况。 here are my linked list and node classes这是我的链表和节点类

class Node:
    def __init__(self,data = None,next = None):
        self.data = data
        self.next = next
class Listy:
    def __init__(self):
        self.head = None

and this is my get_index function这是我的 get_index function

    def get_index(self,key):
        temp = self.head
        count = 0
        while temp.data != key:
            temp = temp.next
            count += 1
        print(count)

Thank you for your help谢谢您的帮助

You get an error because you are trying to get property 'data' of 'temp' in the while loop but your 'temp' is 'None' because you assigned it to 'self.head' which is actually 'None' in case of empty list.你得到一个错误,因为你试图在while循环中获取'temp'的属性'data'但是你的'temp'是'None',因为你将它分配给'self.head'实际上是'None'空列表。 I hope this makes sense我希望这是有道理的

The root caused is because the last node of LinkedList returns None, let's change a bit Your Listy class code to avoid None value.造成的根源是因为LinkedList的最后一个节点返回None,让我们稍微改变一下你的Listy class代码以避免None值。

class Listy:
    def __init__(self, nodes=None):
        self.head = None
        if nodes is not None:
            node = Node(nodes.pop(0))
            self.head = node
            for elem in nodes:
                node.next = Node(elem)
                node = node.next
    
    def get_index(self, key):
        temp = self.head
        count = 0
        currentNode = temp.data
        while temp is not None:
            if count == key:
                currentNode = temp.data
                break
            temp = temp.next
            count += 1
        return currentNode

Let's try to call:让我们尝试调用:

if __name__ == "__main__":
    nodes = ["a", "b", "c"]
    ll = Listy(nodes)
    idx_1 = ll.get_index(0)
    idx_2 = ll.get_index(1)

    print(idx_1)
    print(idx_2)

Return will be as follow:回报如下:

a
b

Your code is too optimistic about finding the key: it does not check whether you have reached the end of the list before finding the value, and so temp will become None resulting in an exception when temp.data is evaluated.您的代码对查找键过于乐观:它不会在查找值之前检查您是否已到达列表末尾,因此temp将变为None导致在评估temp.data时出现异常。

So change your loop to make sure that temp is still not None , and return the index when the key is found (instead of printing it):所以改变你的循环以确保temp仍然不是None ,并在找到键时返回索引(而不是打印它):

def get_index(self, key):
    temp = self.head
    count = 0
    while temp:
        if temp.data == key:
            return count
        temp = temp.next
        count += 1

Note how this will return None when the key is not found.请注意,当找不到密钥时,这将如何返回None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM