将链表基本复制到另一个链表



好吧,所以我是计算机编程的新手,长话短说,我不想在我的余生中做杂货商。我今年25岁,刚刚学习这些概念和python,所以请善待。

我想使用一个名为 copyList 的函数将链表从 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(头部哨兵(独自一人,所以......在您的代码中,您似乎有某些函数使用 self.top 右,而其他函数使用 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()

相关内容

  • 没有找到相关文章

最新更新