所以我已经解决了一些问题,通过在这里获得帮助,以及来自我认识的人的 。我的问题的根源是,我不知道如何包装无如此 ,我不会收到这些错误没有属性,或不可调用。(Python)'NoneType'对象不可调用(链接列表实现)
对于这个链表,我真正需要的是插入和打印列表。 我没有列入打印列表,因为它很简单,并且不会造成问题。
错误在Linked_List下,在插入下,在elif下。 它的注释,以便:#< ----错误
下面是代码:
class Node:
def __init__(self, word):
self.data = word
self.next = None
def nextNode(self):
if self.next is not None:
return self.next
else:
return None
def getData(self):
return self.data
def setNext(self, node):
self.next = node
def hasNext(self):
if self.next == None:
return False
else:
return True
class Linked_List:
def __init__(self):
self.head = Node(None)
self.isempty = True
def insert(self, word):
newNode = Node(word)
prev = self.head.nextNode()
current = self.head.nextNode()
nextFound = False #the next would be the current when it is less than node
#Look for position to insert:
#When empty
if self.isempty == True:
self.isempty = False
self.head = newNode
#When has more than one
elif self.head.hasNext():
while nextFound == False:
if current.getData() > newNode.getData():
prev = current
current = curent.nextNode()
else:
nextFound = True
#Insert
prev.next().setNext(newNode) # <-------ERROR -----HERE~~
newNode.setNext(current)
else:
#When only has one node not empty
if self.head.getData() > newNode.getData():
self.head.setNext(newNode)
else:
newNode.setNext(self.head)
self.head = newNode
插入:
lList.insert(string)
解决这里:
class Linked_List:
def __init__(self):
self.head = Node(None)
self.isempty = True
def insert(self, word):
newNode = Node(word)
prev = self.head.nextNode()
current = self.head.nextNode()
nextFound = False #the next would be the current when it is less than node
#Look for position to insert:
#When empty
if self.isempty == True:
self.isempty = False
self.head = newNode
#When has more than one
elif self.head.hasNext():
while nextFound == False and current != None:
if current.getData() > newNode.getData():
prev = current
if current.hasNext():
current = current.nextNode()
else:
current = None
else:
nextFound = True
#Insert
prev.setNext(newNode)
newNode.setNext(current)
else:
#When only has one node not empty
if self.head.getData() > newNode.getData():
self.head.setNext(newNode)
else:
newNode.setNext(self.head)
self.head = newNode
谢谢大家,耐心等待。 (讽刺) 我问别人解决了我自己的问题,然后我继续发布。当然,我还有另一个问题。我要去尝试我的自我。这就是我为什么离开这个问题的原因。另外,我不知道我可以编辑整个帖子,并提出另一个问题。对我来说很伤心,现在我得到了所有这些努力尽我所能。 – user1831680
感谢您的编辑,我已经退出了我的downvote。但是,您仍未发布*所有相关代码*。你为什么一直期待每个人都想浪费时间猜测你写的是什么? – BartoszKP
我不明白你的意思是相关的代码。 我在链表上唯一的另一件事是打印方法。为了我的意图,我只需要插入和打印。 – user1831680