Python中双链表的深层副本

时间:2017-11-02 19:28:18

标签: python algorithm linked-list doubly-linked-list deep-copy

我在为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。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

要执行深层复制,您需要复制嵌入的链接列表:

def deepCopy(lnk_lst):
    currenthead = lnk_lst.first_node()
    temp = DoublyLinkedList()
    while currenthead is not lnk_lst.trailer:
        if isinstance(currenthead.data, DoublyLinkedList):
            temp.add_last(deepCopy(currenthead.data))
        else:
            temp.add_last(currenthead.data)
        currenthead = currenthead.next
    return temp

答案 1 :(得分:1)

可以使用type(obj)(obj)复制许多基本对象。例如。 dict(dct)list(lst)会创建副本。不可变类型将返回相同的对象,这很好。 int(42)为42,str("string")"string",等等。

以下解决方案仅限于此类型。

因此,让我们使用它,让我们将DoublyLinkedList添加到创建副本的类型集合中(在我们的示例中为深层副本,但仅在嵌套的第一级)以这种方式。已修改__init__

def __init__(self, iterable=()):
    self.header = DoublyLinkedList.Node()
    self.trailer = DoublyLinkedList.Node()
    self.header.next = self.trailer
    self.trailer.prev = self.header
    self.size = 0 
    for item in iterable:
        self.add_last(type(item)(item))

现在我们不再需要deepCopy()了。剩下的唯一改变是替换:

lnk_lst2 = deepCopy(lnk_lst1)

由:

lnk_lst2 = DoublyLinkedList(lnk_lst1)