3 回答
TA贡献1860条经验 获得超8个赞
首先,您Node()拥有None不正确值的init head头应该有一个空头,这就是为什么None最后看到它的原因,因为它传播到结尾,最后您不打印最后一个,Node()因为最后一个节点没有next此条件,因此不显示Node没有下一个:
while currentNode.next != None:
print(currentNode.data, " --> ", end="")
currentNode = currentNode.next
因此,您需要再打印一张。
下面的工作示例略有简化:
class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
def get_data(self):
return self.data
def set_data(self, data):
self.data = data
def get_next(self):
return self.next
def set_next(self, node):
self.next = node
class SingleyLinkedList:
def __init__(self):
self.head = None
def insertAtHead(self, data):
old_head = self.head
self.head = Node(data, old_head)
print("Inserted ", data, " at the head")
def insertAtEnd(self, data):
currentNode = self.head
while currentNode.get_next() != None:
currentNode = currentNode.next
currentNode.set_next(Node(data))
print("Inserted ", data, " at end")
def printNode(self):
print("\nPrinting the nodes")
currentNode = self.head
while currentNode.next != None:
print(currentNode.data, " --> ", end="")
currentNode = currentNode.next
print(currentNode.data, " --> ", end="")
print(" NULL \n")
s = SingleyLinkedList()
s.insertAtHead(5)
s.printNode()
s.insertAtHead(10)
s.printNode()
s.insertAtHead(1)
s.printNode()
s.insertAtEnd(20)
s.printNode()
输出:
Inserted 5 at the head
Printing the nodes
5 --> NULL
Inserted 10 at the head
Printing the nodes
10 --> 5 --> NULL
Inserted 1 at the head
Printing the nodes
1 --> 10 --> 5 --> NULL
Inserted 20 at end
Printing the nodes
1 --> 10 --> 5 --> 20 --> NULL
TA贡献1828条经验 获得超3个赞
您的代码中的问题是:
self.head = Node()
例如,当您实例化SingleyLinkedList类时。您正在将链接列表对象的头分配给下一个为的此类节点None。
因此,您将获得None打印声明Printing the nodes 1 --> 10 --> 5 --> None --> NULL
下面是正确的代码:
class Node:
def __init__(self):
self.data = None
self.next = None
def get_data(self):
return self.data
def set_data(self, data):
self.data = data
def get_next(self):
return self.next
def set_next(self, node):
self.next = node
class SingleyLinkedList:
def __init__(self):
self.head = None
def insertAtHead(self, data):
newNode = Node()
newNode.set_data(data)
if self.head:
newNode.next = self.head
self.head = newNode
else:
self.head = newNode
print "Inserted ", data, " at the head"
def insertAtEnd(self, data):
currentNode = self.head
new_node = Node()
new_node.set_data(data)
while currentNode.get_next() != None:
currentNode = currentNode.next
currentNode.set_next(new_node)
print("Inserted ", data, " at end")
def printNode(self):
print("\nPrinting the nodes")
currentNode = self.head
while currentNode != None :
print currentNode.data,
print " --> ",
currentNode = currentNode.next
s = SingleyLinkedList()
s.insertAtHead(5)
s.printNode()
s.insertAtHead(10)
s.printNode()
s.insertAtHead(1)
s.printNode()
s.insertAtEnd(20)
s.printNode()
输出为:
Inserted 5 at the head
Printing the nodes
5 -->
Inserted 10 at the head
Printing the nodes
10 --> 5 -->
Inserted 1 at the head
Printing the nodes
1 --> 10 --> 5 -->
Inserted 20 at end
Printing the nodes
1 --> 10 --> 5 --> 20 -->
添加回答
举报