Skip to content

Commit 0eb32be

Browse files
committed
Adiciona pilha com array
1 parent 36f568c commit 0eb32be

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

examples/charpter04/stack-array.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const StackArray = require("../../src/js/data-structures/stack-array");
2+
3+
const stack = new StackArray();
4+
console.log(stack.isEmpty());
5+
6+
stack.push(5);
7+
stack.push(8);
8+
9+
console.log(stack.peek());
10+
11+
stack.push(11);
12+
console.log(stack.size());
13+
console.log(stack.isEmpty());
14+
15+
stack.push(15);
16+
17+
stack.pop();
18+
console.log(stack.peek());
19+
console.log(stack.size());
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Stack {
2+
constructor() {
3+
this.items = [];
4+
}
5+
6+
/**
7+
* Adiciona elemento no topo da pilha
8+
* @param {*} element
9+
*/
10+
push(element) {
11+
this.items.push(element);
12+
}
13+
14+
/**
15+
* Remove elemento do topo da pila
16+
*/
17+
pop() {
18+
return this.items.pop();
19+
}
20+
21+
/**
22+
* Retorn o topo da pilha
23+
*/
24+
peek() {
25+
return this.items[this.items.length - 1];
26+
}
27+
28+
/**
29+
* Retorn true se a pilha estiver vazia e false caso não esteja
30+
*/
31+
isEmpty() {
32+
return this.items.length == 0;
33+
}
34+
35+
/**
36+
* Limpa a pilha
37+
*/
38+
clear() {
39+
this.items = [];
40+
}
41+
42+
/**
43+
* Retorna o tamanho da pilha
44+
*/
45+
size() {
46+
return this.items.length;
47+
}
48+
}
49+
50+
module.exports = Stack;

0 commit comments

Comments
 (0)