简体   繁体   中英

Implementing Linked List in Python

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.

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()

You can check link . i hope you will get your answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM