Lua 的数据结构

1. Arrays:

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

 1 data = {};
 2 for y = 1 , 7 do  --
 3   for x = 1 , 8 do --
 4    data[(y-1)*8+x] = (y-1)*8+x;
 5   end
 6 
 7  end
 8 print(#(data))
 9 for y = 1 , 7 do
10 
11   print(data[(y-1)*8+1].." "..data[(y-1)*8+2].." "..data[(y-1)*8+3].." "
12   ..data[(y-1)*8+4].." "..data[(y-1)*8+5].." "..data[(y-1)*8+6].." "
13   ..data[(y-1)*8+7].." "..data[(y-1)*8+8]);
14 
15 end;

2. LinkedList:

倒序:

 1 local head = nil
 2 
 3 head = {next = head, value = "d"}
 4 head = {next = head, value = "c"}
 5 head = {next = head, value = "b"}
 6 head = {next = head, value = "a"}
 7 
 8 local entry = head
 9 
10 while entry do
11   print(entry.value)
12 
13   entry = entry.next
14 end

正序:

 1 head ={next = nil, value = 0}
 2 per = head
 3 for i = 0, 10 do
 4     cur = {next = nil, value = i}
 5     per.next = cur
 6     per = cur
 7 end
 8 while head do
 9     print(head.value);
10     head = head.next;
11 end

 

 

posted on 2014-03-08 03:25  Step-BY-Step  阅读(152)  评论(0编辑  收藏  举报

导航