您的位置:首页 > 理论基础 > 数据结构算法

Lua 的数据结构

2014-03-08 03:25 295 查看
1. Arrays:

注意 #(data), # 加上 table名字 == size of

data = {};
for y = 1 , 7 do  --行
for x = 1 , 8 do --列
data[(y-1)*8+x] = (y-1)*8+x;
end

end
print(#(data))
for y = 1 , 7 do

print(data[(y-1)*8+1].." "..data[(y-1)*8+2].." "..data[(y-1)*8+3].." "
..data[(y-1)*8+4].." "..data[(y-1)*8+5].." "..data[(y-1)*8+6].." "
..data[(y-1)*8+7].." "..data[(y-1)*8+8]);

end;


2. LinkedList:

倒序:

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


正序:

head ={next = nil, value = 0}
per = head
for i = 0, 10 do
cur = {next = nil, value = i}
per.next = cur
per = cur
end
while head do
print(head.value);
head = head.next;
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: