Search code examples
pythonlinked-listcomments

LinkedList data structure explanation in Python


I managed to find python code that defines a LinkedList class and all the defacto methods in it but quite can't figure out what each line of code does...Can someone comment on it explaining what each line does so i can grasp a better understanding of LinkedLists in python?

class Node:#what is the significance of a node
  def __init__(self, data, next):#why these parameters
    self.data = data
    self.next = next
class LinkedList:
    def __init__(self):#what is a head
        self.head = None
    
    def add_at_front(self, data):
        self.head = Node(data, self.head)      

    def add_at_end(self, data):
        if not self.head:#what is it checking 
            self.head = Node(data, None)
            return#what is it returning
        curr = self.head
        while curr.next:
            curr = curr.next
        curr.next = Node(data, None)

    def get_last_node(self):
        n = self.head
        while(n.next != None):
            n = n.next
        return n.data

    def is_empty(self):#i understand this method
        return self.head == None

    def print_list(self):#i also undertsnad this one
        n = self.head
        while n != None:#what is this loop doing
            print(n.data, end = " => ")
            n = n.next
        print()


s = LinkedList()
s.add_at_front(5)
s.add_at_end(8)
s.add_at_front(9)

s.print_list()
print(s.get_last_node())


Solution

  • I suggest you read up Linked Lists first, you don't seem to understand how they are structured completely https://www.javatpoint.com/singly-linked-list

    class Node:#this node class is used to represent a node in the linked list
      def __init__(self, data, next):# data is the data carried by the node, next is a reference to the next node in the list
        self.data = data
        self.next = next
    class LinkedList:
        def __init__(self):#the head is the first node of a linkedlist
            self.head = None
        
        def add_at_front(self, data):
            self.head = Node(data, self.head)      
    
        def add_at_end(self, data):
            if not self.head: #this checks if the list is empty, i.e the head node is None.  
                              #Here the new node is inserted at the head since the rest of the list is empty
                self.head = Node(data, None)
                return# this return is used to exit the function, it returns nothing
            curr = self.head
            while curr.next:
                curr = curr.next
            curr.next = Node(data, None)
    
        def get_last_node(self):
            n = self.head
            while(n.next != None):
                n = n.next
            return n.data
    
        def is_empty(self):#i understand this method
            return self.head == None
    
        def print_list(self):#i also undertsnad this one
            n = self.head
            while n != None: #this checks if we traversed through the whole linked list already
                            # this is what the LL looks like: HEAD->Node->Node->None
                            # if n has reached None, then that means it has successfully visited all the nodes in the list
                print(n.data, end = " => ") 
                n = n.next #this line jumps to the next node of the linked list
            print()
    
    
    s = LinkedList()
    s.add_at_front(5)
    s.add_at_end(8)
    s.add_at_front(9)
    
    s.print_list()
    print(s.get_last_node())