hbrovell
hbrovell

Reputation: 547

'Node' object has no attribute 'set_next'

When I insert a new node I get AttributeError: 'Node' object has no attribute 'set_next'. I can't really understand why, because in my Node class I have a method set_next. And isn't that the one I'm calling?

class Node(object):
    def __init__(self, val):
        self.val = val
        self.next = None

        def get_data(self):
            return self.val

        def set_data(self, val):
            self.val = val

        def get_next(self):
            return self.next

        def set_next(self, next):
            self.next = next

class LinkedList(object):
    def __init__(self, head=None):
        self.head = head
        self.count = 0 

    def get_count(self):
        return self.count

    def insert(self, data):
        new_node = Node(data)
        new_node.set_next()
        self.head = new_node
        self.count += 1

The expected output is that the new node should be the new head node.

Upvotes: 0

Views: 1020

Answers (3)

FHTMitchell
FHTMitchell

Reputation: 12156

This will fix the AttributeError and the subsequent TypeError.

class Node(object):
    def __init__(self, val):
        self.val = val
        self.next = None

    # fixed indentation here
    def get_data(self):
        return self.val

    def set_data(self, val):
        self.val = val

    def get_next(self):
        return self.next

    def set_next(self, next):
        self.next = next

class LinkedList(object):
    def __init__(self, head=None):
        self.head = head
        self.count = 0 

    def get_count(self):
        return self.count

    def insert(self, data):
        new_node = Node(data)
        # fix logic here
        new_node.set_next(self.head)
        self.head = new_node
        self.count += 1

Testing

linked_list = LinkedList()
linked_list.insert('hello')
linked_list.insert('world')
print(linked_list.count)
print(linked_list.head.val)
print(linked_list.head.next.val)

outputs

2
world
hello

Note that, as you can see, this LinkedList inserts only at the front of the list, not the end.


Bonus

If you want to iterate over the list, use this method

def __iter__(self):
   node = self.head
   while node is not None:
       yield node.val
       node = node.next

Upvotes: 2

I tried this and hope this helps you:

class Node(object):
    def __init__(self, val):
        self.val = val
        self.next = None
    def get_data(self):
        return self.val
    def set_data(self, val):
        self.val = val
    def get_next(self):
        return self.next
    def set_next(self, next):
        self.next = next

class LinkedList(object):
    def __init__(self, head=None):
        self.head = head
        self.count = 0 
    def get_count(self):
        return self.count
    def insert(self, data):
        new_node = Node(data)
        new_node.set_next(self.head)
        self.head = new_node
        self.count += 1
>>> itemList = LinkedList()
>>> itemList.insert(38)
>>> itemList.insert(40)
>>> itemList.get_count()
2

Upvotes: 1

doctorlove
doctorlove

Reputation: 19317

With your indentation as it stands,

class Node(object):
    def __init__(self, val):
        self.val = val
        self.next = None

        def get_data(self):
            return self.val

get_data and the following functions, including set_next are local to the __init__ method.

So, as the error says, the "Nodeclass does not have aset_next` method.

You need to pull them back:

class Node(object):
    def __init__(self, val):
        self.val = val
        self.next = None

    def get_data(self):
        return self.val

    #... and the rest

This will give your further problems, but fix your initial problem.

Next, you will see

File "linked.py", line 28, in insert
  new_node.set_next()
  TypeError: set_next() missing 1 required positional argument: 'next'

As was said in the comments, you need to pass this a value. I suspect you are trying to set next on the head or final node to this new_node.

Upvotes: 1

Related Questions