Search code examples
pythondoubly-linked-list

Passing values between two doubly linked lists in python


I would like to pass a value of my doubly linked list into a new, empty doubly linked list so that I can work on sorting and removing nodes in DLL without changing my core one.

Here's how it looks like:

dllToBeSorted = DoublyLinkedList()
dllToBeSorted = newDoublyLinkedList      #contents of newDoublyLinkedList here: 0 3 0 4 1 1 2 3
dllToBeSorted.sortList()
dllToBeSorted.removeDuplicates()
dllToBeSorted.display()                  #contents of dllToBeSorted here: 0 1 2 3 4 
newDoublyLinkedList.display()            #contents of newDoublyLinkedList here: 0 1 2 3 4 

But when I try to display values, both of DLLs are the same. I don't know why this is happening, because this method works on usual variables (strings, ints and whatnot) but here for some reason it changes both of my DLLs.

I'm putting below my whole executable code:

class Node:
    def __init__(self, data):
        self.item = data
        self.next = None
        self.prev = None

class DoublyLinkedList:
    def __init__(self):
        self.head = None
        self.start_node = None

    def insertToEmptyList(self, data):
        if self.start_node is None:
            new_node = Node(data)
            self.start_node = new_node

    def insertToEnd(self, data):
        if self.start_node is None:
            new_node = Node(data)
            self.start_node = new_node
            return
        n = self.start_node
        while n.next is not None:
            n = n.next
        new_node = Node(data)
        n.next = new_node
        new_node.prev = n

    def display(self):
        if self.start_node is None:
            print(" ")
            return
        else:
            n = self.start_node
            while n is not None:
                print(n.item, end=" ")
                n = n.next
            print("\n")

    def searchNode(self, data):
        if self.start_node is None:
            print("0")
            return
        else:
            n = self.start_node
            counter = 0
            while n is not None:
                if n.item == data:
                    counter += 1
                n = n.next
            print(counter)

    def sortList(self):
        if self.start_node is not None:
            n = self.start_node
            while n.next is not None:
                index = n.next
                while index is not None:
                    if n.item > index.item:
                        temp = n.item
                        n.item = index.item
                        index.item = temp
                    index = index.next
                n = n.next

    def removeDuplicates(self):
        if self.start_node is not None:
            n = self.start_node
            while n is not None:
                index = n.next
                while index is not None:
                    if n.item == index.item:
                        temp = index
                        index.prev.next = index.next
                        if index.next is not None:
                            index.next.prev = index.prev
                        temp = None
                    index = index.next
                n = n.next

newDoublyLinkedList = DoublyLinkedList()
newDoublyLinkedList.insertToEnd(0)
newDoublyLinkedList.insertToEnd(3)
newDoublyLinkedList.insertToEnd(0)
newDoublyLinkedList.insertToEnd(4)
newDoublyLinkedList.insertToEnd(1)
newDoublyLinkedList.insertToEnd(1)
newDoublyLinkedList.insertToEnd(2)
newDoublyLinkedList.insertToEnd(3)
    
dllToBeSorted = DoublyLinkedList()
dllToBeSorted = newDoublyLinkedList      #contents of newDoublyLinkedList here: 0 3 0 4 1 1 2 3
dllToBeSorted.sortList()
dllToBeSorted.removeDuplicates()
dllToBeSorted.display()                  #contents of dllToBeSorted here: 0 1 2 3 4 
newDoublyLinkedList.display()            #contents of newDoublyLinkedList here: 0 1 2 3 4 

Solution

  • This is because you are working on 2 different variables that point to the same data structure (https://www.geeksforgeeks.org/copy-python-deep-copy-shallow-copy/). Overriding copy in DoublyLinkedList should do the trick.

    def __copy__(self):
        newList = DoublyLinkedList()
        # add all data from self to newList
        return newList
    

    So your code would become:

    dllToBeSorted = DoublyLinkedList()
    dllToBeSorted = newDoublyLinkedList.copy()      #contents of newDoublyLinkedList here: 0 3 0 4 1 1 2 3
    dllToBeSorted.sortList()
    dllToBeSorted.removeDuplicates()
    dllToBeSorted.display()                  #contents of dllToBeSorted here: 0 1 2 3 4 
    newDoublyLinkedList.display()            #contents of newDoublyLinkedList here: 0 1 2 3 4