国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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

2024-07-21 23:04:59
字體:
來源:轉載
供稿:網友
a={}for i=1,10 do a[i]={} for j=0,10 do if(i%2==0) then  a[i][j]=0 end endendprint(a[9][10])print(a[10][10])print()--雙端隊列List={}function List.new() return {first = 0,last = -1}endfunction List.pushleft(list,value) local first= list.first-1 list[first] = value list.first= firstendfunction List.pushright(list,value) local last = list.last+1 list[last]= value list.last=lastendfunction 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 resendfunction List.popright(list) local last = list.last if last<first then error("the list is empty") end local res = list[last] list[last]= nil list.last= list.last-1 return resendfunction 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]) endendmylist=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 {""}endfunction serialize(o) if type(o) == "number" then io.write(o) elseif type(o) == "string" then  --不要手動加入引號,否則會有邊際效應 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") endendserialize(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) endendfunction save (name, value, saved) saved = saved or {}  -- 參數未傳入的初始化 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)) endenda = {x=1, y=2; {3,4,5}}a[2] = a  -- 循環表a.z = a[1]  -- 共享子表save('a',a)

運行結果:

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

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 谢通门县| 延津县| 苏尼特右旗| 玉树县| 四平市| 土默特右旗| 和田市| 磐安县| 邹城市| 乌拉特前旗| 兴文县| 墨脱县| 敖汉旗| 江西省| 从江县| 疏勒县| 洛浦县| 德格县| 廉江市| 子长县| 中牟县| 虎林市| 且末县| 岑巩县| 卢龙县| 天峨县| 龙江县| 长沙县| 合山市| 邵东县| 仪征市| 原阳县| 龙南县| 云浮市| 南昌市| 志丹县| 怀仁县| 阳西县| 温宿县| 游戏| 姜堰市|