我在实现 DoublyLinkedList
类的深拷贝方法时遇到了问题。深拷贝应该返回一个不引用原始 DLL 的新的、原始的双向链表(与浅拷贝不同)。
以下是我目前的代码:
class EmptyCollection(Exception):
pass
class DoublyLinkedList:
class Node:
def __init__(self, data=None, next=None, prev=None):
self.data = data
self.next = next
self.prev = prev
def disconnect(self):
self.data = None
self.next = None
self.prev = None
def __init__(self):
self.header = DoublyLinkedList.Node()
self.trailer = DoublyLinkedList.Node()
self.header.next = self.trailer
self.trailer.prev = self.header
self.size = 0
def __len__(self):
return self.size
def is_empty(self):
return (len(self) == 0)
def first_node(self):
if (self.is_empty()):
raise EmptyCollection("List is empty")
return self.header.next
def last_node(self):
if (self.is_empty()):
raise EmptyCollection("List is empty")
return self.trailer.prev
def add_first(self, elem):
return self.add_after(self.header, elem)
def add_last(self, elem):
return self.add_after(self.trailer.prev, elem)
def add_after(self, node, elem):
prev = node
succ = node.next
new_node = DoublyLinkedList.Node()
new_node.data = elem
new_node.prev = prev
new_node.next = succ
prev.next = new_node
succ.prev = new_node
self.size += 1
return new_node
def add_before(self, node, elem):
return self.add_after(node.prev, elem)
def delete(self, node):
prev = node.prev
succ = node.next
prev.next = succ
succ.prev = prev
self.size -= 1
data = node.data
node.disconnect()
return data
def __iter__(self):
if(self.is_empty()):
return
cursor = self.first_node()
while(cursor is not self.trailer):
yield cursor.data
cursor = cursor.next
def __str__(self):
return '[' + '<-->'.join([str(elem) for elem in self]) + ']'
def __repr__(self):
return str(self)
def deepCopy(lnk_lst):
currenthead = lnk_lst.first_node()
temp = DoublyLinkedList()
while currenthead is not lnk_lst.trailer:
temp.add_last(currenthead.data)
currenthead = currenthead.next
return temp
lnk_lst1 = DoublyLinkedList()
elem1 = DoublyLinkedList()
elem1.add_last(1)
elem1.add_last(2)
lnk_lst1.add_last(elem1)
elem2 = 3
lnk_lst1.add_last(elem2)
lnk_lst2 = deepCopy(lnk_lst1)
e1 = lnk_lst1.first_node()
e1_1 = e1.data.first_node()
e1_1.data = 10
e2 = lnk_lst2.first_node()
e2_1 = e2.data.first_node()
print(e2_1.data) #should print 1
我的深拷贝方法似乎返回了浅拷贝。程序的输出应该是1(因为lnk_lst2
不应该引用lnk_lst1
中的任何元素)。
有人能解释一下我如何修改我的深拷贝方法以生成链表的深拷贝而不是浅拷贝吗?
我不能使用Python的内置深拷贝或浅拷贝。任何帮助将不胜感激。
temp.add_last(currenthead.data)
将从你正在复制的列表中添加相同的对象到副本中。这是一个浅拷贝。通常,deepcopy
函数必须对对象进行递归操作,所以类似于temp.add_last(deepCopy(currenthead.data))
,而你的deepCopy
将必须知道如何处理你期望的对象。 - juanpa.arrivillagadeepCopy
函数将期望任意对象,那么这可能会变得非常复杂。 - juanpa.arrivillagadeepcopy
的实现:https://github.com/python/cpython/blob/master/Lib/copy.py - juanpa.arrivillaga