使用对偶表示重新实现Stack(lua程序设计21.7练习21.3题)
作者:互联网
使用对偶表示重新实现Stack(lua程序设计21.7练习21.3题)
local data = {}
DualStack = {}
function DualStack:new(o)
o = o or {}
self.__index =self
setmetatable(o,self)
data[o] = {}
return o
end
function DualStack:push(v)
table.insert(data[self],v)
end
function DualStack:pop()
local v = data[self][#data[self]]
table.remove(data[self],#data[self])
return v
end
function DualStack:top()
local v = data[self][#data[self]]
return v
end
function DualStack:isempty()
return #data[self] == 0
end
local stack = DualStack:new()
for i = 1, 10 do
stack:push(i)
end
print(stack:top())
while not stack:isempty() do
print(stack:pop())
end
标签:function,21.7,对偶表示,end,21.3,self,DualStack,data,stack 来源: https://blog.csdn.net/x402596250/article/details/119111358