我在Lua中使用队列 http://www.lua.org/pil/11.4.html
List = {}
function List.New ()
return {first = 0, last = -1}
end
function List.PushRight (list, value)
local last = list.last + 1
list.last = last
list[last] = value
end
function List.PopLeft (list)
local first = list.first
if first > list.last then print("error: list is empty") end
local value = list[first]
list[first] = nil -- to allow garbage collection
list.first = first + 1
return value
end
我使用以下命令访问队列:
list = List.New
List.PushRight(list, position)
local popped = List.PopLeft(list)
List.PushRight(list, popped)
它在PushRight命令中崩溃:
local last = list.last + 1
错误是:
attempt to index local 'list' (a function value)
'list'显然不是函数值。它是一个List,我使用键'last'实例化,我应该能够使用值-1访问它。为什么调用列表是一个函数?
感谢。