转自http://www.cnblogs.com/stephen-liu74/archive/2012/06/25/2417894.html
在Lua中实现队列的简单方法是使用table库函数insert和remove。但是由于这种方法会导致后续元素的移动,因此当队列的数据量较大时,不建议使用该方法。下面的代码是一种更高效的实现方式,如:
List = {} function List.new()
return {first = , last = -}
end function List.pushFront(list, value)
local first = list.first -
list.first = first
list[first] = value
end function List.pushBack(list, value)
local last = list.last +
list.last = last
list[last] = value
end function List.popFront(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 +
return value
end function List.popBack(list)
local last = list.last
if list.first > last then
error("List is empty")
end
local value = list[last]
list[last] = nil
list.last = last -
return value
end