Lua Common Data Structure

Source: Internet
Author: User

The table in Lua is not a simple data structure. It can be used as the basis of other data structures. Such as arrays, records, linear tables, queues, and sets, which can be expressed by tables in Lua.

I. Array

In Lua, you can access the elements in the table through the integer subscript to implement arrays. In addition, you do not need to specify the size of the array. The size can dynamically increase as needed.

a = {}for i = 1,100 doa[i] = 0endprint("The length of array ‘a‘ is " .. #a)squares = {1, 4, 9, 16, 25}print("The length of array ‘a‘ is " .. #squares)

In Lua, the following table of the array is used to start from 1, and the standard library of Lua is consistent with this habit, therefore, if your array subscript is also from 1, you can directly use the function of the standard library, otherwise you will not be able to directly use it.

2. Two-dimensional array

There are two main methods to represent a matrix in Lua. The first method is to represent an array. That is to say, the element of a table is another table.

local N = 3local M = 3mt = {}for i = 1,N domt[i] = {}for j = 1,M domt[i][j] = i * jendendmt = {}for i = 1, N dofor j = 1, M domt[(i - 1) * M + j] = i * jendend

Iii. Linked List

In Lua, tables is used to easily implement a linked list. Each node is a table, a pointer is a field of the table, and points to another node (table ). For example, to implement a basic linked list with only two fields: values and pointers, the Code is as follows:

list = nilfor i = 1, 10 dolist = { next = list ,value = i}endlocal l = listwhile l do --print(l.value)l = l.nextend

4. queue and bidirectional queue

Although the insert and remove operations provided by the Lua table database can be used to implement the queue, the queue implementation in this way has a low aging rate for large data volumes. The effective method is to use two index subscripts, one represents the first element, and the other represents the last element.

List = {} -- create a function list. new () return {First = 0, last =-1} end -- queue header inserted function list. pushfront (list, value) local First = List. first-1list. first = firstlist [first] = valueend -- insert function list at the end of the queue. popfront (list) local First = List. firstif first> list. last thenerror ("list is empty") endlocal value = list [first] list [first] = nillist. first = first + 1 return valueendfunction list. popback (list) Local last = List. lastif list. first> last thenerror ("list is empty") endlocal value = list [last] list [last] = nillist. last = last-1 return valueend -- test code: Local testlist = {First = 0, last =-1} Local tabletest = 12list. pushfront (testlist, tabletest) print (list. popfront (testlist ))

V. Stack

The Code is as follows:

Local stackmng = {} stackmng. _ Index = stackmngfunction stackmng: New () Local temp = {} setretriable (temp, stackmng) return tempendfunction stackmng: Init () self. stacklist = {} endfunction stackmng: reset () Self: Init () endfunction stackmng: clear () self. stacklist = {} endfunction stackmng: Pop () If # self. stacklist = 0 thenreturnendif self. stacklist [1] thenprint (self. stacklist [1]) endreturn table. remove (self. stacklist, 1) endfunction stackmng: Push (t) table. insert (self. stacklist, T) endfunction stackmng: Count () return # self. stacklistend -- test code object = stackmng: New () object: Init () object: Push (1) object: Pop ()

Vi. Set

Using table in Lua to implement a set is very simple. See the following code:

reserved = {["while"] = true, ["end"] = true,["function"] = true, ["local"] = true,}for k,v in pairs(reserved) doprint(k,"->",v)end

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: 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.