Lua编程示例(三):稀疏表、双端队列、格式化输出、表和循环表的格式化输出

时间:2022-06-01 20:11:13
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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()
 
--双端队列
 
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 last<first 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
  --不要手动加入引号,否则会有边际效应
 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 {}  -- 参数未传入的初始化
 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  -- 循环表
a.z = a[1]  -- 共享子表
 
save('a',a)

运行结果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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