-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.ts
132 lines (111 loc) · 2.82 KB
/
solution.ts
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
125
126
127
128
129
130
131
132
/* eslint-disable no-console */
/* eslint-disable max-classes-per-file */
class Stack<T> {
private items: T[];
constructor() {
this.items = [];
}
push(element: T): void {
this.items.push(element);
}
pop(): T | string {
if (this.isEmpty()) {
return 'La pila está vacía';
}
return this.items.pop() as T;
}
isEmpty(): boolean {
return this.items.length === 0;
}
peek(): T | undefined {
return this.items[this.items.length - 1];
}
clear(): void {
this.items = [];
}
size(): number {
return this.items.length;
}
}
class Queue<T> {
private items: T[];
constructor() {
this.items = [];
}
enqueue(element: T): void {
this.items.push(element);
}
dequeue(): T | string {
if (this.isEmpty()) {
return 'La cola está vacía';
}
return this.items.shift() as T;
}
isEmpty(): boolean {
return this.items.length === 0;
}
size(): number {
return this.items.length;
}
}
const pila = new Stack<number>();
pila.push(1);
pila.push(2);
pila.push(3);
console.log(pila.pop()); // 3
console.log(pila.pop()); // 2
const cola = new Queue<string>();
cola.enqueue('a');
cola.enqueue('b');
cola.enqueue('c');
console.log(cola.dequeue()); // a
console.log(cola.dequeue()); // b
class Navegador {
private history: Stack<string>;
private future: Stack<string>;
private currentPage: string | null;
constructor() {
this.history = new Stack<string>();
this.future = new Stack<string>();
this.currentPage = null;
}
// navegar a una nueva página
goToPage(page: string): void {
if (this.currentPage !== null) {
this.history.push(this.currentPage);
}
this.currentPage = page;
this.future.clear();
console.log('Página actual: ', this.currentPage);
}
// retroceder a la página anterior
goBack(): void {
if (this.history.isEmpty()) {
console.log('No hay páginas anteriores');
return;
}
this.future.push(this.currentPage as string);
this.currentPage = this.history.pop() as string;
console.log('Página actual: ', this.currentPage);
}
// avanzar a la página siguiente
goForward(): void {
if (this.future.isEmpty()) {
console.log('No hay páginas siguientes');
return;
}
this.history.push(this.currentPage as string);
this.currentPage = this.future.pop() as string;
console.log('Página 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