Python 将链接列表基本复制到另一个LinkedList

Python 将链接列表基本复制到另一个LinkedList,python,linked-list,nodes,Python,Linked List,Nodes,好吧,我是电脑编程新手,长话短说,我不想在我的余生中成为一名杂货商。我今年25岁,正在学习这些概念和python,所以请友好一点 我想使用copyList函数将链接列表从LinkedList对象复制到另一个对象。它不应该接受除自身以外的任何参数,并且应该在不改变原始列表的情况下输出LinkedList的副本 我尝试查看堆栈,发现了一个类似的代码,但它没有解决我的问题,因为代码与我的类似,但不起作用,因为我尝试打印新的LinkedList,它不包含值,我相信是空的。我在下面注释掉的代码中提供了类似

好吧,我是电脑编程新手,长话短说,我不想在我的余生中成为一名杂货商。我今年25岁,正在学习这些概念和python,所以请友好一点

我想使用copyList函数将链接列表从LinkedList对象复制到另一个对象。它不应该接受除自身以外的任何参数,并且应该在不改变原始列表的情况下输出LinkedList的副本

我尝试查看堆栈,发现了一个类似的代码,但它没有解决我的问题,因为代码与我的类似,但不起作用,因为我尝试打印新的LinkedList,它不包含值,我相信是空的。我在下面注释掉的代码中提供了类似的代码:

class Node:
    def __init__(self,x):
        self.data = x
        self.next = None
class LinkedList:
    def __init__(self):
        self.top = None
    def printList(self):
        print("top^")
        while self.top is not None:
            print(self.top.data)
            self.top = self.top.next
        print("tail^")
    def in_list(self, x):
        current = self.top 
        while current is not None:
            if current.data is x:
                return 1
            current = current.next
        return 0
    def findCellBefore(self, x):
        current = self.top
        if current is None:
            return 0
        while current.next is not None:
            if current.next.data is x:
                current = current.next
        return 1
    def findCellBeforeSential(self,x):
        if (self.top.next is None):
            return 0 
        while (self.top.next is not None):
            if (self.top.next.data is x):
                return self.top.data
        return 1
    def add_0(self, newNode):
        # i. make next of new node as head.
        newNode.next = self.top
        # ii. move head to point to new node.
        self.top = newNode
    def add_end(self, newNode):
        current = self.top
        if (current is None):
            self.add_0(newNode)
            return 
        while (current.next is not None):
            current = current.next

        current.next = newNode 
        newNode.next = None
    def insertNode(self, after_me, new_cell):
        new_cell.next = after_me.next 
        after_me.next = new_cell

        # update prev links.

        new_cell.next.prev = new_cell 
        new_cell.prev = after_me 
    def  deleteAfter(self, after_me):
            after_me.next = after_me.next.next

    def CopyList(self):#Out put new Linked List that is a copy of current Linked List with out altering it. 
        # create new LinkedList
        newLinkedList = LinkedList()
        #current = self.top
        #below is from stackoverflow : https://stackoverflow.com/questions/36491307/how-to-copy-linked-list-in-python
        #while current.next != None:
        #    newLinkedList.add_end(current.data)
        #    current = current.next
        #newLinkedList.add_end(current.data)
        #return newLinkedList
        while self.top is not None:
            newNode = Node(self.top.data)
            newLinkedList.add_end(newNode)
            self.top = self.top.next
        return newLinkedList

LIST0 = LinkedList()

node0 = Node(1)
node1 = Node(2)
node2 = Node(3)
LIST0.add_end(node1)
LIST0.add_0(node0)
LIST0.add_0(node2)
node3 = Node(4)
LIST0.insertNode(node2, node3)

LIST0.printList()

LIST1=LIST0.CopyList()

LIST1.printList()

我希望它只打印出一个新列表,它是LIST0的副本,并让LIST1作为LinkedList对象工作。

问题的一个主要部分是:

while self.top is not None:
    newNode = Node(self.top.data)
    newLinkedList.add_end(newNode)
    self.top = self.top.next
通常self.top指向节点的顶部,除非替换或删除顶部节点,否则不应更改。这里所做的基本上是从列表中删除所有节点


注释掉的代码看起来是正确的,只是行“newLinkedList.add_end(current.data)”缩进不够。Python缩进系统的一个抱怨是,如果代码以改变缩进的方式粘贴,也会改变行的分组。该行应该是循环的一部分,并与上面行的缩进相匹配。

在阅读代码之后,@John Bayko似乎是正确的,因为您必须将self.top(head sentinel)单独留下,以便。。。在代码中,某些函数使用self.top out right,其他函数使用current=self.top引用它并在函数中执行相应的工作函数的顶部指针应该正好是,它应该是一个参考(普遍认为它是“北极星”,让您在导航列表时遵循它作为指南),以便代码的其余部分遵循它

下面是更正的代码:在理解链表和其他概念之后,应该会更容易理解

class Node:
    def __init__(self,x):
        self.data = x
        self.next = None
class LinkedList:
    def __init__(self):
        self.top = None
    def printList(self):
        print("top^")
        current = self.top
        while current is not None:
            print(current.data)
            current = current.next
        print("tail^")
    def in_list(self, x):
        current = self.top 
        while current is not None:
            if current.data is x:
                return 1
            current = current.next
        return 0
    def findCellBefore(self, x):
        current = self.top
        if current is None:
            return 0
        while current.next is not None:
            if current.next.data is x:
                current = current.next
        return 1
    def findCellBeforeSential(self,x):
        current = self.top
        if (current.next is None):
            return 0 
        while (current.next is not None):
            if (current.next.data is x):
                return current.data
        return 1
    def add_0(self, newNode):
        # i. make next of new node as head.
        newNode.next = self.top
        # ii. move head to point to new node.
        self.top = newNode
    def add_end(self, newNode):
        current = self.top
        if (current is None):
            self.add_0(newNode)
            return 
        while (current.next is not None):
            current = current.next

        current.next = newNode 
        newNode.next = None
    def insertNode(self, after_me, new_cell):
        new_cell.next = after_me.next 
        after_me.next = new_cell

        # update prev links.

        new_cell.next.prev = new_cell 
        new_cell.prev = after_me 
    def  deleteAfter(self, after_me):
            after_me.next = after_me.next.next

    def CopyList(self):#Out put new Linked List that is a copy of current Linked List with out altering it. 
        # create new LinkedList
        newLinkedList = LinkedList()
        current = self.top
        #below is from stackoverflow : https://stackoverflow.com/questions/36491307/how-to-copy-linked-list-in-python
        while current is not None:
            newNode = Node(current.data)
            newLinkedList.add_end(newNode)
            current = current.next
        return newLinkedList
        print("here")

        #current = self.top
        #print(current)
        #while current.next is not None:
        #    print(0)
        #    newNode = Node(self.top.data)
        #    print(1)
        #    newLinkedList.add_end(newNode)
        #    print(2)
        #    self.top = self.top.next
        return newLinkedList

LIST0 = LinkedList()

node0 = Node(1)
node1 = Node(2)
node2 = Node(3)
LIST0.add_end(node1)
LIST0.add_0(node0)
LIST0.add_0(node2)
node3 = Node(4)
LIST0.insertNode(node2, node3)

LIST0.printList()

LIST1=LIST0.CopyList()

LIST1.printList()

@Cameron Carter你是对的,注释掉的代码除了缩进还有其他问题,我没有仔细看。