现在的位置: 首页 > 综合 > 正文

lua链表的简单实现

2019年08月06日 ⁄ 综合 ⁄ 共 537字 ⁄ 字号 评论关闭

lua 实现链表 是比较容易实现的, 但是遇到一个问题,导致判断条件一直出错。

遇到的问题分析:

list = {}

list = nil

 

两者是不同的。可以做个简单的判断:

 

1、

list = {}

 

if list == nil then 

print("list  == nil")

end

 

list = nil

 

if list == nil then

print ("list == nil")

end

 

 

2、

list = {}

 

list = { value = 1 , next = list }

 

 

if list.next == nil then

print("list == nil")

end

 

print (list.next.value)

 

 

链表的简单实现:

 

list = nil

 

for i =1 ,10 ,2 do

list={ value = i ,next = list }

end

 

local t = list

 

while t.next do

print(t.value)

t = t.next

end

 

 

if  t== nil then

print("t==nil")

end

 

t.next = { value = 20, next = nil}

 

local templist = list

 

while templist do

print(templist.value)

templist = templist.next

end

 

 

 

 

 

 

 

 

 

 

 

抱歉!评论已关闭.