0
当前的代码,我试图做一个链表,然后按升序对链表进行排序。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()
目前得到的错误
File "python", line 25, in File "python", line 16, in insertNode AttributeError: 'NoneType' object has no attribute 'data'
我真的很新的Python和努力得到这个工作,有什么建议?
不要重新发明轮子,看看[Python的链表的问题(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