Need Help with Creating a LinkedList in Python



  • 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()
    


  • 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()
    
    


  • 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()
    
    

Log in to reply