(Python)'NoneType'对象不可调用(Linked List实现)

时间:2013-08-28 16:02:48

标签: python callable

所以我已经解决了几个问题,在这里获得帮助,并且 来自我认识的人。我的问题的根源是我不知道如何包装无 我没有得到这些没有属性或无法调用的错误。

对于这个链表,我真正需要的只是insert和printlist。 我没有包含打印列表,因为它很简单,并且没有引起问题。

错误位于elif下插入下的Linked_List下。 它评论如下:#< ---- ERROR

以下是代码:

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

1 个答案:

答案 0 :(得分:0)

你如何使用它?我猜你做的事情是yourList.insert(1)。在您的代码中,您执行:self.head = node,其中node是用户传递给insert的内容。因此,在下次拨打insert时,您最终会尝试拨打int或您尝试放入列表的任何内容。您需要使用Node类包装用户提供的任何对象:

def insert(self, thing):
    node = Node(thing)
    //...

但是,请记得发布所有相关代码,以便那些试图帮助您的人不必猜测。

编辑:仍然,编辑后案例仍然相同。你没有包装传递给你的列表的对象,所以你一直试图在非Node对象上调用Node方法......