链接列表类型错误

时间:2016-09-21 18:38:20

标签: python linked-list typeerror

我是初学者,我开始学习python编程,但我遇到了错误。我收到了类型错误

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)

我看到的错误是:

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

如果有人能够解释这个错误的原因,我们将不胜感激。

谢谢。

3 个答案:

答案 0 :(得分:0)

看起来你在这里有一个拼写错误:

class Node:

    def __init__(self):
        self.data = None
        self.next = Node

那应该是self.next = None

您获得getNext() missing 1 required positional argument的原因是因为最终insertTail到达链接列表的末尾,而Node类型被分配给currentNode。因此,下次您致电currentNode.getNext()时,您实际上正在调用Node.getNext(),这会引发错误,因为没有Node实例隐式分配给self

答案 1 :(得分:0)

while currentNode.hasNext总是如此,因为它是一种方法。

另一方面,while currentNode.hasNext()在某些时候可能是假的。

self.next = Node表示下一堂课。

另一方面,self.next = Node()会创建一个新实例。

但我想你不希望这样,因为它会尝试创建一个无穷无尽的链表,所以self.next = None可能会更好。

这会导致currentNode = currentNode.getNext()将班级分配到currentNode,之后,下一次调用currentNode.getNext()实际上会调用Node.getNext(),这会导致此错误。

答案 2 :(得分:0)

你的代码犯了错误:

Node课程中:

self.next = Node

为什么要发表这个声明?将类本身分配给下一个是不合理的。它应该是:

self.next = None

另一件事,我想建议你为每个班级设计一个好的__init__函数。将类中的__init__函数视为类的构造函数。

__init__函数中,没有提供任何参数,这意味着在实例化类时,您无法将任何数据传递给它。实际上,我们总是需要提供一些数据来实例化一个类。让我们以Node类为例,当我们使用它时,我们通常已经知道该节点应该存储的数据,并且next节点信息可能已知,也可能不知道。因此,定义其__init__函数的更好方法是:

def Node():
    def __init__(self, data, next=None):
        self.data = data
        self.next = next

下次,您使用data属性已知的新实例:

node = Node(data=1)

如果您也知道next属性:

node = Node(data=1, next=next)

这使您更方便,无需调用setX功能。