使用lua语言实现循环链表
2013-03-13 09:16
507 查看
List = {} --[[ 创建链表]] function List.new() local t = {next = nil, prev = nil, value = 0} t.next = t t.prev = t return t; end function List.push_front(list, val) local t = {next = list.next, prev = list, value = val} list.next.prev = t list.next = t end function List.push_back(list,val) local t = {next = list, prev = list.prev, value = val} list.prev.next = t list.prev = t end function List.pop_front(list) local t = list.next list.next = t.next t.next.prev = list t = nil end function List.pop_back(list) local t = list.prev list.prev = t.prev t.prev.next = list t = nil end function List.find(list , val) local t = list.next while not (t == list) do if t.value == val then return t end t = t.next end return nil end function List.insert(list , val) List.push_fornt(list, val) end --[[ 删除元素 ]] function List.erase(list, val) local t = List.find(list, val) if t then t.next.prev = t.prev t.prev.next = t.next t = nil else print("元素不存在") end end --[[ 输出链表 ]] function List.dump(list) local t = list.next while not (t == list) do print(t.value) t = t.next end end --[[ 链表反转 ]] function List.reverse(list) local t = list.next local curnode = list repeat curnode.next = curnode.prev curnode.prev = t curnode = t t = t.next until curnode == list end --[[ 查找最小值 ]] function List.find_min(beglist, endlist ) local t = beglist local m = beglist while not (t == endlist) do if t.value < m.value then m = t end t = t.next end return m end --[[ 排序 ]] function List.sort(list) local t = list.next while not (t == list) do --[[ 从剩余节点中查找最小值 ]] local m = List.find_min(t, list) --[[ 如果找到的是剩余节点的开始节点,则将开始节点后移 ]] if m == t then t = t.next end List.pop_front(m.prev) List.push_front(list, m.value) end List.reverse(list) end b = List.new() List.push_front(b, 10) List.push_front(b, 11) List.push_front(b, 15) List.dump(b) List.reverse(b) List.dump(b) List.sort(b) List.dump(b)
相关文章推荐
- 在LUA中使用GETTEXT实现多语言支持
- 使用C++实现的单向循环链表
- iOS使用lua语言的使用步骤与实现插件的动态更新
- 使用递归来实现在循环链表里删除第一节点不是数字‘2’
- 算法导论10.2-5 使用单向循环链表实现字典操作 INSERT、DELETE 和 SEARCH
- 单链表循环输入实现,使用Ctrl+Z结束输入
- 使用单向循环链表实现约瑟夫问题
- 合并有序链表,实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字。
- 使用循环神经网络实现语言模型——源自《TensorFlow:实战Goole深度学习框架》
- STL--> list 双向循环链表容器 接口使用及介绍。 模拟实现 STL list容器
- quick Lua 在Lua中使用gettext实现多语言支持
- 使用Lua实现链表的各种操作
- 在LUA中使用GETTEXT实现多语言支持
- 链表的反转(使用循环遍历实现)
- 使用数组实现栈和循环队列(JAVA语言)
- day01之合并两个有序链表+实现1+2+3...+n要求不能使用乘除法循环条件判断等
- tensorflow17《TensorFlow实战Google深度学习框架》笔记-08-02 使用循环神经网络实现语言模型 code
- 使用C++实现的单向循环链表
- 使用单向循环链表实现字典操作 INSERT、DELETE 和 SEARCH
- C语言 实现循环链表及双向链表