在链接列表中,我编写了一个drop_between函数,该函数删除指定范围内的节点。
drop_between函数之外的所有内容都可以正常工作。
我的预期输出:
sample = Deque()
sample.push_front(3)
sample.push_front(2)
sample.push_front(1)
sample.push_front(0)
linked list = [0, 1, 2, 3]
sample.drop_between(1,3)
linked list = [0, 3] # 1, 2 removed
但是,我的函数会删除drop_between中指定的所有内容,但传入的第一个数字(即开头)除外。
我的链表功能文件:
class Node:
"""
Initialize empty node
"""
def __init__(self, data, prev = None, next = None):
self.data = data
self.next = next
self.prev = prev
class Deque:
"""
A double-ended queue
"""
def __init__(self):
"""
Initializes an empty Deque
"""
self.head = None
self.tail = None
self.size = 0
def __len__(self):
"""
Computes the number of elements in the Deque
:return: The size of the Deque
"""
return self.size
def push_front(self, e):
"""
Inserts an element at the front of the Deque
:param e: An element to insert
"""
new_head = Node(data = e, next = self.head)
if len(self) == 0:
self.tail = new_head
if self.head:
self.head.prev = new_head
self.head = new_head
self.size += 1
def drop_between(self, start, end): #need help with this function
"""
Deletes elements from the Deque that within the range [start, end)
:param start: indicates the first position of the range
:param end: indicates the last position of the range(does not drop this element)
"""
cur_index = 0
if (start > end):
raise IndexError
if (start < 0):
raise IndexError
curr = self.head
while curr.next:
last_node = curr
curr = curr.next
if ((cur_index >= start) and (cur_index < end)):
print("here", cur_index)
last_node.next = curr.next.next
curr.next.next.prev = last_node.next
cur_index += 1
def listprint(self, node):
"""
Prints each element of the node front to back
:param node:
"""
while (node is not None):
print(node.data)
last = node
node = node.next
我的主文件:
def main():
D = Deque()
D.push_front(9)
D.push_front(8)
D.push_front(7)
D.push_front(6)
D.push_front(5)
D.push_front(4)
D.push_front(3)
D.push_front(2)
D.push_front(1)
D.push_front(0)
D.drop_between(4,7)
D.listprint(D.head)
我的main的输出显示数字0-4和7-9(包括0和7-9),但是4的输出是意外的。如何才能正确取消链接列表中的4个链接?
答案 0 :(得分:1)
请注意,collections
已经有一个implementation of deque
考虑到这一点,您的任务最终将非常简单:
from collections import deque
def drop_between(d, start, end):
d.rotate(-start)
for __ in range(end-start):
d.popleft()
d.rotate(+start)
d = deque(n for n in range(20))
print(d)
drop_between(d, 5, 9)
print(d)
但是,如果不允许使用此外部源,我建议编写类似的逻辑。
答案 1 :(得分:1)
将您的 drop_between 函数替换为:
def drop_between(self, start, end): #need help with this function
"""
Deletes elements from the Deque that within the range [start, end)
:param start: indicates the first position of the range
:param end: indicates the last position of the range(does not drop this element)
"""
cur_index = 0
if (start > end):
raise IndexError
if (start < 0):
raise IndexError
#assumption: start node != head
prev_to_start_node = self.get_node_by_index(start-1)
end_node = self.get_node_by_index(end)
prev_to_start_node.next = end_node
end_node.prev = prev_to_start_node
def get_node_by_index(self,index):
i = 0
curr = self.head
while i < index and curr.next:
curr = curr.next
i+=1
return curr
请注意,我添加了 get_node_by_index ,因此,如果要删除它,则需要相应地替换代码。