Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have a problem with syntax in nodes (data Structure)

This code should create a node but I have a problem with it I tried to fix it but I couldn`t

I want to know why there is a problem at in the Linked_List (Next_Node)

that is what show in the error "(Cannot assign member "next_node" for type "node"
Expression of type "node | None" cannot be assigned to member "next_node" of class "node"
Type "node | None" cannot be assigned to type "None"
Type cannot be assigned to type "None")"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class node :
    data = None
    next_node = None

def __init__(self , data) :
    self.data = data

def __repr__(self) :
    return "<node data: %s>" % self.data

class linked_list :

def __init__(self ):
    self.head = None

def add (self , data):
    new_node = node(data)
    new_node.next_node = self.head
    self.head = new_node 
def __repr__ (self):
    nodes =[]
    current = self.head
    while current :
        if current is self.head:
            nodes.append ("[:head %s ]" % current.data)
        elif current.next.node is None :
            nodes.append ("[tail: %s ]" % current.data)
        else :
            nodes.append ("[:%s ]" % current.data)
    current = current.next_node
    return  "->".join(nodes) 

>Solution :

There are several problems with current attempt:

  1. The Node class as pointed by John Gordon is wrongly constructed. The data and next_node should be in __init__ method.
  2. The add method is not adding new node in correct position.
  3. The __repr__ is not looping through all the nodes in the linked list because of wrong indentation.

Updated code:

class node:
    def __init__(self, data):
        self.data = data
        self.next_node = None

    def __repr__(self):
        return "<node data: %s>" % self.data


class linked_list:

    def __init__(self):
        self.head = None

    def is_empty(self):
        return self.head == None

    def size(self):
        current = self.head
        count = 0

        while current:
            count += 1
            current = current.next_node
        return count

    def add(self, data):
        new_node = node(data)
        if self.head == None:
            self.head = new_node
        else:
            current = self.head
            while current.next_node != None:
                current = current.next_node
            current.next_node = new_node

    def __repr__(self):
        nodes = []
        current = self.head
        while current:
            if current is self.head:
                nodes.append("[:head %s ]" % current.data)
            elif current.next_node is None:
                nodes.append("[tail: %s ]" % current.data)
            else:
                nodes.append("[:%s ]" % current.data)
            current = current.next_node
        return "->".join(nodes)


l = linked_list()
l.add(1)
l.add(2)
l.add(3)
print(l)

Output:

[:head 1 ]->[:2 ]->[tail: 3 ]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading