练习21.1
stack = {}
function stack:new()
o = {first = 0}
self.__index = self
setmetatable(o,self)
return o
end
function stack:push(value)
local first = self.first - 1
self.first = first
self[first] = value
end
function stack:pop()
local first = self.first
if first > -1 then
print("stack is empty")
return
end
local value = self[first]
self[first] = nil
self.first = first + 1
return value
end
function stack:isempty()
return self.first > -1
end
mystack = stack:new()
mystack:push(1)
print(mystack:pop())
print(mystack:isempty())
练习21.2
stack = {first = 0}
function stack:new(o)
o = o or{}
self.__index = self
setmetatable(o,self)
return o
end
function stack:push(value)
local first = self.first - 1
self.first = first
self[first] = value
end
function stack:pop()
local first = self.