1:首先说个蛋疼的问题
--声明2个字段名相同的table
arr = { value =99 ,age =23 }
arr = { value = 100 , id =10001}
print(arr.value) ----> 100
print(arr.age) ----->nil
=======================代码2==================
arr = { value =99 ,age =23 }
arr = { value = 100 , id =10001 , next = arr} ----->这里的next 指向了 前面的 arr
print(arr.value) ----> 100
print(arr.age) ----->nil
print(arr.next.age) ----->23
----->那前面的一个arr去哪了?没有被回收,也不能直接访问(可以在声明下一个同名table前直接访问)
2:声明链表
arr = { value = 99,age = 23}
--print(arr.age)
arr = { value = 100, id =100001,xia = arr}
arr = { xia = arr,value=101}
print(arr.xia.xia.age) -----> 23
=================当然 循环创建是最方便的===================
list = { value = 1} --表头
for i = 1 , 10 do
list = {next = list,value =i+1}
end
l = list
while l do
print(l.value) ---------> 11 10 9 8 7 6 5 4 3 2 1
l = l.next
end
print(list.next.next.value) -----> 9
---------->怎么直接访问链表中的第五个元素呢?list.next.next.next.next.next ?循环一下
five = list
for i = 1 , 4 do
five = five.next
end
print(five.value) ----> 5