1

I was following a tutorial about data structures and algorithms and the instructor in the course was using java script. The instructor implemented a Linked List in java script using a object/hash table/hash map. So i tried to do the same implementation in python and this is the code that i wrote.

class LinkedList():
def __init__(self,value):
    self.head = {"value":value , "next": None}
    self.tail = self.head
    self.length = 1
    
def append(self,value):
    new_node = {"value":value , "next":None}
    self.tail["next"] = new_node
    self.tail = new_node
    self.length += 1
    
def prepend(self,value):
    new_node = {"value":value , "next":self.head}
    self.head = new_node
    self.length += 1
    
def insert(self,index,value):
    if index > self.length:
        return "The index is greater than the Linked List"
    elif index == 0:
        self.prepend(value)
    elif index == self.length:
        self.append(value)
    else:
        prev_node = self.head
        new_node = {"value":value , "next":None}
        for i in range(0,self.length):
            if i == index-1:
                new_node["next"] = prev_node["next"]
                prev_node["next"] = new_node
                self.length += 1
                break
            else:
                prev_node = prev_node["next"]
                
def delete(self,index):
    current_node = self.head
    if index >= self.length:
        return "There is no such index in the list"
    elif index == 0:
        self.head = self.head["next"]
        self.length += -1
    else:
        for i in range(0,self.length-1):
            if i == index-1:
                unwanted_node = current_node["next"]
                current_node["next"] = unwanted_node["next"]
                if index == self.length-1:
                    self.tail = current_node
                self.length += -1
                break
            else:
                current_node = current_node["next"]
                
                
def reverse(self):
    current_node = self.head
    new_head = LinkedList(current_node["value"])
    while current_node["next"] != None:
        current_node = current_node["next"]
        new_head.prepend(current_node["value"])
    self.head = new_head.head
    self.tail = new_head.tail
   

But when i tried searching on the internet there was different implementation of linked list which doesn't consist of dictionaries. I just wanted to know that is the code that i have written is a successful implementation of Linked List in python or not

  • I would suggest you write some unit test code to try it out and see! You could write a handful of tests for basic operations and convince yourself that it works. – AirSquid Jun 22 '20 at 14:21
  • Actually the thing is the code works perfectly as a linked list is expected to work . its just that on the internet the code for implementing Linked List is different – Bilal Ahmed Jun 22 '20 at 14:29
  • 2
    There are MANY ways to implement linked list, as with many other data structures. Some are more efficient in speed or memory usage than others. As long as yours passes functional tests, you're good. There are other sites that offer code review etc. if you want suggestions. Somebody may pop one on here. – AirSquid Jun 22 '20 at 14:35
  • My personal opinion is that dictionaries are for dynamically storing key/value pairs with no structure to follow, while classes (or named tuples) are for objects which always have the same set of key/value pairs. I would prefer the latter for the implementation of linked-list nodes, and I prefer writing `.value` over `["value"]`. However, dictionaries are somewhat faster. What you prefer is a matter of opinion. See also [Should I use a class or dictionary?](https://stackoverflow.com/q/4045161/5459839). – trincot Jun 22 '20 at 17:48

0 Answers0