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

error in creating a linked list using python

I am new to data structures.Tried to create a linkled list with a print function,but when running the program it is throwing an error stating "add_link takes one positional argument but 2 were given".Below is the code.Please help me out. thanks in advance

class node:
    def __init__( self ,value, Next = None):
        self.value = value
        self.Next = None
        newnode = None
    
    def add_link(self,data):
         if(self.Next == None):
              self.Next = node(data)
              newnode = self.Next
         else:
             newnode.Next = node(data)
             newnode = newnode.Next
    
    def print(self):
        if(self.Next !=None):
            print(self.node)
            self.next.print()

 # main         
link = node(10)

link.add_link(20)

link.add_link(30)

link.add_link(40)

link.print()

>Solution :

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

You need to add self as an argument to the add_link() function:

def add_link(self, data):
     if(self.Next == None):
          self.Next = node(data)
          newnode = self.Next
     else:
         newnode.Next = node(data)
         newnode = newnode.Next
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