
- Lua Tutorial
- Lua - Home
- Lua Basics
- Lua - Overview
- Lua - Environment
- Lua - Basic Syntax
- Lua - Comments
- Lua - Print Hello World
- Lua - Variables
- Lua - Data Types
- Lua - Operators
- Lua - Loops
- Lua - Generic For
- Lua - Decision Making
- Lua - Date and Time
- Lua Functions
- Lua - Functions
- Lua - Multiple Results
- Lua - Named Arguments
- Lua - Default/Optional Arguments
- Lua - Closures
- Lua - Uses of Closures
- Lua - Local Functions
- Lua - Anonymous Functions
- Lua - Functions in Table
- Lua - Proper Tail Calls
- Lua Strings
- Lua - Strings
- Lua - String Concatenation
- Lua - Loop Through String
- Lua - String to Int
- Lua - Split String
- Lua - Check String is NULL
- Lua Arrays
- Lua - Arrays
- Lua - Multi-dimensional Arrays
- Lua - Array Length
- Lua - Iterating Over Arrays
- Lua - Slicing Arrays
- Lua - Sorting Arrays
- Lua - Merging Arrays
- Lua - Sparse Arrays
- Lua - Searching Arrays
- Lua - Resizing Arrays
- Lua - Array to String Conversion
- Lua - Array as Stack
- Lua - Array as Queue
- Lua - Array with Metatables
- Lua - Immutable Arrays
- Lua - Shuffling Arrays
- Lua Iterators
- Lua - Iterators
- Lua - Stateless Iterators
- Lua - Stateful Iterators
- Lua - Built-in Iterators
- Lua - Custom Iterators
- Lua - Iterator Closures
- Lua - Infinite Iterators
- Lua - File Iterators
- Lua - Table Iterators
- Lua - Numeric Iterators
- Lua - Reverse Iterators
- Lua - Filter Iterators
- Lua - Range Iterators
- Lua - Chaining Iterators
- Lua Tables
- Lua - Tables
- Lua - Tables as Arrays
- Lua - Tables as Dictionaries
- Lua - Tables as Sets
- Lua - Table Length
- Lua - Table Iteration
- Lua - Table Constructors
- Lua - Loop through Table
- Lua - Merge Tables
- Lua - Nested Tables
- Lua - Accessing Table Fields
- Lua - Copy Table by Value
- Lua - Get Entries from Table
- Lua - Table Metatables
- Lua - Tables as Objects
- Lua - Table Inheritance
- Lua - Table Cloning
- Lua - Table Sorting
- Lua - Table Searching
- Lua - Table Serialization
- Lua - Weak Tables
- Lua - Table Memory Management
- Lua - Tables as Stacks
- Lua - Tables as Queues
- Lua - Sparse Tables
- Lua Lists
- Lua - Lists
- Lua - Inserting Elements into Lists
- Lua - Removing Elements from Lists
- Lua - Iterating Over Lists
- Lua - Reverse Iterating Over Lists
- Lua - Accessing List Elements
- Lua - Modifying List Elements
- Lua - List Length
- Lua - Concatenate Lists
- Lua - Slicing Lists
- Lua - Sorting Lists
- Lua - Reversing Lists
- Lua - Searching in Lists
- Lua - Shuffling List
- Lua - Multi-dimensional Lists
- Lua - Sparse Lists
- Lua - Lists as Stacks
- Lua - Lists as Queues
- Lua - Functional Operations on Lists
- Lua - Immutable Lists
- Lua - List Serialization
- Lua - Metatables with Lists
- Lua Modules
- Lua - Modules
- Lua - Returning Functions from Modules
- Lua - Returning Functions Table from Modules
- Lua - Module Scope
- Lua - SubModule
- Lua - Module Caching
- Lua - Custom Module Loaders
- Lua - Namespaces
- Lua - Singleton Modules
- Lua - Sharing State Between Modules
- Lua - Module Versioning
- Lua Metatables
- Lua - Metatables
- Lua - Chaining Metatables
- Lua Coroutines
- Lua - Coroutines
- Lua File Handling
- Lua - File I/O
- Lua - Opening Files
- Lua - Modes for File Access
- Lua - Reading Files
- Lua - Writing Files
- Lua - Closing Files
- Lua - Renaming Files
- Lua - Deleting Files
- Lua - File Buffers and Flushing
- Lua - Reading Files Line by Line
- Lua - Binary File Handling
- Lua - File Positioning
- Lua - Appending to Files
- Lua - Error Handling in File Operations
- Lua - Checking if File exists
- Lua - Checking if File is Readable
- Lua - Checking if File is Writable
- Lua - Checking if File is ReadOnly
- Lua - File Descriptors
- Lua - Creating Temporary Files
- Lua - Working with Large Files
- Lua Advanced
- Lua - Error Handling
- Lua - Debugging
- Lua - Garbage Collection
- Lua - Object Oriented
- Lua - Web Programming
- Lua - Database Access
- Lua - Game Programing
- Lua Useful Resources
- Lua - Quick Guide
- Lua - Useful Resources
- Lua - Discussion
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