我正在尝试将元素添加到链接列表的末尾,但看不到这种情况。似乎总是在链接列表的末尾添加“无”。我不确定为什么会发生这种情况,并且我猜想这与我的.next设置不正确有关。我正在使用python来实现我的链表。有人可以帮我吗
下面是我的Node类
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 = Node()
def insertAtHead(self, data):
currentNode = self.head
newNode = Node()
newNode.set_data(data)
if currentNode != None:
newNode.set_next(currentNode)
self.head = newNode
print("Inserted ", data, " at the head")
else:
self.head.set_next(newNode)
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.next != None:
print(currentNode.data, " --> ", end="")
currentNode = currentNode.next
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()
我得到以下结果,
在头部插入5
打印节点 5-> NULL
在头部插入10个
打印节点 10-> 5-> NULL
在头部插入1
打印节点1-> 10-> 5-> NULL
最后插入20个
打印节点1-> 10-> 5->无-> NULL
答案 0 :(得分:1)
首先使用public class U1 extends Rocket {
public U1() {
maxWeight = 18;
}
}
public class U2 extends Rocket {
public U2() {
maxWeight = 23;
}
}
保留Node()
值的初始头是不正确的,您应该拥有空头,这就是为什么您最终看到None
的原因,因为它传播到末尾并且最后,您不打印最后一个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()
答案 1 :(得分:0)
您的insertAtEnd
是正确的,但是您的printNode
是错误的。实际上,它始终用NULL
替换链接列表中的最后一项。当您将项目添加到列表的开头时,这很好,因为它抑制了用来初始化链接列表的空head
节点。但是,当您在列表末尾插入项目时,该虚拟节点现在位于列表的中间,并且在打印时将显示为None
。
s = SingleyLinkedList()
s.insertAtEnd(1)
s.insertAtEnd(2)
s.insertAtEnd(3)
打印
Printing the nodes
None --> 1 --> 2 --> NULL
解决方案:初始化self.head = None
,并在您的方法中适当地处理这种情况。
答案 2 :(得分:0)
您的代码中的问题是:-
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
print
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 -->