Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

你好,关于循环双向链表的remove方法的一些疑问 #24

Closed
xiaopingzhong opened this issue Jul 15, 2019 · 2 comments
Closed

Comments

@xiaopingzhong
Copy link

我试了下,remove方法,发现只是长度上的改变,
链表的值没有改变(即使我使用del,删除对应的节点).
我修改的删除代码如下:

    def remove(self, value_node: Node):
        if self.lenght == 0:
            # raise Exception("is empty")
            return
        # 后一个节点连接前一个节点
        value_node.next.previous = value_node.previous
        # 前一个节点连接后一个节点
        value_node.previous = value_node.next
        del value_node
        # 记住还要记得删除节点
        self.lenght -= 1
        return 1

测试代码为:

    # 添加元素
    cycle_double_link_list.append(0)
    cycle_double_link_list.append(1)
    cycle_double_link_list.append(2)
    # 坐标添加元素
    cycle_double_link_list.appendLeft(4)

    # 长度判断
    assert len(cycle_double_link_list) == 4

    cycle_double_link_list.remove(value_node=head_node)
    assert len(cycle_double_link_list) == 3
    #下面这句报错,之前没错
    # assert list(cycle_double_link_list) == [0,1,2]

如果要实现列表的实时更新,该怎么做.谢谢.

@xiaopingzhong
Copy link
Author

list(cycle_double_link_list)的结果还是[4,0,1,2]

@PegasusWang
Copy link
Owner

 # 前一个节点连接后一个节点
        value_node.previous = value_node.next

这里不对,应该是 value_node.previous.next = value_node.next ,应该是让前一个节点指向value_node的后一个节点,少了 next。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants