Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you implement a Stack(data structure)?

Asked by 3 years ago
Edited by Ziffixture 3 years 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 — 3y
0
Follow PEP my guy. Ziffixture 6913 — 3y

1 answer

Log in to vote
1
Answered by 3 years 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