可疑的温度 = temp.next
Suspicious temp = temp.next
我在练习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
我认为这会删除临时记忆...
但是当我想打印所有节点时,它们已经存在了!
怎么样?
temp
只是一个参考。当您初始化 OurList
并将 Node
添加到其 head
时,您已经在内存中创建了实例。 temp
只是对这些实例的引用,可以随时更新。
让我们从 temp = OurList.head
开始。
GivenNode = 1
OurList = linkList()
OurList.head = Node(GivenNode) // Node1
temp = OurList.head // here temp = Node1
GivenNode = 2
temp.next = Node(GivenNode) // temp = Node1, temp.next = OurList.head.next = Node2
temp = temp.next // temp = Node2
看到了吗?
首先,Node
的 class 实例已初始化。这个 class 有一个内存地址 Address A
并且存储在 LinkedList
实例的 head
中。
在每个循环中,执行两个操作。
next
Node
class 的属性使用 temp.next
指向一个新的 Node
实例,存储在 Address B
- 在此初始化之后,
temp
被重新初始化为 Address B
处的 Node
实例。所以下一个循环将在 Address C
处有 Node
个实例,依此类推。
因此,实例相互指向,所有内容都在内存中,temp
一直从一个节点移动到另一个节点,沿途创建新连接。
现在,当您初始化 temp_2 = OurList.head
时,temp_2
现在指向内存地址 Address A
上的一个实例,并且循环只遍历 [=11= 建立的所有连接] 在上一个循环中。
简而言之,temp
和 temp_2
只是在任何给定时间点指向某个实例的一些引用。您只是在任何给定时间点更改这些变量指向的实例,整个数据在内存中仍然完好无损,并且可以通过 OurList.head
、OurList.head.next
、OurList.head.next.next
引用等等。
我在练习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
我认为这会删除临时记忆... 但是当我想打印所有节点时,它们已经存在了! 怎么样?
temp
只是一个参考。当您初始化 OurList
并将 Node
添加到其 head
时,您已经在内存中创建了实例。 temp
只是对这些实例的引用,可以随时更新。
让我们从 temp = OurList.head
开始。
GivenNode = 1
OurList = linkList()
OurList.head = Node(GivenNode) // Node1
temp = OurList.head // here temp = Node1
GivenNode = 2
temp.next = Node(GivenNode) // temp = Node1, temp.next = OurList.head.next = Node2
temp = temp.next // temp = Node2
看到了吗?
首先,Node
的 class 实例已初始化。这个 class 有一个内存地址 Address A
并且存储在 LinkedList
实例的 head
中。
在每个循环中,执行两个操作。
next
Node
class 的属性使用temp.next
指向一个新的Node
实例,存储在Address B
- 在此初始化之后,
temp
被重新初始化为Address B
处的Node
实例。所以下一个循环将在Address C
处有Node
个实例,依此类推。
因此,实例相互指向,所有内容都在内存中,temp
一直从一个节点移动到另一个节点,沿途创建新连接。
现在,当您初始化 temp_2 = OurList.head
时,temp_2
现在指向内存地址 Address A
上的一个实例,并且循环只遍历 [=11= 建立的所有连接] 在上一个循环中。
简而言之,temp
和 temp_2
只是在任何给定时间点指向某个实例的一些引用。您只是在任何给定时间点更改这些变量指向的实例,整个数据在内存中仍然完好无损,并且可以通过 OurList.head
、OurList.head.next
、OurList.head.next.next
引用等等。