【问题标题】:Basic copying of Linked List to another LinkedList将链表基本复制到另一个链表
【发布时间】:2019-09-08 23:25:38
【问题描述】:

好的,所以我是计算机编程的新手,长话短说,我不想在余生中成为杂货商。我今年 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 对象工作。

【问题讨论】:

    标签: python linked-list nodes


    【解决方案1】:

    问题的很大一部分是这样的:

    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 缩进系统的抱怨之一是,如果粘贴代码的方式会改变缩进,也会改变行的分组。该行应该是循环的一部分,并且与它上面的行的缩进匹配。

    【讨论】:

    • @Cameron Carter 你是对的,注释掉的代码除了缩进还有其他问题,我没有仔细看。
    【解决方案2】:

    通读您的代码后,@John Bayko 似乎是正确的,因为您必须不理会 self.top(head sentinel) 所以......在您的代码中,您似乎有某些使用 self.top 的函数以及其他使用 current= self.top 来引用它并在函数内完成相应工作的人。 函数的顶部指针应该完全是因为它应该是一个引用普遍地 - 把它想象成“北极星”,让你在导航时作为你的向导list) 以供您的其余代码遵循。

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

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多