所以我已經解決了一些問題,通過在這裏獲得幫助,以及來自我認識的人的 。我的問題的根源是,我不知道如何包裝無如此 ,我不會收到這些錯誤沒有屬性,或不可調用。(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