【发布时间】:2020-11-19 12:04:02
【问题描述】:
我在 python 中练习了一些链表问题,这是我从用户那里提供链表节点然后打印它们的完整代码:
class Node:
def __init__(self,data):
self.data = data
self.next = None
class linkList:
def __init__(self):
self.head = None
# In this program first we get linked list nodes from users
# and then we will try to print all the nodes
GivenNode = input("please enter the first node")
OurList = linkList()
OurList.head = Node(GivenNode)
# Since now , temp is our node that were doing something on it:
temp = OurList.head
while (1):
GivenNode = input("Please enter the next node and if that ends please type end:")
if(GivenNode == "end"):
break
temp.next = Node(GivenNode)
temp = temp.next
# Now we want to print all nodes
temp_2 = OurList.head
while( temp_2.next != None ):
print(temp_2.data)
temp_2=temp_2.next
我不明白:
temp = temp.next
我认为这会删除临时记忆... 但是当我想打印所有节点时,它们已经存在了! 怎么样?
【问题讨论】:
-
"我认为这会删除临时内存。"好吧,它没有。为什么你这么想? Python 没有'任何直接控制内存管理。您不能手动删除对象。
-
@ juanpa.arrivillaga 但是当我们想删除一个笔记时,我们如何使用 temp=None?