File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change
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 ( ) ) ;
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments