从链表中删除节点
我想创建一个 delete_node 函数,该函数从第一个节点开始删除列表中该位置的节点。到目前为止,这是我的代码:
class node:
def __init__(self):
self.data = None # contains the data
self.next = None # contains the reference to the next node
class linked_list:
def __init__(self):
self.cur_node = None
def add_node(self, data):
new_node = node() # create a new node
new_node.data = data
new_node.next = self.cur_node # link the new node to the 'previous' node.
self.cur_node = new_node # set the current node to the new one.
def list_print(self):
node = ll.cur_node
while node:
print node.data
node = node.next
def delete_node(self,location):
node = ll.cur_node
count = 0
while count != location:
node = node.next
count+=1
delete node
ll = linked_list()
ll.add_node(1)
ll.add_node(2)
ll.add_node(3)
ll.list_print()
I would like to create a delete_node function that deletes the node at the location in the list as a count from the first node. So far this is the code I have:
class node:
def __init__(self):
self.data = None # contains the data
self.next = None # contains the reference to the next node
class linked_list:
def __init__(self):
self.cur_node = None
def add_node(self, data):
new_node = node() # create a new node
new_node.data = data
new_node.next = self.cur_node # link the new node to the 'previous' node.
self.cur_node = new_node # set the current node to the new one.
def list_print(self):
node = ll.cur_node
while node:
print node.data
node = node.next
def delete_node(self,location):
node = ll.cur_node
count = 0
while count != location:
node = node.next
count+=1
delete node
ll = linked_list()
ll.add_node(1)
ll.add_node(2)
ll.add_node(3)
ll.list_print()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您不应该在 Python 中从字面上
删除
节点。如果没有任何东西指向该节点(或者更准确地说,在 Python 中,没有任何东西引用它),无论如何它最终都会被虚拟机销毁。如果
n
是一个节点并且它有一个.next
字段,则:有效地丢弃
n.next
,使.next<
n
的 /code> 字段改为指向n.next.next
。如果n
是要删除的节点之前的节点,则相当于在 Python 中删除它。[PS,最后一段可能有点令人困惑,直到你在纸上勾勒出来 - 然后它应该变得非常清晰]
You shouldn't literally
delete
a node in Python. If nothing points to the node (or more precisely in Python, nothing references it) it will be eventually destroyed by the virtual machine anyway.If
n
is a node and it has a.next
field, then:Effectively discards
n.next
, making the.next
field ofn
point ton.next.next
instead. Ifn
is the node before the one you want to delete, this amounts to deleting it in Python.[P.S. the last paragraph may be a bit confusing until you sketch it on paper - it should then become very clear]
这是一种方法。
您没有真正使用 del 的原因(这让我在这里绊倒,直到我回来再次查看它)是因为它所做的只是删除它所调用的特定引用。它不会删除该对象。在 CPython 中,一旦不再有对象的引用,该对象就会被删除。这里会发生什么,当
运行时,(至少)有两个对该节点的引用:我们要删除的名为
node
的引用和前一个节点的next
属性。由于前一个节点仍在引用它,因此实际对象不会被删除,列表也不会发生任何变化。Here's one way to do it.
The reason that you don't really use
del
(and this tripped me up here until I came back and looked at it again) is that all it does is delete that particular reference that it's called on. It doesn't delete the object. In CPython, an object is deleted as soon as there are no more references to it. What happens here that whenruns, there are (at least) two references to the node: the one named
node
that we are deleting and thenext
attribute of the previous node. Because the previous node is still referencing it, the actual object is not deleted and no change occurs to the list at all.我使用递归函数执行 pop() 函数,因为引用的迭代方式不太好。所以代码如下。我希望这对你有帮助! ;)
I did the pop() function using a recursive function because the iterative way with references it's not so good. So the code is below. I hope this help you! ;)
Python 列表是链接列表。
Python lists are linked lists.
我就是这样做的。
This is how I did it.