佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > Lua編程示例(三):稀疏表、雙端隊(duì)列、格式化輸出、表和循環(huán)表的格式化輸出

Lua編程示例(三):稀疏表、雙端隊(duì)列、格式化輸出、表和循環(huán)表的格式化輸出

熱門標(biāo)簽:呼叫中心市場(chǎng)需求 Linux服務(wù)器 AI電銷 百度競(jìng)價(jià)排名 鐵路電話系統(tǒng) 服務(wù)外包 網(wǎng)站排名優(yōu)化 地方門戶網(wǎng)站
a={}
for i=1,10 do
 a[i]={}
 for j=0,10 do
 if(i%2==0) then
  a[i][j]=0
 end
 end
end

print(a[9][10])
print(a[10][10])
print()

--雙端隊(duì)列

List={}

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

function List.pushleft(list,value)
 local first= list.first-1
 list[first] = value
 list.first= first
end

function List.pushright(list,value)
 local last = list.last+1
 list[last]= value
 list.last=last
end

function List.popleft(list)
 local first=list.first
 if(first>list.last) then
 error("list is empty")
 end
 local res= list[first]
 list[first]=nil
 list.first=list.first+1
 return res
end

function List.popright(list)
 local last = list.last
 if lastfirst then
 error("the list is empty")
 end
 local res = list[last]
 list[last]= nil
 list.last= list.last-1
 return res
end

function List.display(list)
 if(list.first>list.last) then
 error("the list is empty",2)
 end
 for i=list.first ,list.last do
 print(list[i])
 end
end

mylist=List.new()
List.pushleft(mylist,12)
List.pushleft(mylist,"00")
List.pushright(mylist,34)
List.pushright(mylist,56)
List.display(mylist)
print()

function newStack ()
 return {""}
end


function serialize(o)
 if type(o) == "number" then
 io.write(o)
 elseif type(o) == "string" then
  --不要手動(dòng)加入引號(hào),否則會(huì)有邊際效應(yīng)
 io.write(string.format("%q",o))
 elseif type(o) == "table" then
 io.write("{\n")
 for i,v in pairs(o) do
  io.write("  "..i.." = ")
  serialize(v)
  io.write(",\n")
 end
 io.write("}\n")
 end
end

serialize(123)
print()
serialize("112233")
print()
tab = { a=11,haha="www" ,c=333}
serialize(tab)

function basicSerialize (o)
 if type(o) == "number" then
  return tostring(o)
 else
  return string.format("%q", o)
 end
end

function save (name, value, saved)
 saved = saved or {}  -- 參數(shù)未傳入的初始化
 io.write(name, " = ")
 if type(value) == "number" or type(value) == "string" then
 io.write(basicSerialize(value), "\n")
 elseif type(value) == "table" then
 if saved[value] then
  io.write(saved[value], "\tcircle\n")
 else
  saved[value] = name
  io.write("{}\n")
  for k,v in pairs(value) do
   local fieldname = string.format("%s[%s]", name,
    basicSerialize(k))
   save(fieldname, v, saved)
  end
 end
 else
 error("cannot save a " .. type(value))
 end
end

a = {x=1, y=2; {3,4,5}}
a[2] = a  -- 循環(huán)表
a.z = a[1]  -- 共享子表

save('a',a)

運(yùn)行結(jié)果:

nil
0

00
12
34
56

123
"112233"
{
  a = 11,
  c = 333,
  haha = "www",
}
a = {}
a[1] = {}
a[1][1] = 3
a[1][2] = 4
a[1][3] = 5
a[2] = a circle
a["y"] = 2
a["x"] = 1
a["z"] = a[1] circle

您可能感興趣的文章:
  • Lua編程中使用嵌套循環(huán)的使用教程
  • Lua中的repeat...until循環(huán)語(yǔ)句使用教程
  • 詳解Lua中repeat...until循環(huán)語(yǔ)句的使用方法
  • Lua中for循環(huán)語(yǔ)句的使用教程
  • 詳解Lua中的while循環(huán)語(yǔ)句的使用
  • Lua中數(shù)字for循環(huán)實(shí)例
  • Lua中變相實(shí)現(xiàn)continue跳出循環(huán)
  • Lua中的for循環(huán)和迭代器的秘密探究
  • Lua中三種循環(huán)語(yǔ)句的使用講解

標(biāo)簽:銅川 湖南 蘭州 湘潭 仙桃 崇左 黃山 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Lua編程示例(三):稀疏表、雙端隊(duì)列、格式化輸出、表和循環(huán)表的格式化輸出》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    鄱阳县| 石城县| 哈巴河县| 海盐县| 吉林市| 灵寿县| 南澳县| 信丰县| 盐池县| 应用必备| 信阳市| 万源市| 耒阳市| 土默特右旗| 合阳县| 门源| 滦平县| 东兴市| 维西| 新竹市| 武威市| 克拉玛依市| 九台市| 东海县| 鹤峰县| 禹州市| 绵竹市| 万州区| 奉化市| 乌恰县| 蓬溪县| 丰县| 新建县| 中宁县| 牡丹江市| 阿坝县| 屏东县| 五大连池市| 比如县| 武川县| 花莲县|