Ad
Log in to vote
0

How do you implement a Stack(data structure)?

Asked by 7 months ago
Edited by Ziffixture 7 months ago

I've been messing around with a lot of data structures lately and I can't figure out how to implement a Stack in Lua. Here is how it would look like in Python.

Is there a possible solution to implement a Stack in Lua? If so, please provide me with a sample code!

0
i dong have any experience in that sorry. catsalt29 57 — 7mo
0
Follow PEP my guy. Ziffixture 6785 — 7mo

1 answer

Log in to vote
1
Answered by 7 months ago

A very simple stack system can be implemented using built-in Lua functions.

local function top(stack, item)
    table.insert(stack, item)
end

local function pop(stack)
    return table.remove(stack, #stack)
end

function peek(stack)
    return stack[#stack]
end

local stack = {}

top(stack, 67)

print(peek(stack))
print(pop(stack))
print(peek(stack))

Ad

Answer this question