Navigation

    ask avan logo
    • Register
    • Login
    • Search
    • Categories
    • Unsolved
    • Solved

    Need Help with Creating a LinkedList in Python

    Python
    2
    2
    35
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      pranjal last edited by avan

      class Node:
           def _init_(self,data):
                self.data=data
                self.next=None
      
      class LinkedList:
           def _init_(self):
          self.head=None
        def insert(self,newnode):
          if self.head is None:
            self.head=newnode
          else:
            temp=self.head
            while temp.next!=None:
              temp=temp.next
            temp.next=newnode
        def printlist(self):
          temp=self.head
          if temp==None:
            print("empty list") 
            return 0  
      
          while temp.next!=None:
            print(temp.data)
            temp=temp.next
      
      firstnode=Node("Sita")      
      linkedlist=LinkedList()
      linkedlist.insert("john")
      secondnode=Node("Ben")
      linkedlist.insert(secondnode)
      thirdnode=Node("Matthew")
      linkedlist.insert(thirdnode)
      linkedlist.printlist()
      
      Reply Quote 0
        1 Reply Last reply

      • avan
        avan last edited by avan

        Where to start...

        I am not a python guy but here is what I have noticed.

        Constructor

        Add extra underscores (one before and one after) to both constructors
        So it should look like this:

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

        Adding a Node instead a String

        So replace

        linkedlist.insert("john")
        

        with

        linkedlist.insert(firstnode)
        

        Print Last Element

        Don't forget to print the last element once you are done iterating over the linkedlist

        So add the following line to the end of the while loop

        print(temp.data)
        

        Final Code for a python LinkedList

        class Node:
          def __init__(self,data):
            self.data=data
            self.next=None
        
        class LinkedList:
          def __init__(self):
            self.head=None
          def insert(self,newnode):
            if self.head is None:
              self.head=newnode
            else:
              temp=self.head
              while temp.next!=None:
                temp=temp.next
              temp.next=newnode
          def printlist(self):
            temp=self.head
            if temp==None:
              print("empty list") 
              return 0  
            while temp.next!=None:
              print(temp.data)
              temp=temp.next
            print(temp.data)
        
        firstnode=Node("Sita")      
        linkedlist=LinkedList()
        linkedlist.insert(firstnode)
        secondnode=Node("Ben")
        linkedlist.insert(secondnode)
        thirdnode=Node("Matthew")
        linkedlist.insert(thirdnode)
        linkedlist.printlist()
        
        
        Reply Quote 0
          1 Reply Last reply

        • avan
          avan last edited by avan

          Where to start...

          I am not a python guy but here is what I have noticed.

          Constructor

          Add extra underscores (one before and one after) to both constructors
          So it should look like this:

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

          Adding a Node instead a String

          So replace

          linkedlist.insert("john")
          

          with

          linkedlist.insert(firstnode)
          

          Print Last Element

          Don't forget to print the last element once you are done iterating over the linkedlist

          So add the following line to the end of the while loop

          print(temp.data)
          

          Final Code for a python LinkedList

          class Node:
            def __init__(self,data):
              self.data=data
              self.next=None
          
          class LinkedList:
            def __init__(self):
              self.head=None
            def insert(self,newnode):
              if self.head is None:
                self.head=newnode
              else:
                temp=self.head
                while temp.next!=None:
                  temp=temp.next
                temp.next=newnode
            def printlist(self):
              temp=self.head
              if temp==None:
                print("empty list") 
                return 0  
              while temp.next!=None:
                print(temp.data)
                temp=temp.next
              print(temp.data)
          
          firstnode=Node("Sita")      
          linkedlist=LinkedList()
          linkedlist.insert(firstnode)
          secondnode=Node("Ben")
          linkedlist.insert(secondnode)
          thirdnode=Node("Matthew")
          linkedlist.insert(thirdnode)
          linkedlist.printlist()
          
          
          Reply Quote 0
            1 Reply Last reply

          • First post
            Last post