Lua - Arrays as Stack
The stack is a crucial data structure that operates on the principle of Last-In-First-Out (LIFO). It represents a collection of elements where the most recently added element takes precedence in removal. The stack class in Java offers several methods to manipulate elements e-ffectively. For example, the push method allows you to add an element to the top of the stack, while pop removes and returns the topmost element.
In Lua, we can easy implement a Stack using Array. Let's create a Stack class in following steps:
Step 1: Create Stack class
Create a Stack function with metatable to modify the behaviour of the table.
function Stack()
return setmetatable({
-- stack table
_stack = {},
-- size of stack
count = 0,
}, {
__index = function(self, index)
return rawget(self._stack, index)
end,
})
end
Step 2: Push() function
Add the push function to the stack where we're appending an element to the end of array. As array in lua is dynamic, we're incrementing the count and setting the element works.
function Stack()
...
-- push an element to the stack underlying array
push = function(self, obj)
-- increment the index
self.count = self.count + 1
-- set the element at the end of the array
rawset(self._stack, self.count, obj)
end,
...
end
Step 2: Pop() function
Add the pop function to the stack where we're getting element from the end of array after removing it from the array.
function Stack()
...
-- pop an element from the stack
pop = function(self)
-- decrement the index
self.count = self.count - 1
-- remove and return the last element
return table.remove(self._stack)
end,
...
end
Step 3: Test Stack Operations
Create the Stack class and add values to the push() method, print the size of stack using count and then get a value from stack using pop() method.
-- create the stack
local stack = Stack()
-- push values to the stack
stack:push('A')
-- print the size of the stack
print(stack.count)
-- pop top element from the stack
print(stack:pop())
Stack: Complete Example
Following is the complete example of stack implementation using array with use of push() and pop() methods.
main.lua
-- Stack Implementation
function Stack()
return setmetatable({
-- stack table
_stack = {},
-- size of stack
count = 0,
-- push an element to the stack underlying array
push = function(self, obj)
-- increment the index
self.count = self.count + 1
-- set the element at the end of the array
rawset(self._stack, self.count, obj)
end,
-- pop an element from the stack
pop = function(self)
-- decrement the index
self.count = self.count - 1
-- remove and return the last element
return table.remove(self._stack)
end,
}, {
__index = function(self, index)
return rawget(self._stack, index)
end,
})
end
-- create the stack
local stack = Stack()
-- push values to the stack
stack:push('A')
stack:push('B')
-- print the size of the stack
print(stack.count)
-- pop top element from the stack
print(stack:pop())
-- print the updated size of the stack
print(stack.count)
-- pop top element from the stack
print(stack:pop())
Output
When we run the above code, we will get the following output−
B 1 A