Creating and Printing Single Linked List

After trying to call the following code to my terminal I get the following AttributeError:
‘Function’ object has no attribute ‘get_value’

I made sure the get_value method was in the Node class. I also made sure my Node class had been instantiated inside my LinkedList class. I’m stumped.

class Node:
    def __init__(self, value, next_node=None):
        self.value = value
        self.next_node = next_node

    def get_value(self):
        return self.value

    def get_next_node(self):
        return self.get_next_node

    def set_next_node(self, next_node):
        self.next_node = next_node

class LinkedList:
    def __init__(self, value=None):
        self.head_node = Node(value)

    def get_head_node(self):
        return self.head_node

    def insert_beginning(self, new_value):
        new_node = Node(new_value)
        new_node.set_next_node(self.head_node)
        self.head_node = new_node

    def print_list(self):
        string_list = ""
        current_node = self.get_head_node()
        while current_node:
            if current_node.get_value() != None:
                string_list += str(current_node.get_value()) + "\n"
            current_node = current_node.get_next_node()
        return string_list

test = LinkedList()
test.insert_beginning(20)
test.insert_beginning(40)
print(test.print_list())

EDIT

Actual error message…

Traceback (most recent call last):
  File "main.py", line 39, in <module>
    print(test.print_list())
  File "main.py", line 31, in print_list
    if current_node.get_value() != None:
AttributeError: 'function' object has no attribute 'get_value'

Please post a link to the exercise page, thanks.

Edit

Found it…

Linked Lists

4 Likes

Try,

def get_next_node(self): 
    return self.next_node
4 Likes