我的countInt
功能有问题。除了它被标记为 countINT 并且我将'-'
其作为参数放入的事实之外。我测试了我的链接列表的创建,它似乎工作正常。所以我觉得我可以安全地排除这个问题。但是,由于 NoneType 对象没有属性值错误,我不确定我哪里出错了。有人能成为我的另一双眼睛,帮助我找出错误并指导我纠正吗?非常感谢!
输出:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "linked_list.py", line 63, in <module>
print countInt(r,1)
File "linked_list.py", line 25, in countInt
countInt(head.next,n)
File "linked_list.py", line 25, in countInt
countInt(head.next,n)
File "linked_list.py", line 25, in countInt
countInt(head.next,n)
File "linked_list.py", line 25, in countInt
countInt(head.next,n)
File "linked_list.py", line 25, in countInt
countInt(head.next,n)
File "linked_list.py", line 25, in countInt
countInt(head.next,n)
File "linked_list.py", line 25, in countInt
countInt(head.next,n)
File "linked_list.py", line 25, in countInt
countInt(head.next,n)
File "linked_list.py", line 25, in countInt
countInt(head.next,n)
File "linked_list.py", line 25, in countInt
countInt(head.next,n)
File "linked_list.py", line 25, in countInt
countInt(head.next,n)
File "linked_list.py", line 25, in countInt
countInt(head.next,n)
File "linked_list.py", line 23, in countInt
if head.next.value == n:
AttributeError: 'NoneType' object has no attribute 'value'
预期输出:
2
2
我的代码:
class Node:
def __init__(self,value):
self.next = None
self.value = value
def createLinkedList(root, node):
if root.next is None:
root.next = node
else:
createLinkedList(root.next, node)
def countInt(head, n, count= 0): #create a dictionary with keys as the values of the linked list and count up if the value occurs again
count = 0
if head.value is None:
return None
else:
if head.next.value == n:
count += 1
countInt(head.next, n, count)
return count
# root
r = Node(1)
# nodes
a = Node(4)
b = Node(1)
c = Node(5)
d = Node('-')
e = Node(4)
f = Node(1)
g = Node(2)
h = Node('-')
i = Node(8)
j = Node(9)
k = Node(8)
l = Node(3)
createLinkedList(r,a)
createLinkedList(r,b)
createLinkedList(r,c)
createLinkedList(r,d)
createLinkedList(r,e)
createLinkedList(r,f)
createLinkedList(r,g)
createLinkedList(r,h)
createLinkedList(r,i)
createLinkedList(r,j)
createLinkedList(r,k)
createLinkedList(r,l)
print countInt(r,1)
print countInt(r,'-')