稀疏表、双端队列、格式化输出、表和循环表的格式化输出

  1. a={}  
  2. for i=1,10 do  
  3.     a[i]={}  
  4.     for j=0,10 do  
  5.         if(i%2==0) then  
  6.             a[i][j]=0  
  7.         end  
  8.     end  
  9. end  
  10.   
  11. print(a[9][10])  
  12. print(a[10][10])  
  13. print()  
  14.   
  15. --双端队列  
  16.   
  17. List={}  
  18.   
  19. function List.new()  
  20.     return {first = 0,last = -1}  
  21. end  
  22.   
  23. function List.pushleft(list,value)  
  24.     local first= list.first-1  
  25.     list[first] = value  
  26.     list.first= first  
  27. end  
  28.   
  29. function List.pushright(list,value)  
  30.     local last = list.last+1  
  31.     list[last]= value  
  32.     list.last=last  
  33. end  
  34.   
  35. function List.popleft(list)  
  36.     local first=list.first  
  37.     if(first>list.last) then  
  38.         error("list is empty")  
  39.     end  
  40.     local res= list[first]  
  41.     list[first]=nil  
  42.     list.first=list.first+1  
  43.     return res  
  44. end  
  45.   
  46. function List.popright(list)  
  47.     local last = list.last  
  48.     if last<first then  
  49.         error("the list is empty")  
  50.     end  
  51.     local res = list[last]  
  52.     list[last]= nil  
  53.     list.last= list.last-1  
  54.     return res  
  55. end  
  56.   
  57. function List.display(list)  
  58.     if(list.first>list.last) then  
  59.         error("the list is empty",2)  
  60.     end  
  61.     for i=list.first ,list.last do  
  62.         print(list[i])  
  63.     end  
  64. end  
  65.   
  66. mylist=List.new()  
  67. List.pushleft(mylist,12)  
  68. List.pushleft(mylist,"00")  
  69. List.pushright(mylist,34)  
  70. List.pushright(mylist,56)  
  71. List.display(mylist)  
  72. print()  
  73.   
  74. function newStack ()  
  75.   return {""}  
  76. end  
  77.   
  78.   
  79. function serialize(o)  
  80.     if type(o) == "number" then  
  81.         io.write(o)  
  82.     elseif type(o) == "string" then  
  83.       --不要手动加入引号,否则会有边际效应  
  84.         io.write(string.format("%q",o))  
  85.     elseif type(o) == "table" then  
  86.         io.write("{\n")  
  87.         for i,v in pairs(o) do  
  88.             io.write("    "..i.." = ")  
  89.             serialize(v)  
  90.             io.write(",\n")  
  91.         end  
  92.         io.write("}\n")  
  93.     end  
  94. end  
  95.   
  96. serialize(123)  
  97. print()  
  98. serialize("112233")  
  99. print()  
  100. tab = { a=11,haha="www" ,c=333}  
  101. serialize(tab)  
  102.   
  103. function basicSerialize (o)  
  104.   if type(o) == "number" then  
  105.    return tostring(o)  
  106.   else  
  107.    return string.format("%q", o)  
  108.   end  
  109. end  
  110.   
  111. function save (name, value, saved)  
  112.     saved = saved or {}    -- 参数未传入的初始化  
  113.     io.write(name, " = ")  
  114.     if type(value) == "number"  or type(value) == "string"  then  
  115.         io.write(basicSerialize(value), "\n")  
  116.     elseif type(value) == "table" then  
  117.         if saved[value] then  
  118.             io.write(saved[value], "\tcircle\n")  
  119.         else  
  120.             saved[value] = name  
  121.             io.write("{}\n")  
  122.                 for k,v in pairs(value) do  
  123.                     local fieldname = string.format("%s[%s]", name,  
  124.                                 basicSerialize(k))  
  125.                     save(fieldname, v, saved)  
  126.                 end  
  127.         end  
  128.     else  
  129.         error("cannot save a " .. type(value))  
  130.     end  
  131. end  
  132.   
  133. a = {x=1, y=2; {3,4,5}}  
  134. a[2] = a   -- 循环表  
  135. a.z = a[1]    -- 共享子表  
  136.   
  137. save('a',a)  

 

运行结果:

  1. nil  
  2. 0  
  3.   
  4. 00  
  5. 12  
  6. 34  
  7. 56  
  8.   
  9. 123  
  10. "112233"  
  11. {  
  12.     a = 11,  
  13.     c = 333,  
  14.     haha = "www",  
  15. }  
  16. a = {}  
  17. a[1] = {}  
  18. a[1][1] = 3  
  19. a[1][2] = 4  
  20. a[1][3] = 5  
  21. a[2] = a    circle  
  22. a["y"] = 2  
  23. a["x"] = 1  
  24. a["z"] = a[1]   circle  

 

本篇博客出自  阿修罗道,转载请注明出处:http://blog.youkuaiyun.com/fansongy/article/details/7007371

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值