-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (106 loc) · 2.46 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* eslint-disable no-console */
/* eslint-disable max-classes-per-file */
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
if (this.isEmpyt()) {
return 'La pila esta vacia';
}
return this.items.pop();
}
isEmpyt() {
return this.items.length === 0;
}
peek() {
return this.items[this.items.length - 1];
}
clear() {
this.items = [];
}
size() {
return this.items.length;
}
}
class Queue {
constructor() {
this.items = [];
}
enqueue(element) {
this.items.push(element);
}
dequeue() {
if (this.isEmpyt()) {
return 'La cola esta vacia';
}
return this.items.shift();
}
isEmpyt() {
return this.items.length === 0;
}
size() {
return this.items.length;
}
}
const pila = new Stack();
pila.push(1);
pila.push(2);
pila.push(3);
console.log(pila.pop()); // 3
console.log(pila.pop()); // 2
const cola = new Queue();
cola.enqueue('a');
cola.enqueue('b');
cola.enqueue('c');
console.log(cola.dequeue()); // a
console.log(cola.dequeue()); // b
class Navegador {
constructor() {
this.history = new Stack();
this.future = new Stack();
this.currentPage = null;
}
// navegar a una nueva pagina
goToPage(page) {
if (this.currentPage !== null) {
this.history.push(this.currentPage);
}
this.currentPage = page;
this.future.clear();
console.log('Pagina actual: ', this.currentPage);
}
// retroceder a la pagina anterior
goBack() {
if (this.history.isEmpyt()) {
console.log('No hay paginas anteriores');
return;
}
this.future.push(this.currentPage);
this.currentPage = this.history.pop();
console.log('Pagina actual: ', this.currentPage);
}
// avanzar a la pagina siguiente
goForward() {
if (this.future.isEmpyt()) {
console.log('No hay paginas siguientes');
return;
}
this.history.push(this.currentPage);
this.currentPage = this.future.pop();
console.log('Pagina actual: ', this.currentPage);
}
}
const navegador = new Navegador();
navegador.goToPage('google.com');
navegador.goToPage('facebook.com');
navegador.goToPage('twitter.com');
navegador.goBack(); // regresa a facebook.com
navegador.goBack(); // regresa a google.com
navegador.goForward(); // avanza a facebook.com
navegador.goToPage('instagram.com'); // instagram.com
navegador.goBack(); // regresa a facebook.com
navegador.goForward(); // avanza a instagram.com