【问题标题】:Creating recursive function findValue() in linked list在链表中创建递归函数 findValue()
【发布时间】:2019-05-03 03:40:28
【问题描述】:

我正在尝试创建一个名为 findValue() 的递归函数,用于确定给定数据值是否在链表中。如果值存在,该函数返回 True,否则返回 False。这是 Node 类的实现:

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

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setData(self,newdata):
        self.data = newdata

    def setNext(self,newnext):
        self.next = newnext

这是我到目前为止的代码:

from Node import Node

def findValue(value, linkedList):
    if value == linkedList.getData():
        return True
    if linkedList == None:
        return False
    else:
        print("now:", linkedList.getData())
        print("next", linkedList.getNext())
        return findValue(value, linkedList.getNext())

我尝试使用这些值测试我的代码:

n1 = Node(10); n2 = Node(20); n3 = Node(30); head = Node(40); n2.setNext(n1); 
n3.setNext(n2); head.setNext(n3)

当给定的值不在我的链接列表中时,我希望我的代码返回 False。但是,当我尝试 findValue(50, head) 时,我得到:

now: 40
next: <Node.Node object at 0x10c410ac8>
now: 30
next: <Node.Node object at 0x10bf3b710>
now: 20
next: <Node.Node object at 0x10de5b898>
now: 10
next: None
Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    findValue(50, head)
  File "/Users/Shared/Homework3/hw3.py", line 29, in findValue
    return findValue(value, linkedList.getNext())
  File "/Users/Shared/Homework3/hw3.py", line 29, in findValue
    return findValue(value, linkedList.getNext())
  File "/Users/Shared/Homework3/hw3.py", line 29, in findValue
    return findValue(value, linkedList.getNext())
  [Previous line repeated 1 more time]
  File "/Users/Shared/Homework3/hw3.py", line 22, in findValue
    if value == linkedList.getData():
AttributeError: 'NoneType' object has no attribute 'getData'

所以到最后,我看到下一个值是无。不应该 None 是递归调用的参数,因此如果 linkedList == None: 将为真,因此返回 False?

【问题讨论】:

    标签: python


    【解决方案1】:

    这可以通过对 4 行进行不同的排序来解决。在这些行中:

        if value == linkedList.getData():
            return True
        if linkedList == None:
            return False
    

    当您的代码使用linkedList == None 到达此处时,它会尝试在空对象中查找.getData(),然后再检查对象是否为空并返回false。重新排序这些,你应该很高兴:

        if linkedList == None:
            return False
        if value == linkedList.getData():
            return True
    

    【讨论】:

      【解决方案2】:

      首先,我想说我不确定这个特定问题是否属于 CS StackExchange - 我们更多的是关于数学和理论,而不是编码(尽管我们仍然编码,我敢肯定)。我会指出你使用 StackOverflow 或一些更面向编程的 StackExchanges 来解决未来的问题。

      说了这么多,让我们来看看发生了什么。

      你有一个 Node 类,它有数据和下一个引用(下一个引用是另一个节点)。

      Node 类也有 getter 和 setter,很好,很好。

      现在您似乎在遍历 LinkedList 时遇到了某种问题 - 您的 findValue() 函数似乎在 NoneType 对象上调用 getData - 我假设它相当于 Java 和其他中的 null语言。

      因此,对于您的示例,您知道 50 根本不在 LinkedList 中。您调用 findValue 并通过您的 linkedList 递归,然后它神秘地中断...

      切换你的 if 语句,所以 None 检查首先出现。如果 LinkedList 是 NoneType 对象怎么办?然后检查 getData() 将失败,因为您在 没有 具有该属性/方法的东西上调用它 - 而您这样做是因为 getData 检查在 None 检查之前。我认为这是问题的根源。

      但是,用 Knuth 的话来说——“当心上述代码中的错误;我只是证明它是正确的,没有尝试过。”

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-07
        • 1970-01-01
        • 1970-01-01
        • 2017-03-19
        • 2016-12-13
        • 1970-01-01
        • 1970-01-01
        • 2013-07-27
        相关资源
        最近更新 更多