Location list in Lua

I have been looking up the list of links in Lua recently and have a quick question, so far I have not found the answer

local head = nil

head = {next = head, value = "d"}
head = {next = head, value = "c"}
head = {next = head, value = "b"}
head = {next = head, value = "a"}

local entry = head

while entry do
print(entry.value)

entry = entry.next
end

This will finally print out “a,b,c,d “. I understand why it prints backwards, because the first “node” available will be the final one created (node ​​value = a). My question is why the head created before the last one still exists and has not been Simply cover in memory.

What do you mean by “covered in memory”? Nothing you do will cause this to happen.

Let us take a step-by-step look at what you are doing.

local head = nil

There is now a local variable named head. Its value is zero.

head = {next = head, value = " d"}

Let’s break it down into the sequence of operations here. This is equivalent to the following:

do
local temp = { }
temp.next = head --This is still `nil`
temp.value = "d"
head = temp
end

You build Each table in is a unique value. So we first call this table d table. It is constructed and stored in the temporary variable temp. The next value of the table is nil. It gets the value “d”. The result is stored in In the local variable header.

So now the head has the value table-d. Next step:

head = {next = head, value = "c" }

Same as:

do
local temp = {}
temp.next = head --Not nil anymore.< br /> temp.value = "c"
head = temp
end

Ok, let’s create a new table. For clarity, we will call this table c .

We store it in temp. Then we set its next field to the value in the head. The value is table-d. We set the value field to “c”. Then we set The table-c is stored in the head.

The table-c table is as follows:

{
next = {value = "d"}
value = "c"
}

That is the table stored in the header.

This continues like this. So what will be “overwritten”?

I have been looking up the list of links in Lua recently and have a quick question, so far I have not found the answer

local head = nil

head = {next = head, value = "d"}
head = {next = head, value = "c"}< br />head = {next = head, value = "b"}
head = {next = head, value = "a"}

local entry = head
< br />while entry do
print(entry.value)

entry = entry.next
end

This will finally print out “a,b ,c,d”. I understand why it prints backwards, because the first “node” available will be the one created in the end (node ​​value = a). My question is why the head created before the last one is still Exists and is not simply overwritten in memory.

What do you mean by “covered in memory”? Nothing you do will cause this to happen.

Let us take a step-by-step look at what you are doing.

local head = nil

There is now a local variable named head. Its value is zero.

head = {next = head, value = " d"}

Let’s break it down into the sequence of operations here. This is equivalent to the following:

do
local temp = { }
temp.next = head --This is still `nil`
temp.value = "d"
head = temp
end

You build Each table in is a unique value. So we first call this table d table. It is constructed and stored in the temporary variable temp. The next value of the table is nil. It gets the value “d”. The result is stored in In the local variable header.

So now the head has the value table-d. Next step:

head = {next = head, value = "c" }

Same as:

do
local temp = {}
temp.next = head --Not nil anymore.< br /> temp.value = "c"
head = temp
end

Ok, let’s create a new table. For clarity, we will call this table c .

We store it in temp. Then we set its next field to the value in the head. The value is table-d. We set the value field to “c”. Then we set The table-c is stored in the head.

The table-c table is as follows:

{
next = {value = "d"}
value = "c"
}

That is the table stored in the header.

This continues like this. So what will be “overwritten”?

Leave a Comment

Your email address will not be published.