[英]Python - Linked List node comparision requires additional dereferencing
I am using Python 3.6.3. 我正在使用Python 3.6.3。 I am trying to write a simple Linked List in Python.
我试图用Python编写一个简单的链表。 Here is my code:
这是我的代码:
class Node(object):
"""Represents a node within a linked list"""
def __init__(self, data, next=None):
self.stuff = data
self.next = next
def __str__(self):
return str(self.stuff)
class LinkedList(object):
def __init__(self):
self.head = None
self.size=0
def append(self, data):
if not self.head:
self.head = Node(data)
return
else:
n=self.head
while n.next:
n = n.next
new_node = Node(data)
n.next = new_node
return
def insertAfter(self, data, newNode):
if not self.head:
return
else:
n=self.head
while n and n.stuff != data:
n = n.next
if not n:
return
else:
newNode.next = n.next
n.next = newNode
return
def printlist(self):
if not self.head:
print("List is empty")
return
else:
n = self.head
while n:
print(str(n))
n = n.next
return
ll = LinkedList()
ll.append(Node("1"))
ll.append(Node("2"))
ll.append(Node("3"))
ll.insertAfter("2", Node("2.5"))
ll.printlist()
I expect it to print: 我希望它能打印:
1
2
2.5
3
But, instead it prints: 但是,它打印:
1
2
3
After debugging, I realized that changing this line in the insertAfter method: 调试后,我意识到在insertAfter方法中更改此行:
while n and n.stuff != data:
to: 至:
while n and n.stuff.stuff != data:
prints the expected output. 打印预期的输出。 I don't understand why it is doing this.
我不明白为什么要这么做。 Please help.
请帮忙。 Thank you
谢谢
The problem is you are not adding 1, 2, 3 to the LinkedList
and letting it create a Node
to wrap each. 问题是您没有在
LinkedList
添加1、2、3,而是让它创建了一个用于包装每个Node
。 You are adding a Node
whose stuff
variable is 1, 2, and 3. When you call the append method to add each Node
, they get wrapped in another Node
by the append
method. 您将添加一个
stuff
变量为1、2和3的Node
。当您调用append方法添加每个Node
,它们会被append
方法包裹在另一个Node
。 Therefore you need to call Node.stuff.stuff
to access the actual element that is stored. 因此,您需要调用
Node.stuff.stuff
来访问存储的实际元素。
Look at what your append
method is actually doing. 查看您的
append
方法实际上在做什么。 It accepts some parameter data
and then creates a Node
with either the line self.head = Node(data)
or the line new_node = Node(data)
, depending on if the LinkedList
already has a head Node
or not. 它接受一些参数
data
,然后创建一个Node
与任一行self.head = Node(data)
或线路new_node = Node(data)
,这取决于如果LinkedList
已经具有头部Node
或没有。
Change ll.append(Node("1"))
to just ll.append("1")
. 将
ll.append(Node("1"))
更改为ll.append("1")
。 Alternatively, change your append method to assume it is being passed a Node object. 或者,更改您的append方法以假定它正在传递给Node对象。 The first solution is much more preferable as the
Node
class has little use outside the context of the LinkedList
class and the LinkedList
doesn't work unless it is populated with Nodes
anyways; 第一种解决方案更为可取,因为
Node
类在LinkedList
类的上下文之外几乎没有用处,并且除非始终用Nodes
填充,否则LinkedList
不起作用。 it seems like extra work to make the user of the LinkedList
class have to create a Node
every single time. 使
LinkedList
类的用户必须每次创建一个Node
似乎是一项额外的工作。
EDIT: Also what is the reason you are passing in numbers as strings? 编辑:也是您将数字作为字符串传递的原因是什么? You don't need to put 1, 2, 2.5, and 3 in quotes unless you specifically want them to be strings- but if so, why?
除非您特别希望它们是字符串,否则不需要在引号中加上1、2、2.5和3,但是如果是这样,为什么呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.