Other 5 data structures implemented in LUA using table _lua

Source: Internet
Author: User
Tags data structures lua reserved

The table in Lua is not a simple data structure that can be used as a basis for other data structures, such as arrays, records, lists, queues, and so on.

1, array

In Lua, the index of a table can be represented in many ways. If you use an integer to represent the index of a table, you can use a table to implement the array, and in Lua the index usually starts at 1.

Copy Code code as follows:

--Two-dimensional array
n=10 m=10
arr={}
For I=1,n do
arr[i]={}
For J=1,m do
Arr[i][j]=i*j
End
End

For I=1, n do
For J=1, M do
if (j~=m) then Io.write (Arr[i][j] ... " ")
else print (Arr[i][j])
End
End
End

2, linked list

In Lua, because a table is a dynamic entity, it is convenient to represent a linked list, where each node is represented by a table.

Copy Code code as follows:

List = Nil
For i = 1, the Do
List = {next = list, value = i}
End

Local L = List
While L do
Print (L.value)
L = L.next
End

3, queue and two-terminal queue

The simple way to implement queues in Lua is to call the insert and remove functions in the table, but if the volume of data is large, the efficiency is slow, and the following are manual implementations that are much faster.

Copy Code code as follows:

list={}

function List.new ()
return {first=0, last=-1}
End

function List.pushfront (list,value)
List.first=list.first-1
list[List.first]=value
End

function List.pushback (list,value)
List.last=list.last+1
list[List.last]=value
End

function List.popfront (List)
Local First=list.first
If First>list.last then error ("list is empty!")
End
Local value =list[first]
List[first]=nil
List.first=first+1
return value
End

function List.popback (List)
Local Last=list.last
If Last<list.first then error ("list is empty!")
End
Local value =list[last]
List[last]=nil
List.last=last-1
return value
End

Lp=list.new ()
List.pushfront (lp,1)
List.pushfront (lp,2)
List.pushback (lp,-1)
List.pushback (lp,-2)
X=LIST.POPFRONT (LP)
Print (x)
X=LIST.POPBACK (LP)
Print (x)
X=LIST.POPFRONT (LP)
Print (x)
X=LIST.POPBACK (LP)
Print (x)
X=LIST.POPBACK (LP)
Print (x)
--Output results
--2
---2
--1
---1
--Lua: ... List is empty!

4. Collection and Package

Using the table in Lua to implement the collection is very simple, see the following code:

Copy Code code as follows:

reserved = {[' while '] = true, [' end '] = true, [' function '] = true,}
If not reserved[' while '] then
--do something
End

In Lua we can consider a package (BAG) as a multiset, unlike a normal set, where elements that allow key in the container appear multiple times in the container. The following code simulates the implementation of the data structure by adding a counter to the elements in the table, such as:

Copy Code code as follows:

function Insert (bag,element)
Bag[element]= (Bag[element] or 0) +1
End

function Remove (bag,element)
Local Count=bag[element]
If Count >0 then bag[element]=count-1
else Bag[element]=nil
End
End

5, Stringbuild

If you concatenate a series of strings into a large string in Lua, there are the following methods:

Low efficiency:

Copy Code code as follows:

Local buff= ""
For line in Io.lines () do
Buff=buff.. Line ... " \ n "
End

High efficiency:

Copy Code code as follows:

Local t={}

For line in Io.lines () do
if (Line==nil) then break end
t[#t +1]=line
End

Local S=table.concat (t, "\ n")--Joins the string in table T

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.