lua 实现栈

local  Stack = {}

function Stack:ctor()
    local  o = o or {};
    setmetatable(o,{__index = self})
    return o
end

function Stack:push(...)
    local args = {...}
    self.stackTB = self.stackTB or {}

    if next(args) then
        for i = 1,#args do
            table.insert(self.stackTB,args[i])
            print(args[i])
        end
    end
end

function Stack:pop()
    local popTB = {}
    if next(self.stackTB) then
        table.insert(popTB,self.stackTB[#self.stackTB])
        table.remove(self.stackTB)
    end
end

function Stack:cont()
    if next(self.stackTB) then
        return #self.stackTB
    end
    return 0
end

local  s = Stack:ctor()
s:push(1,2)

你可能感兴趣的:(lua,lua,开发语言)