내 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,'-')
감사합니다. 내 코드에 결함이 있음을 알 수 있습니다. – Liondancer
그러나 값으로 n 값을 가진 노드를 하나만 입력하면 어떻게됩니까? – Liondancer
@Liondancer : 좋은 지적 : 제안 된 코드의 순서가 잘못되어이 경우 작동하지 않습니다. 결정된. –