I am learning about LinkedList. I ran this once. But when I am trying to run this code once again It didn't work out. I wonder why. The error shows when I'm trying to call the method "remove" Firstly, a class of linkedlist has been created. And then we add new method to this class. The method "addFirst" is used to insert element at the beginning of the linkedlist. On the other hand, the method "addLast" is used to insert element at the end of the linkedlist. "addAfter" is used to insert element after a target node. Similar to the method "addBefore" While method "remove" is used to remove a node in the linkedlist.
class LinkedList:
def __init__(self):
self.head = None
def __repr__(self):
node = self.head
nodes = []
while node is not None:
nodes.append(node.data)
node = node.next
nodes.append("None")
return " -> ".join(nodes)
def __iter__(self):
node = self.head
while node is not None:
yield node
node = node.next
def add_first(self, node):
node.next = self.head
self.head = node
def add_last(self, node):
if self.head is None:
self.head = node
return
for current_node in self:
pass
current_node.next = node
def add_after(self, target_node_data, new_node):
if self.head is None:
raise Exception("List is empty")
for node in self:
if node.data == target_node_data:
new_node.next = node.next
node.next = new_node
return
raise Exception("Target not found")
def add_before(self, target_node_data, new_node):
if self.head is None:
raise Exception("List is empty")
if self.head.data == target_node_data:
return self.add_first(new_node)
prev_node = self.head
for node in self:
if node.data == target_node_data:
prev_node.next = new_node
new_node.next = node
return
prev_node = node
raise Exception("Target Node not found")
def remove_node(self, target_node_data):
if self.head is None:
raise Exception("List is empty")
if self.head.data == target_node_data:
self.head = self.head.next
return
previous_node = self.head
for node in self:
if node.data == target.node.data:
previous_node.next = node.next
node.next = None
return
previous_node = node
raise Exception("Target Node not found")
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __repr__(self):
return self.data
llist = LinkedList()
first_node = Node("a")
llist.head = first_node
second_node = Node("b")
third_node = Node("c")
first_node.next = second_node
second_node.next = third_node
for node in llist:
print(node)
llist.add_first(Node("b2"))
llist
llist.add_last(Node("b"))
llist
llist.add_after("a", Node("b"))
llist
llist.remove_node("a")
CodePudding user response:
Should be target_node_data
not target.node.data
:
class LinkedList:
def __init__(self):
self.head = None
def __repr__(self):
node = self.head
nodes = []
while node is not None:
nodes.append(node.data)
node = node.next
nodes.append("None")
return " -> ".join(nodes)
def __iter__(self):
node = self.head
while node is not None:
yield node
node = node.next
def add_first(self, node):
node.next = self.head
self.head = node
def add_last(self, node):
if self.head is None:
self.head = node
return
for current_node in self:
pass
current_node.next = node
def add_after(self, target_node_data, new_node):
if self.head is None:
raise Exception("List is empty")
for node in self:
if node.data == target_node_data:
new_node.next = node.next
node.next = new_node
return
raise Exception("Target not found")
def add_before(self, target_node_data, new_node):
if self.head is None:
raise Exception("List is empty")
if self.head.data == target_node_data:
return self.add_first(new_node)
prev_node = self.head
for node in self:
if node.data == target_node_data:
prev_node.next = new_node
new_node.next = node
return
prev_node = node
raise Exception("Target Node not found")
def remove_node(self, target_node_data):
if self.head is None:
raise Exception("List is empty")
if self.head.data == target_node_data:
self.head = self.head.next
return
previous_node = self.head
for node in self:
if node.data == target_node_data:
previous_node.next = node.next
node.next = None
return
previous_node = node
raise Exception("Target Node not found")
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __repr__(self):
return self.data
llist = LinkedList()
first_node = Node("a")
llist.head = first_node
second_node = Node("b")
third_node = Node("c")
first_node.next = second_node
second_node.next = third_node
for node in llist:
print(node)
llist.add_first(Node("b2"))
llist
llist.add_last(Node("b"))
llist
llist.add_after("a", Node("b"))
llist
llist.remove_node("a")