Lua数据结构。
来源:互联网 发布:淘宝朋友代付怎么没了 编辑:程序博客网 时间:2024/05/21 21:15
Lua数组/矩阵
> 方法一:local function DoTest() array = {} for i = 1,4 do array[i] = {} for j = 1,4 do array[i][j] = 12 end end for i = 1,4 do for j = 1,4 do print(array[i][j]) end endend> 方法二:local function DoTest() local array = {} for i = 1,4 do for j = 1,4 do -- 这里是一种计算偏差,这其实相当于一个一维连续数组了。 array[(i - 1) * 4 + j] = 12 end end for k = 1,4 do for v = 1,4 do print(array[(k - 1) * 4 + v]) end endend
Lua链表实现
list = nilfor i = 1,100 do -- 这里next = list不太理解,它是怎么找到下一个的list的呐? list = {next = list,value = i}endlocal l = listwhile l do print(l.value) l = l.nextend-- 其实我还是详细理解了一下,但我理解出来的结果是这样的。-- 上面的一句其实好像先自己弄了一个不知道哪来的相同结构表,-- 然后再把这个表给上一个list,所以我发现输出结果是倒序的。 temp = {} -- 这里的list 实际上就是刚声明的那个nil,把nil赋值给新的表的next -- 但是如果是第二次添加的话,这个list就是指第一个添加的list了,依此类推。 -- 其实这里是最不理解的了,它是怎么知道我现在是第几个表的哇? -- 大约过了5分钟。。。 -- 我现在明白了,因为list是一个全局变量,再我最后一句赋值后,下次再进来的时候, -- 明显他的next就是上次赋值后的那个list了,然后依此类推,并且它们是包括与被包括的。 -- next的值是另一个table,然后table里还有table。。。 temp.next = list temp.data = var list = temp
Lua队列
-- 这里想到的就是双端,每一端都是独立的一个队列。可以理解为一个x轴,-- pushfirst越多,first值越小,依此类推。local List = {}function List.new() return { first = 0,last = -1 }endfunction List.pushfirst(list,value) local first = list.first - 1 list.first = first list[first] = valueendfunction List.pushlast(list,value) local last = list.last + 1 list.last = last list[last] = valueendfunction List.popfirst(list) local first = list.first if first > list.last then error("list is empty") end local value = list[first] list[first] = nil list.first = first + 1 return valueendfunction List.poplast(list) local last = list.last if list.first > list.last then error("list is empty") end local value = list[last] list[last] = nil list.last = last - 1 return valueendlist = List.new()List.pushfirst(list,1990)List.pushlast(list,1991)print(List.popfirst(list))print(List.poplast(list))
0 0
- Lua数据结构
- lua数据结构
- Lua数据结构
- lua 数据结构
- Lua 数据结构
- Lua数据结构。
- lua数据结构
- lua的数据结构
- Lua的数据结构
- lua的数据结构 -- 数组
- lua的数据结构 -- 链表
- lua的数据结构 -- 队列
- lua中Table数据结构
- Lua 常用数据结构
- Lua数据结构 — T
- 细说Lua常用数据结构
- Lua学习之数据结构
- Lua学习笔记-数据结构
- 实体转化为JsonString的路线图
- SQL Server 字段设置默认值
- 一个字符串分隔多个参数存储过程调试
- 通过多重循环来遍历复杂json(json里面嵌套json)
- Spring事物管理的四种方式
- Lua数据结构。
- Node.js 事件发射器和监听器
- 浏览器查看信息妙用
- Java分页算法_3个最常用的
- java读取UTF-8的txt文件发现开头的一个字符问题
- 程序中的某一数据不能提交
- java学习笔记12——IO流解析
- 操作系统相关概念
- js跳转级别控制:js中跳转到父子区别