双链表python
我使用的是Python 3.0,我必须创建以下内容: 1将名为Setplus的ADT实现为有序双链接列表,其中项目从列表中最小的项目到最大的项目进行排序 首先我创建了一个名为Double_Node的模块 然后我创建了一个名为Setplus的类:双链表python,python,python-3.x,doubly-linked-list,Python,Python 3.x,Doubly Linked List,我使用的是Python 3.0,我必须创建以下内容: 1将名为Setplus的ADT实现为有序双链接列表,其中项目从列表中最小的项目到最大的项目进行排序 首先我创建了一个名为Double_Node的模块 然后我创建了一个名为Setplus的类: class Setplus: """ Field: _head points to the first node in the linked list _tail points to the last node in
class Setplus:
"""
Field: _head points to the first node in the linked list
_tail points to the last node in a linked list
"""
## Setplus() produces a newly constructed empty setplus.
## __init__: -> Setplus
def __init__(self):
self._head = None
self._tail = None
## self.empty() produces True if self is empty.
## empty: Setplus -> Bool
def empty(self):
return self._head == None
## value in self produces True if value is an item in self.
## __contains__: Setplus Any -> Bool
def __contains__(self, value):
current = self._head
while current:
if current.get_value == value:
return True
else:
current = current.get_next
return False
## self.distinct() produces True if all items are distinct.
## distinct: Setplus -> Bool
#def distinct(self):
## count(value) produces the number of occurrences of value.
## count: Setplus Any -> Int
def count(self, value):
counter = 0
current = self._head
while current != None:
if current.value == value:
counter += 1
print (counter)
else:
current = current.next
return counter
## self.add(value) adds value as an item in order.
## Effects: Mutates self.
## add: Setplus Any -> None
def add(self, value):
new_node = Double_Node(value)
if self.head == None:
self.head = new_node
if self.tail != None:
slef.tail.next = new_node
self.tail = new_node
我在创建contains方法count时遇到问题,该方法计算值的数量,add以正确的非减量顺序添加节点
提前感谢如果您想使用现有的Python类来实现这一点,您可能会发现这些类很有用 双链接列表: deque,从“集合”模块: 从最小到最大的有序数据结构: heapq模块,它实现了最小堆
代码中的第一个主要问题是拼写错误和名称不正确 在你的一个函数中有一个明显的输入错误,slef而不是self 还有很多地方,你用两个不同的名字来表示同一个属性,例如,head和head,next和next节点 在Double_节点类中还定义了getter和setter函数,但只有在Setplus中尝试使用它们时,才引用了该方法,而没有调用它。行current=current.get\u next几乎肯定应该是current=current.get\u next 关于getter和setter函数的一个简短转移:Python类中通常不需要它们。直接使用属性即可。如果以后发现需要更奇特的行为,例如验证新设置的值或动态生成请求的值,则可以更改类“使用属性”将属性访问语法转换为方法调用。在其他编程语言中,通常不能以这种方式改变属性访问,因此鼓励使用getter和setter方法,以便从一开始就拥有可扩展的API 请注意,如果您是一名学生,您的讲师可能对Python的熟悉程度不如其他语言,因此他们可能希望您编写getter和setter,尽管它们在Python代码中通常是糟糕的样式。考虑学习如何使用一个属性,而你可能会在以后把他们的思想炸掉! 我将去掉Double_节点中的getter和setter函数,这只是一个风格问题。但是,如果您打算保留它们,可能是因为您的任务需要它们,那么您应该在代码中实际使用它们 最后,为了得到您需要帮助的实际问题,按排序顺序插入到链接列表中,您可能需要执行以下操作:
def add(self, value):
new_node = Double_Node(value)
if self._head == None: # inserting into empty list
self._head = self._tail = new_node
else: # inserting into a list that contains at least one node already
current = self._head
while current and current.value < value: # find a node to insert before
current = current.get_next()
if current: # not inserting at end of list
prev = current.get_prev()
if prev: # not inserting at start
new_node.set_prev(prev)
prev.set_next(new_node)
else: # inserting at start
self._head = new_node
new_node.set_next(current)
current.set_prev(new_node)
else: # inserting at end
new_node.set_prev(self._tail)
self._tail.set_next(new_node)
self._tail = new_node
在按排序顺序进行addinsert之后,其他方法可以利用这一事实。例如,\uuuu contains\uuuu可以在看到大于其正在查找的值时停止搜索,count将在一个连续组中找到所有匹配的值。出现问题意味着什么?我看到您已经实现了一个_contains、count和add方法。他们怎么了?除了错误的计数缩进(这可能是将代码复制到堆栈溢出而不是原始版本中的代码)之外,我没有看到任何明显的错误。如果你真的描述了当你使用你的类时会发生什么,以及你期望会发生什么,那么你的问题会得到很大的改善。我的错误在add中,因为它必须是一个按当前形式排列的链表,它必须在末尾附加,我无法比较这两个元素,并将元素包含在其中。例如:如果我有一个列表1 2 3 6 7,并且我想在当前表单中插入5,它会添加到结尾,使其成为1 2 3 4 6 7 5,而我正在尝试使其成为1 2 3 4 6 7。谢谢这并没有真正回答这个问题。虽然像您提到的库数据结构很有用,并且可能比大多数真实代码中的手写数据结构更好,但这显然是学生学习链表的家庭作业。collections.deque对象或使用heapq构建的堆都不是链表,因此它们根本不相关。请您向我解释一下这一行的意思,简单介绍一下getter和setter函数:Python类中通常不需要它们。直接使用属性就行了。你能解释一下这一行的意思吗?这是关于getter和setter函数的一个简短的转移:Python类中通常不需要它们。直接使用属性即可。那么我该怎么称呼它呢?通常你不需要在Double_Node类中编写get_Whatch和set_Whatch函数,在Setplus中你只需要直接访问下一个_Node和上一个_Node的属性,例如current.prev_Node而不是current.get_prev。该函数无法插入到链接列表的中间,当在中间插入某个元素时,它会添加该元素并删除前面的所有内容,因此我认为当我们指定“prev=current.get_prev”时会出现问题。例如
e、 如果我们要在下面的列表中添加5个1 2 3 4 6 7 8,它将创建一个列表5 6 7 8。啊,你说得对。问题是,我将prev=current.get_prev行移到了current.set_prev new_节点行的下方,而它原来位于上面。这会破坏一切。我已经编辑了我的答案,将当前和新_节点的链接放在上一个链接代码之后,这样它就可以工作了。请注意,Double_Node类中还有一个问题我在这里没有解决,它在set_prev中设置self.next_prev_Node而不是self.prev_Node。
def add(self, value):
new_node = Double_Node(value)
if self._head == None: # inserting into empty list
self._head = self._tail = new_node
else: # inserting into a list that contains at least one node already
current = self._head
while current and current.value < value: # find a node to insert before
current = current.get_next()
if current: # not inserting at end of list
prev = current.get_prev()
if prev: # not inserting at start
new_node.set_prev(prev)
prev.set_next(new_node)
else: # inserting at start
self._head = new_node
new_node.set_next(current)
current.set_prev(new_node)
else: # inserting at end
new_node.set_prev(self._tail)
self._tail.set_next(new_node)
self._tail = new_node