Hi guys. Below is my code:
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
class LinkedList:
def __init__(self,value=None):
self.head_node = Node(value)
def __repr__(self):
current_node = self.head_node
stringify = '\n'
while current_node:
stringify += str(current_node.get_value())+(', ' if current_node.get_next_node()!= None else '')
current_node = current_node.get_next_node()
return stringify+'\nLength: '+str(len(self))+'\n'
def __len__(self):
counter = 0
current_node = self.head_node
while current_node:
counter+=1
current_node = current_node.get_next_node()
return counter
def get_head_node(self):
return self.head_node
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 insert_at_end(self,new_value):
#I want to get the node at the end of the list
current_node = self.head_node
new_node = Node(new_value)
while current_node.get_next_node() != None:
current_node = current_node.get_next_node()
current_node.set_next_node(new_node)
def insert_in_middle(self,new_value):
#Only work if Length of LinkedList is at least 2
if len(self)<2:
print ("LinkedList not long enough for this method. Please ensure at least 2 nodes are present")
else:
#Divide Length by 2, get int, go to that space and insert
middle = int(len(self)/2)-1
current_node = self.head_node
for traverse in range(middle):
current_node = current_node.get_next_node()
new_node = Node(new_value)
new_node.set_next_node(current_node.get_next_node())
current_node.set_next_node(new_node)
def remove_node(self,value_to_remove):
current_node = self.head_node
next_node = current_node.get_next_node()
if current_node.get_value() == value_to_remove:
#If it's the head node, all you need to to is to replace the head node with the next node
self.head_node = next_node
else:
while current_node:
next_node = current_node.get_next_node()
next_next_node = next_node.get_next_node()
if next_node.get_value() == value_to_remove:
#If it's not the head node, then the NEXT node's pointer needs to become the CURRENT node's pointer
#The NEXT node falls away automatically because of Garbage Collection
current_node.set_next_node(next_next_node)
current_node = None
def remove_nodes(self,value_to_remove):
#I would think this just iterates the remove_node method. But first we find how many instances of the value to remove there is
times_to_remove = 0
current_node = self.head_node
for i in range(len(self)-1):
times_to_remove+=1 if current_node.get_value() == value_to_remove else 0
current_node = current_node.get_next_node()
#Now Loop the remove_node method:
for p in range(times_to_remove):
self.remove_node(value_to_remove)
#print(times_to_remove)
So I tried to create the remove_nodes function, but in the last loop, my terminal just doesn’t process it. Not sure where I’m going wrong?