Skip to content

Commit fe57eb6

Browse files
committed
#7 javascript
1 parent 115c058 commit fe57eb6

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
const readline = require('readline');
2+
3+
// Implementación de la Pila (Stack - LIFO)
4+
class Stack {
5+
constructor() {
6+
this.items = [];
7+
}
8+
9+
// Método para añadir un elemento a la pila
10+
push(element) {
11+
this.items.push(element);
12+
}
13+
14+
// Método para quitar un elemento de la pila
15+
pop() {
16+
if (this.isEmpty()) {
17+
console.log("Error: Pila vacía");
18+
return null;
19+
}
20+
return this.items.pop();
21+
}
22+
23+
// Método para verificar si la pila está vacía
24+
isEmpty() {
25+
return this.items.length === 0;
26+
}
27+
28+
// Método para obtener el elemento en la cima de la pila sin quitarlo
29+
peek() {
30+
if (this.isEmpty()) {
31+
console.log("Error: Pila vacía");
32+
return null;
33+
}
34+
return this.items[this.items.length - 1];
35+
}
36+
}
37+
38+
// Implementación de la Cola (Queue - FIFO)
39+
class Queue {
40+
constructor() {
41+
this.items = [];
42+
}
43+
44+
// Método para añadir un elemento a la cola
45+
enqueue(element) {
46+
this.items.push(element);
47+
}
48+
49+
// Método para quitar un elemento de la cola
50+
dequeue() {
51+
if (this.isEmpty()) {
52+
console.log("Error: Cola vacía");
53+
return null;
54+
}
55+
return this.items.shift();
56+
}
57+
58+
// Método para verificar si la cola está vacía
59+
isEmpty() {
60+
return this.items.length === 0;
61+
}
62+
63+
// Método para obtener el primer elemento de la cola sin quitarlo
64+
peek() {
65+
if (this.isEmpty()) {
66+
console.log("Error: Cola vacía");
67+
return null;
68+
}
69+
return this.items[0];
70+
}
71+
}
72+
73+
// Función para el simulador de navegador web
74+
function webBrowserSimulator() {
75+
const backStack = new Stack();
76+
const forwardStack = new Stack();
77+
let currentPage = "";
78+
79+
const rl = readline.createInterface({
80+
input: process.stdin,
81+
output: process.stdout
82+
});
83+
84+
console.log("Simulador de navegador web (escribe 'salir' para terminar):");
85+
86+
function askForAction() {
87+
rl.question("Ingresa una acción o nombre de página web: ", (input) => {
88+
input = input.trim().toLowerCase();
89+
90+
if (input === 'salir') {
91+
rl.close();
92+
printerSimulator();
93+
} else if (input === 'atrás') {
94+
if (!backStack.isEmpty()) {
95+
forwardStack.push(currentPage);
96+
currentPage = backStack.pop();
97+
console.log("Página actual:", currentPage);
98+
} else {
99+
console.log("No hay páginas anteriores");
100+
}
101+
} else if (input === 'adelante') {
102+
if (!forwardStack.isEmpty()) {
103+
backStack.push(currentPage);
104+
currentPage = forwardStack.pop();
105+
console.log("Página actual:", currentPage);
106+
} else {
107+
console.log("No hay páginas siguientes");
108+
}
109+
} else {
110+
if (currentPage) {
111+
backStack.push(currentPage);
112+
}
113+
forwardStack = new Stack(); // Limpia la pila de adelante
114+
currentPage = input;
115+
console.log("Página actual:", currentPage);
116+
}
117+
118+
askForAction();
119+
});
120+
}
121+
122+
askForAction();
123+
}
124+
125+
// Función para el simulador de impresora compartida
126+
function printerSimulator() {
127+
const printQueue = new Queue();
128+
129+
const rl = readline.createInterface({
130+
input: process.stdin,
131+
output: process.stdout
132+
});
133+
134+
console.log("\nSimulador de impresora compartida (escribe 'salir' para terminar):");
135+
136+
function askForDocument() {
137+
rl.question("Ingresa un nombre de documento o 'imprimir': ", (input) => {
138+
input = input.trim().toLowerCase();
139+
140+
if (input === 'salir') {
141+
rl.close();
142+
return;
143+
} else if (input === 'imprimir') {
144+
const document = printQueue.dequeue();
145+
if (document) {
146+
console.log("Imprimiendo:", document);
147+
}
148+
} else {
149+
printQueue.enqueue(input);
150+
console.log("Documento añadido a la cola:", input);
151+
}
152+
153+
askForDocument();
154+
});
155+
}
156+
157+
askForDocument();
158+
}
159+
160+
// Iniciar el simulador de navegador web
161+
webBrowserSimulator();

0 commit comments

Comments
 (0)