Lua - Table as Stack



A table can be enhanced using metamethods to act as a 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.

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. We're adding a metatable _stack to our Table.

function Stack()
   return setmetatable({
      -- stack table  
      _stack = {},
      -- size of stack
      count = 0,

   }, {
      __index = function(self, index)
         -- get element from stack at given 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 table.

function Stack()
...
   -- push an element to the stack underlying table
   push = function(self, obj)
      -- increment the index
      self.count = self.count + 1
      -- set the element at the end of the table
      rawset(self._stack, self.count, obj)
   end,
...
end	

Step 3: Pop() function

Add the pop function to the stack where we're getting element from the end of table after removing it from the table.

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 4: 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()
   stackTable = {
      -- stack table  
      _stack = {},
      -- size of stack
      count = 0,

      -- push an element to the stack underlying table
      push = function(self, obj)
         -- increment the index
         self.count = self.count + 1
         -- set the element at the end of the table
         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,
   }
   
   metaTable = {
      __index = function(self, index)
         return rawget(self._stack, index)
      end,
   }

   return setmetatable( stackTable, metaTable)
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
Advertisements