1

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.

0

1 Answer 1

1

Few issues I can spot in your code -

  1. This line - self.value = value - in remove() method, why? Why is it there? you do not need to add the value to delete to the linked list as an instance variable, and you should not. Just access it as value completely, inside the function.

  2. Secondly , why do you keep changing self.current_data in all of your functions? You do not need to change it in display() or remove() , you should define a local variable , like current_node` instead and use that.

  3. Thirdly , your logic for removal is wrong, currently when you find the node, you are just changing the head to point to current_node , that is in no way what you want. You want to loop till you find that the next data contains the data you are looking for and then change current's next to point to its next.

Fixed code -

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
        current_node = self.head
        while (current_node is not None):
            print(current_node.data)
            current_node = current_node.next
    def remove (self,value):
        current_node = self.head
        if current_node.data == value:
            self.head = current_node.next
            return
        while current_node.next is not None:
            if current_node.next.data == value:
                current_node.next = current_node.next.next
                break

            current_node = current_node.next

k = lk()
k.add (3)
k.add(4)
k.add(5)
k.display()
k.remove(4)
print("Hmm")
k.display()
Sign up to request clarification or add additional context in comments.

5 Comments

Yes correct, thanks fixed that in the latest update.
Thanks for the observations , I am a newbie to Python , so these observations would help.
for removeal you have used in your code "if current_node.next.data == value" to check if the value sent for removal exists iin the next node., I was attempting the same thing with "self.current_data.next.data" but I got "Null type doesnt have data attribute" error , I can't seem to understand why.Could you help with that
At present I am at office , so don't have access to code, the commented part of #k = self.current_data.next , was to check for this error only. #print k.data
Mayb the while condition "self.current_data is not None" could have caused that error , I am not sure though

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.