正在筛选DLL中的节点



以下是我迄今为止所做的,我想创建一个函数,该函数将删除双链表中低于指定值或高于指定值的元素。

class DoublyLinkedList:
class Node:
""" Nodes in list """
def __init__(self, element, prev=None, next_node=None):
self.element = element
self.prev = prev
self.next = next_node
def __init__(self):
self._header = self.Node(None, None, None)
self._trailer = self.Node(None, None, None)
self._header.next = self._trailer
self._trailer.prev = self._header
self._size = 0
def append(self, element):
:arg element: the value to add to this list

new_node = self.Node(element, self._trailer.prev, self._trailer)
self._trailer.prev.next = new_node
self._trailer.prev = new_node
self._size += 1
def __len__(self):
return self._size
def __str__(self):
# Python's list is used here to avoid string concatenation
result = []
current = self._header.next
for i in range(len(self)):
result.append(current.element)
current = current.next
return str(result)
def remove(self, low, high):
for element in self:
try:

if element.next < low:
element.next = element.next.next

elif element.next > high:
element.next = element.next.next

except Exception as e:
print('low must be less than or equal to high')

pass  

^^这就是我迄今为止所尝试的^^以下是我希望它工作的方式:我不知道如何让它过滤掉较高或较低的值

DoublyLinkedList.append(5)
DoublyLinkedList.append(7)
DoublyLinkedList.append(8)
DoublyLinkedList.append(3)
DoublyLinkedList.append(9)
[5, 7, 8, 3, 9]

DoublyLinkedList.remove(5,8)
its output should be:
[5, 7, 8]

代码中的一些问题:

  • append有一行应该是注释(从#开始(
  • for element in self:将不起作用,因为self是不可迭代的。此外,当您计划从同一集合中删除项目时,使用for循环迭代该集合也不是一个好主意。最好使用已经在__str__方法中使用过的迭代形式
  • element.next < low比较了两种不同的类型:.next是节点,而low是数字。调用那个循环变量element是令人困惑的,因为它也是节点属性的名称。你想要current.next.element < low这样的东西
  • 如果上述情况得到纠正,那么try块就没有理由触发异常,假设它与lowhigh如何相互关联有关。如果您想在high > low时输出消息,那么不要在循环中这样做,只需在开始循环之前测试该条件即可
  • 删除一个节点时,还应减小列表大小
  • 通过使用一个if与一个表达式中的lowhigh进行比较,可以避免代码重复
  • DoublyLinkedList是类,但列表应该是该类的实例。因此,您需要首先创建该实例,然后调用该实例上的方法,而不是类上的方法

以下是对您的代码的更正:

class DoublyLinkedList:
class Node:
""" Nodes in list """
def __init__(self, element, prev=None, next_node=None):
self.element = element
self.prev = prev
self.next = next_node
def __init__(self):
self._header = self.Node(None, None, None)
self._trailer = self.Node(None, None, None)
self._header.next = self._trailer
self._trailer.prev = self._header
self._size = 0
def append(self, element):
# :arg element: the value to add to this list
new_node = self.Node(element, self._trailer.prev, self._trailer)
self._trailer.prev.next = new_node
self._trailer.prev = new_node
self._size += 1
def __len__(self):
return self._size
def __str__(self):
result = []
current = self._header.next
for i in range(len(self)):
result.append(current.element)
current = current.next
return str(result)
def remove(self, low, high):
# Perform the check before the loop
if low > high:
print('low must be less than or equal to high')
return
# Iterate the nodes like in __str__, but start one node earlier
current = self._header
for i in range(len(self)):
# Don't compare the node, but its element.
# Use chained comparison;
if low <= current.next.element <= high:
# Only move to next node when not removing
current = current.next
else:
current.next = current.next.next
self._size -= 1  # Also reduce size
# Should create an instance and work with that
lst = DoublyLinkedList()
lst.append(5)
lst.append(7)
lst.append(8)
lst.append(3)
lst.append(9)
print(lst)  # [5, 7, 8, 3, 9]
lst.remove(5,8)
print(lst)  # [5, 7, 8]

相关内容

  • 没有找到相关文章

最新更新