gpt4 book ai didi

python - python中的单链表反向

转载 作者:行者123 更新时间:2023-11-30 23:46:58 25 4
gpt4 key购买 nike

我正在尝试在 python 中创建一个简单的单链表。(我知道不需要在Python中实现列表,但这不是重点)

这是我的代码:

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



class List:
def __init__(self):
self.firstNode = Node(None)

def inserthead(self,newnode):
if not self.firstNode.next:
newnode.next = None
self.firstNode.next = newnode
else:
newnode.next = self.firstNode.next
self.firstNode.next= newnode


def __show(self,start):
if start.next:
print start.data
self.__show(start.next)

def printlist(self):
self.__show(self.firstNode)

def __reverte_recursive(self,node):
temp = None
if not node.next: return node
else:
temp = self.__reverte_recursive(node.next)
node.next.next= node
node.next = None
return temp

def reverte_list1(self):
self.firstNode=self.__reverte_recursive(self.firstNode)

def __reverte_iterative(self,node):
temp = None
previous = None
while node and node.next:
temp = node.next
node.next= previous
previous = node
node = temp
return previous
def reverte_iterative(self):
self.firstNode=self.__reverte_iterative(self.firstNode)

nodeA = Node("A")
nodeB = Node("B")
nodeC = Node("C")
nodeD = Node("D")
nodeE = Node("E")


list1= List()

list1.inserthead(nodeA)
list1.inserthead(nodeB)
class Node:
def __init__(self,data):
self.data = data
self.next= None



class List:
def __init__(self):
self.firstNode = Node(None)

def inserthead(self,newnode):
if not self.firstNode.next:
newnode.next = None
self.firstNode.next = newnode
else:
newnode.next = self.firstNode.next
self.firstNode.next= newnode


def __show(self,start):
if start.next:
print start.data
self.__show(start.next)

def printlist(self):
self.__show(self.firstNode)

def __reverte_recursive(self,node):
temp = None
if not node.next: return node
else:
temp = self.__reverte_recursive(node.next)
node.next.next= node
node.next = None
return temp

def reverte_list1(self):
self.firstNode=self.__reverte_recursive(self.firstNode)

def __reverte_iterative(self,node):
temp = None
previous = None
while node and node.next:
temp = node.next
node.next= previous
previous = node
node = temp
return previous
def reverte_iterative(self):
self.firstNode=self.__reverte_iterative(self.firstNode)

nodeA = Node("A")
nodeB = Node("B")
nodeC = Node("C")
nodeD = Node("D")
nodeE = Node("E")


list1= List()

list1.inserthead(nodeA)
list1.inserthead(nodeB)
list1.inserthead(nodeC)
list1.inserthead(nodeD)
list1.inserthead(nodeE)


print "list"
list1.printlist()
print "list reverse"
list1.reverte_list1()
list1.printlist()
list1.reverte_iterative()
print "list reverse reverse"
list1.printlist()

结果如下:

None
E
D
C
B
list reverse
A
B
C
D
E
list reverse reverse
E
D
C
B

由于某种原因,我无法打印所有列表,并且在第一种情况下不打印“A”节点但打印第一个节点(但我检查过并且 B 节点指向 A)第一个反向就OK了但第三个同样不打印 A 节点,即使它是由 B 节点指向的。打印的问题可能出在 __show 函数中。但我想我是一个概念错误。

谢谢

最佳答案

def __show(self,start):
if start.next:
print start.data
self.__show(start.next)

仅在有下一个节点时才打印当前节点,这就是为什么最后一个节点永远不会被打印的原因。应该是:

def __show(self,start):
if start:
print start.data
self.__show(start.next)

您在整个代码中检查/分配节点而不是下一个节点时犯了类似的错误,反之亦然(例如在 inserthead() 中 - 这导致 None正在打印)

关于python - python中的单链表反向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8555661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com