Linked List Implementation
Implementation
- In this tutorial, we are going to learn how to implement a linked list and it's operations implementation.
- First, we implemented a node class to hold values and links to other nodes.
- Then we implemented a linked list class to handle external operations on the list like adding and removing nodes.
Python Code:
# We'll be using our Node class
class Node:
def __init__(self, value, next_node=None):
self.value = value
self.next_node = next_node
def get_value(self):
return self.value
def get_next_node(self):
return self.next_node
def set_next_node(self, next_node):
self.next_node = next_node
# Our LinkedList class
class LinkedList:
def __init__(self, value=None):
self.head_node = Node(value)
def get_head_node(self):
return self.head_node
# Add your insert_beginning and stringify_list methods below:
def insert_beginning(self, new_value):
new_node = Node(new_value)
new_node.set_next_node(self.head_node)
self.head_node = new_node
def stringify_list(self):
string_list = ""
curr = self.head_node
while curr:
if curr.get_value() != None:
string_list += str(curr.get_value()) + "\n"
curr = curr.get_next_node()
return string_list
def remove_node(self, value_to_remove):
current_node = self.head_node
if self.head_node.get_value() == value_to_remove:
self.head_node = current_node.get_next_node()
else:
while current_node:
current_next_node = current_node.get_next_node()
if current_next_node.get_value() == value_to_remove:
current_node.set_next_node(current_next_node.get_next_node())
current_node = None
else:
current_node = current_node.get_next_node()
ll = LinkedList(5)
ll.insert_beginning(70)
ll.insert_beginning(5675)
ll.insert_beginning(90)
print(ll.stringify_list())
Output:
90
5675
70
5
Let's understand the code in detail and what it does:
- Here, we have node class first to set the basic blocks in making the linked list.
- In the constructor of our linked list, we initialized the head node.
- There are many methods like get_head_node(), inset_beginning() to insert a new node at the beginning, and stringify_list() method to traverse the list and print its value, we also have a function named remove_node() to remove a node from the front, back or somewhere in the middle of the linked list.