Question

So i solved a couple of problems already, by getting help here, and from people i know. the root of my problems is that i don't know how to wrap None so that i don't keep getting these errors of not having the attribute, or not callable.

For this linked list, all i really need is insert and printlist. I didn't include print list because it is simple, and is not causing problems.

The error is under Linked_List, under insert, under the elif. It's commented so: #<----ERROR

Here is the code:

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

Insertion:

lList.insert(string)

Solved Here:

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
Was it helpful?

Solution

How do you use it? I'm guessing that you do something like yourList.insert(1). In your code you do: self.head = node, where node is what user have passed to insert. So, on the next call to insert you end up trying to call an int or anything you've tried to put into the list. You need to wrap any objects given by the user with your Node class:

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

However, please remember to post all of the relevant code, so the people trying to help you won't have to guess.

EDIT: still, after the edit the case remains the same. You don't wrap objects passed to your list, so you keep trying to call Node methods on non-Node objects...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top