2013-08-28 110 views
1

所以我已经解决了一些问题,通过在这里获得帮助,以及来自我认识的人的 。我的问题的根源是,我不知道如何包装无如此 ,我不会收到这些错误没有属性,或不可调用。(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 
+0

谢谢大家,耐心等待。 (讽刺) 我问别人解决了我自己的问题,然后我继续发布。当然,我还有另一个问题。我要去尝试我的自我。这就是我为什么离开这个问题的原因。另外,我不知道我可以编辑整个帖子,并提出另一个问题。对我来说很伤心,现在我得到了所有这些努力尽我所能。 – user1831680

+0

感谢您的编辑,我已经退出了我的downvote。但是,您仍未发布*所有相关代码*。你为什么一直期待每个人都想浪费时间猜测你写的是什么? – BartoszKP

+0

我不明白你的意思是相关的代码。 我在链表上唯一的另一件事是打印方法。为了我的意图,我只需要插入和打印。 – user1831680

回答

0

你好吗?是吗?我猜你喜欢yourList.insert(1)。在你的代码中:self.head = node,其中node是用户传递给insert的内容。因此,在下一次拨打insert时,您最终会尝试拨打int或您试图将其放入列表中的任何内容。你需要用用户给出任何物体与Node类:

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

但是,请记住张贴所有相关的代码,所以人们试图帮助你将不必去猜测。

编辑:仍然,编辑后案件仍然是一样的。您不包装传递给您的列表的对象,因此您一直试图在非节点对象上调用Node方法...

+0

这个头上总是空的。插入的节点将在头后。 – user1831680

+0

你说得对。这就是我正在努力的。这些常见的错误是这个原因的影响。我不知道如何包装。 – user1831680

+0

你现在做的方式似乎没问题。现在有什么错误信息?什么是'项目'? – BartoszKP

相关问题