-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
57 lines (45 loc) · 1.05 KB
/
stack.js
File metadata and controls
57 lines (45 loc) · 1.05 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
46
47
48
49
50
51
52
53
54
55
56
57
// estrutura de pilha
class Stack {
constructor() {
this.items = [];
}
// add um novo elemento no fim da pilha
add(element) {
return this.items.push(element);
}
// remove e exibe o último elemento da pilha
remove() {
if(this.items.length > 0) {
return this.items.pop();
}
}
// exibe o último elemento
lastElement() {
return this.items[this.items.length - 1];
}
// verifica se a pilha está vazia
isEmpty(){
return this.items.length == 0;
}
// retorna o tamanho da pilha
size(){
return this.items.length;
}
// esvazia a pilha
clear(){
this.items = [];
}
}
let stack = new Stack();
stack.add(1);
stack.add(2);
stack.add(4);
stack.add(8);
console.log(stack.items);
stack.remove();
console.log(stack.items);
console.log(stack.lastElement());
console.log(stack.isEmpty());
console.log(stack.size());
stack.clear();
console.log(stack.items);