Stack<T> (LIFO)

Arrays and collections

-- An array is a stack: push and pop at the end
local int_stack = {}
table.insert(int_stack, 1)
table.insert(int_stack, 3)
table.insert(int_stack, 5)

-- Look at the top without removing it
local top = int_stack[#int_stack]
-- top = 5
local first = table.remove(int_stack)
-- first = 5
local second = table.remove(int_stack)
-- second = 3
local third = table.remove(int_stack)
-- third = 1

local is_empty = #int_stack == 0
-- is_empty = true

print("top = " .. top)
print("first = " .. first)
print("second = " .. second)
print("third = " .. third)
print("is_empty = " .. tostring(is_empty))