diff options
Diffstat (limited to 'stack.lua')
-rw-r--r-- | stack.lua | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/stack.lua b/stack.lua new file mode 100644 index 0000000..bee5fd3 --- /dev/null +++ b/stack.lua @@ -0,0 +1,38 @@ +local M = {} + +local Stack = {} + +function Stack:push(v) + table.insert(self._et, v) +end + +function Stack:pop() + return table.remove(self._et) +end + +function Stack:remove(it) + local idx = nil + for i, v in ipairs(self._et) do + if v == it then + idx = i + break + end + end + if idx == nil then + error(it .. " not in stack") + end + table.remove(self._et, idx) +end + +function Stack:new() + local o = setmetatable({}, self) + self.__index = self + + o._et = {} + + return o +end + +M.Stack = Stack + +return M |