forked from tomieiro/std-moon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpilha.lua
More file actions
45 lines (37 loc) · 1.2 KB
/
Copy pathpilha.lua
File metadata and controls
45 lines (37 loc) · 1.2 KB
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
--Definicao padrao dos atributos da Pilha
Pilha = {itens = {}, tamanho = 0, _indexfirst = 1, _indexlast = 1};
--Metodo construtor que instancia o objeto Pilha.
--args: (Table) Atributos desejados para a Pilha.
--return: (Object) Pilha instanciada.
function Pilha:new(atributos)
atributos = atributos or {};
setmetatable(atributos, self);
self.__index = self;
return atributos;
end
--Metodo push que insere um objeto na Pilha.
--args: (Object) Objeto desejados para incluir na Pilha.
function Pilha:push(objeto)
table.insert(self.itens,self._indexlast,objeto);
self.tamanho = self.tamanho + 1;
self._indexlast = self._indexlast + 1;
return;
end
--Metodo pop que remove um objeto da Pilha.
--return: (Object) Objeto removida da Pilha.
function Pilha:pop()
self._indexlast = self._indexlast - 1;
return table.remove(self.itens,self._indexlast + 1);
end
--Metodo para liberar a Pilha
function Pilha:free()
for i=self._indexfirst, self._indexlast do
self.itens[i] = nil;
end
local aux = {__mode = "k"}
setmetatable(self.itens,aux);
setmetatable(self, aux);
self = nil;
collectgarbage();
end
return Pilha;