在Python中实现链接列表

时间:2015-08-11 05:50:06

标签: python linked-list

我试图在Python中实现链接列表以实现实践,但我陷入了困境。我已经能够编写添加和遍历列表的代码,但是从列表中删除elemnt会遇到麻烦。

我正在尝试删除用户在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

在remove方法中,我试图访问下一个节点的数据,使用self.current.next.data给出错误,但我能够访问下一个链接的地址,有人可以尝试和解释我哪里出错了以及如何纠正它。

2 个答案:

答案 0 :(得分:1)

我可以在您的代码中发现几个问题 -

  1. 这一行 - self.value = value - 在remove()方法中,为什么?为什么会这样?您不需要将要删除的值作为实例变量添加到链接列表中,您不应该这样做。只需在函数内完全访问它value

  2. 其次,为什么要在所有功能中不断更改self.current_data?您无需在display()remove()中更改它,您应该定义一个局部变量,例如current_node`并使用它。

  3. 第三,您的删除逻辑是错误的,目前当您找到该节点时,您只是将head更改为指向current_node,这绝不是您想要的。您希望循环,直到您发现下一个数据包含您要查找的数据,然后将当前的数据更改为指向下一个数据。

  4. 固定代码 -

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

答案 1 :(得分:0)

您可以查看link。我希望你能得到答案。