Updated April 7, 2023
Introduction to Lua list
A wonderful data structure used to add elements, remove elements and iterate through the elements efficiently in Lua is called Linked lists, which are of two kinds, namely singly-linked lists and doubly linked list where we have a reference pointing to the first node in a singly linked list, and there is a reference from each node to the next node in the singly linked list, and we have references to both first node and last node in a doubly-linked list and each node holds a reference to the previous node and to the next node in a doubly-linked list.
Syntax to define lists in Lua:
list_name = {}
list_name = {next = list_name, value = value_to_be_inserted}
Where,
- list_name is the name of the list to be created.
- value_to_be_inserted is the value that is to be inserted at the beginning of the list.
Working of lists in Lua
- A wonderful data structure used to add elements, remove elements and iterate through the elements efficiently in Lua is called Linked lists.
- The Linked lists in Lua are of two kinds, namely singly-linked lists and doubly linked list.
- A singly linked list in Lua will have a reference pointing to the first node in a singly linked list, and there is a reference from each node to the next node in a singly linked list.
- A doubly linked list in Lua will have references to both the first node and last node in a doubly-linked list, and each node holds a reference to the previous node and to the next node in a doubly-linked list.
- But it is not possible to find an element through indexes in Linked lists.
Examples of Lua list
Given below are the examples of the Lua list:
Example #1
Lua program to demonstrate linked lists where we create a linked list, insert the elements into the list, display the elements from the list after insert operation, delete an element from the list and display the elements from the list after a delete operation.
Code:
--creating a list called mylist
local mylist = nil
--inserting the elements into the list mylist
mylist = {next = mylist, value = "EDUCBA"}
mylist = {next = mylist, value = "to"}
mylist = {next = mylist, value = "Welcome"}
--displaying the elements of the list after insert operation
local firstit = mylist
print("The elements in the list after insert operation is:\n")
while firstit do
print(firstit.value)
firstit = firstit.next
end
print("\nPlease enter the element from the list to be removed:\n")
elementremoved = io.read()
--delete an element from the list
local secondit = mylist
while secondit do
if(secondit.value==elementremoved) then
secondit.value = nil
break
end
secondit = secondit.next
end
--displaying the elements of the list after delete operation
print("The elements in the list after delete operation are:\n")
local thirdit = mylist
while thirdit do
print(thirdit.value)
thirdit = thirdit.next
end
Output:
- In the above program, we are creating an empty singly linked list called mylist. Then we are inserting the element into the list and then display the elements in the list after insert operation as the output on the screen.
- Then we are making use of io.read() function to take the input from the user and delete the corresponding element from the list and then display the elements in the list after delete operation as the output on the screen.
Example #2
Lua program to demonstrate linked lists where we create a linked list, insert the elements into the list, display the elements from the list after insert operation, delete an element from the list and display the elements from the list after a delete operation.
Code:
--creating a list called mylist
local mylist = nil
--inserting the elements into the list mylist
mylist = {next = mylist, value = "10"}
mylist = {next = mylist, value = "20"}
mylist = {next = mylist, value = "30"}
mylist = {next = mylist, value = "40"}
mylist = {next = mylist, value = "50"}
mylist = {next = mylist, value = "60"}
--displaying the elements of the list after insert operation
local firstit = mylist
print("The elements in the list after insert operation is:\n")
while firstit do
print(firstit.value)
firstit = firstit.next
end
print("\nPlease enter the element from the list to be removed:\n")
elementremoved = io.read()
--delete an element from the list
local secondit = mylist
while secondit do
if(secondit.value==elementremoved) then
secondit.value = nil
break
end
secondit = secondit.next
end
--displaying the elements of the list after delete operation
print("The elements in the list after delete operation are:\n")
local thirdit = mylist
while thirdit do
print(thirdit.value)
thirdit = thirdit.next
end
Output:
Explanation:
- In the above program, we are creating an empty singly linked list called mylist. Then we are inserting the element into the list and then display the elements in the list after insert operation as the output on the screen.
- Then we are making use of io.read() function to take the input from the user and delete the corresponding element from the list and then display the elements in the list after delete operation as the output on the screen. The output is shown in the snapshot above.
Recommended Articles
We hope that this EDUCBA information on “Lua list” was beneficial to you. You can view EDUCBA’s recommended articles for more information.