私は初心者です。私はPythonプログラミングを学び始めました。私はエラーで立ち往生しています。リンクされたリストタイプのエラー
Traceback (most recent call last):
File "C:/Users/XPS 13/PycharmProjects/DataStructures/LinkedList/Implementation/SinglyLinkedList.py", line 154, in <module>
ll.insertNode(2)
File "C:/Users/XPS 13/PycharmProjects/DataStructures/LinkedList/Implementation/SinglyLinkedList.py", line 92, in insertNode
self.insertTail(node)
File "C:/Users/XPS 13/PycharmProjects/DataStructures/LinkedList/Implementation/SinglyLinkedList.py", line 121, in insertTail
while currentNode.getNext() != None:
TypeError: getNext() missing 1 required positional argument: 'self'
Process finished with exit code 1
誰かが私にそれが理解されるであろう。このエラーの理由を説明してくださいすることができた場合:私は見ていますエラーがある型エラーに
class Node:
def __init__(self):
self.data = None
self.next = Node
def setData(self,data)
self.data = data
def getData(self):
return self.data
def setNext(self,next):
self.next = next
def getNext(self):
return self.next
def hasNext(self):
return self.next!=None
class LinkedList(object):
def __init__(self):
self.length = 0
self.head = None
def listLength(self):
currentNode = self.head
length = 0
while currentNode.hasNext:
length = length + 1
currentNode = currentNode.getNext()
return length
"""
Methods to Insert nodes in a Linked List:
# insertNode: Use this method to simply insert a node to the Linked List
# insertHead: Use this method to insert a node at the head of the Linked List
# insertTail: Use this method to insert a node at the tail of the Linked List
# insertAtPosition: Use this method to insert a node at a particular position of the Linked List
"""
def insertNode(self,node):
if self.length == 0:
self.insertHead(node)
else:
self.insertTail(node)
def insertHead(self, data):
nodeToBeInserted = Node()
nodeToBeInserted.setData(data)
if self.length == 0:
self.head = nodeToBeInserted
else:
nodeToBeInserted.setNext(self.head)
self.head = nodeToBeInserted
self.length = self.length + 1
def insertTail(self,data):
nodeToBeInserted = Node()
nodeToBeInserted.setData(data)
currentNode = self.head
while currentNode.getNext() != None:
currentNode = currentNode.getNext()
currentNode.setNext(nodeToBeInserted)
self.length = self.length + 1
def insertAtPosition(self,data, position):
if position > self.length or position < 0:
print("Invalid position!, The size of the Linked List is:%s"%self.length)
else:
if position ==0:
self.insertHead(data)
else:
nodeToBeInserted = Node()
nodeToBeInserted.setData(data)
currentNode = self.head
count = 0
while count < position - 1:
currentNode = currentNode.getNext()
count = count + 1
nodeToBeInserted.setNext(currentNode.getNext())
currentNode.setNext(nodeToBeInserted)
self.length = self.length+1
ll = LinkedList()
ll.insertNode(1)
ll.insertNode(2)
ll.insertNode(3)
を取得します。
ありがとうございます。
class Node:
def __init__(self):
self.data = None
self.next = Node
self.next = None
次のようになります。あなたは右ここにタイプミスを持っているよう
'x!= None'ではなく' x is None'を使うべきです。詳細はPEP 8を参照してください。 http://pep8.org/ –