问题描述
我试图在 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.
我正在尝试删除用户在 remove() 中传递的元素
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
在 remove 方法中,我尝试使用 self.current.next.data 访问下一个节点的数据,这会出现错误,但我能够访问下一个链接的地址,请有人尝试并解释我哪里出错了以及如何纠正它.
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 -
这一行 -
self.value = value
- 在remove()
方法中,为什么?为什么会在那里?您不需要将要删除的值作为实例变量添加到链表中,您也不应该这样做.只需在函数内部完全以value
的形式访问它.
This line -
self.value = value
- inremove()
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 asvalue
completely, inside the function.
其次,为什么在所有函数中都不断更改 self.current_data
?您不需要在 display()
或 remove()
中更改它,您应该定义一个局部变量,例如 current_node` 并使用它.
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.
第三,你的删除逻辑是错误的,目前当你找到节点时,你只是将 head
更改为指向 current_node
,即在没办法你想要什么.您想循环直到发现下一个数据包含您要查找的数据,然后将当前的 next 更改为指向它的 next.
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.
固定代码 -
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()
这篇关于在 Python 中实现链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!