I was trying to implementing linked list in Python for pratice , but am stuck at a point. I have been able to code for adding and traversing the list but am facing trouble for removing elemnt from list.
I am trying to remove the element which the user passes in the remove ()
class node (object):
def __init__ (self,data):
self.data = data
self.next = None
class lk (object):
def __init__ (self):
self.current_data = None
self.header = None
def add (self,data):
New_node = node (data)
New_node.next = self.current_data
self.current_data = New_node
self.head = self.current_data
def display (self):
#print 'coming to display', self.current_data
self.current_data = self.head
while (self.current_data is not None):
print self.current_data.data
self.current_data = self.current_data.next
def remove (self,value):
self.value = value
present_node = self.head
self.current_data = self.head
while self.current_data is not None:
if self.head == self.value:
self.head = self.head.next
else:
#k = self.current_data.next
#print k.data
print self.current_data.data
print self.current_data.next
self.current_data = self.current_data.next
k = lk()
k.add (3)
k.add(4)
k.add(5)
k.display()
k.remove(4)
k.display
In the remove method I am trying to access the data of the next node , using self.current.next.data which gives an error , but I am able to access the address of the next link , could someone please try and explain where am I going wrong and how to rectify it.