-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path118-Stack.js
51 lines (41 loc) · 1.33 KB
/
118-Stack.js
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
// Implementar una pila (stack) en tu lenguaje de programacion preferido - Con nodos
// ----------- Implementacion con Nodos -------------------
class Node {
constructor (data, nextNode) {
this.data = data;
this.next = nextNode;
}
}
class Stack {
constructor() {
this.top = null;
}
// Agrega un elemento a la cima de la pila
push(item) {
let newTop = new Node(item, this.top);
this.top = newTop;
}
// Elimina el primer elemento de la pila, el de la cima y devuelve su valor
pop() {
if (this.isEmpty()) return;
let aux = this.top;
this.top = this.top.next;
return aux.data;
}
// Retorna el elemento de la cima de la pila sin eliminarlo
peek() {
return this.top.data;
}
isEmpty() {
return this.top == null;
}
}
const stack = new Stack();
console.log(stack); // Stack { top: Node { data: undefined, next: undefined } }
stack.push(1);
console.log(stack); // Stack {top: Node { data: 1, next: Node { data: undefined, next: undefined } }}
stack.push(2);
console.log(stack); // Stack { top: Node { data: 2, next: Node { data: 1, next: [Node] } } }
console.log(stack.peek()); // 2
console.log(stack.pop()); // 2
console.log(stack); // top: Node { data: 1, next: Node { data: undefined, next: undefined } }