Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you implement a Stack(data structure)?

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!

2 comments

0

i dong have any experience in that sorry.

0

Follow PEP my guy.

1 answer

✓ Accepted answer

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))

This discussion is preserved as it was. New answers, comments, and votes are closed.