lua实现stack(lua程序设计21.7 练习21.1题)
Stack = {}
function Stack:new(o)o = o or {}self.__index = selfsetmetatable(o,self)return o
end
function Stack:push(v)table.insert(self,v)
end
function Stack:pop()local v = self[#self]table.remove(self,#self)return v
end
function Stack:top()local v = self[#self]return v
end
function Stack:isempty()return #self == 0
endlocal stack1 = Stack:new()
for i = 1, 10 dostack1:push(i)
end
print(stack1:top())
while not stack1:isempty() doprint(stack1:pop())
end