現在のコードでリンクリストを作成し、リンクリストを昇順でソートしようとしています。AttributeError: 'NoneType'オブジェクトに 'data'属性がありません。リンクリスト
import random
random_nums = random.sample(range(100), 10)
class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = None
def __str__(self):
return str(self.data)
def insertNode(data, first_node):
current_node = first_node
while current_node !=None:
if data > current_node.data and data <= current_node.next.data:
new_node = Node(data, current_node.next)
last_node.next = new_node
print("Inserting node: " + str(data))
current_node = current_node.next
first_node = Node(random_nums[0], None)
for i in random_nums[1:]:
insertNode(i, first_node)
print("Linked list values:")
current_node = first_node
while current_node != None:
print (str(current_node.data) + " => ", end="")
current_node = current_node.next
input()
は現在、任意の提案を、私は、Pythonには本当に新しいですエラー
File "python", line 25, in File "python", line 16, in insertNode AttributeError: 'NoneType' object has no attribute 'data'
を取得し、この作業を取得しようとしていますか?
を(HTTP ://stackoverflow.com/questions/280243/python-linked-list)、もっと洗練されています。 –
あなたのユースケース(存在する場合)に応じて、[pythonの 'deque'](https://docs.python.org/3/library/collections.html#collections.deque)を使用するともっと多くなる場合があります実用的です - リンクされたリストとして実装されています(http://stackoverflow.com/a/6257048/5349916)。 – MisterMiyagi