|
| 1 | +/* |
| 2 | + * EJERCICIO: |
| 3 | + * Implementa los mecanismos de introducción y recuperación de elementos propios de las |
| 4 | + * pilas (stacks - LIFO) y las colas (queue - FIFO) utilizando una estructura de array |
| 5 | + * o lista en javascript. |
| 6 | + */ |
| 7 | + |
| 8 | +// Implementación de una pila (stack - LIFO) |
| 9 | +/*function Stack() { |
| 10 | + // Atributos |
| 11 | + this.stack = []; |
| 12 | + |
| 13 | + // Añadir un elemento a la pila |
| 14 | + this.push = function (element) { |
| 15 | + this.stack.push(element); |
| 16 | + } |
| 17 | +
|
| 18 | + // Eliminar un elemento de la pila |
| 19 | + this.pop = function () { |
| 20 | + return this.stack.pop(); |
| 21 | + } |
| 22 | +
|
| 23 | + // Devolver el elemento que está en la cima de la pila |
| 24 | + this.peek = function () { |
| 25 | + return this.stack[this.stack.length - 1]; |
| 26 | + } |
| 27 | +
|
| 28 | + // Devolver si la pila está vacía |
| 29 | + this.isEmpty = function () { |
| 30 | + return this.stack.length === 0; |
| 31 | + } |
| 32 | + |
| 33 | + // Devolver el tamaño de la pila |
| 34 | + this.size = function () { |
| 35 | + return this.stack.length; |
| 36 | + } |
| 37 | + |
| 38 | + // Imprimir la pila |
| 39 | + this.print = function () { |
| 40 | + console.log(this.stack); |
| 41 | + } |
| 42 | + |
| 43 | + // Limpiar la pila |
| 44 | + this.clear = function () { |
| 45 | + this.stack = []; |
| 46 | + } |
| 47 | +
|
| 48 | +
|
| 49 | +} |
| 50 | +
|
| 51 | +// Implementación de una cola (queue - FIFO) |
| 52 | +function Queue() { |
| 53 | + // Atributos de la cola (queue) |
| 54 | + this.queue = []; |
| 55 | +
|
| 56 | + // Métodos de la cola (queue) |
| 57 | + this.enqueue = function (element) { |
| 58 | + // Añadir un elemento a la cola |
| 59 | + this.queue.push(element); |
| 60 | + } |
| 61 | +
|
| 62 | + // Eliminar un elemento de la cola |
| 63 | + this.dequeue = function () { |
| 64 | + return this.queue.shift(); |
| 65 | + } |
| 66 | +
|
| 67 | + // Devolver el primer elemento de la cola |
| 68 | + this.front = function () { |
| 69 | + return this.queue[0]; |
| 70 | + } |
| 71 | +
|
| 72 | + // Devolver si la cola está vacía |
| 73 | + this.isEmpty = function () { |
| 74 | + return this.queue.length === 0; |
| 75 | + } |
| 76 | +
|
| 77 | + // Devolver el tamaño de la cola |
| 78 | + this.size = function () { |
| 79 | + return this.queue.length; |
| 80 | + } |
| 81 | +
|
| 82 | + // Imprimir la cola |
| 83 | + this.print = function () { |
| 84 | + console.log(this.queue); |
| 85 | + } |
| 86 | + |
| 87 | + // Limpiar la cola |
| 88 | + this.clear = function () { |
| 89 | + this.queue = []; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// Pruebas pila |
| 94 | +var stack = new Stack(); |
| 95 | +stack.push(1); |
| 96 | +stack.push(2); |
| 97 | +stack.push(3); |
| 98 | +stack.print(); |
| 99 | +console.log(stack.pop()); |
| 100 | +console.log(stack.peek()); |
| 101 | +console.log(stack.isEmpty()); |
| 102 | +console.log(stack.size()); |
| 103 | +stack.print(); |
| 104 | +stack.clear(); |
| 105 | +stack.print(); |
| 106 | +
|
| 107 | +// Pruebas cola |
| 108 | +var queue = new Queue(); |
| 109 | +queue.enqueue(1); |
| 110 | +queue.enqueue(2); |
| 111 | +queue.enqueue(3); |
| 112 | +queue.print(); |
| 113 | +console.log(queue.dequeue()); |
| 114 | +console.log(queue.front()); |
| 115 | +console.log(queue.isEmpty()); |
| 116 | +console.log(queue.size()); |
| 117 | +queue.print(); |
| 118 | +queue.clear(); |
| 119 | +queue.print();/* |
| 120 | +
|
| 121 | +/* DIFICULTAD EXTRA (opcional): |
| 122 | + * - Utilizando la implementación de pila y cadenas de texto, simula el mecanismo adelante/atrás |
| 123 | + * de un navegador web. Crea un programa en el que puedas navegar a una página o indicarle |
| 124 | + * que te quieres desplazar adelante o atrás, mostrando en cada caso el nombre de la web. |
| 125 | + * Las palabras "adelante", "atrás" desencadenan esta acción, el resto se interpreta como |
| 126 | + * el nombre de una nueva web. |
| 127 | + */ |
| 128 | +/*const readline = require("readline"); |
| 129 | +
|
| 130 | +class Browser { |
| 131 | + constructor() { |
| 132 | + this.historyStack = []; // Pila para el historial hacia atrás |
| 133 | + this.forwardStack = []; // Pila para el historial hacia adelante |
| 134 | + this.currentPage = null; // Página actual |
| 135 | + } |
| 136 | +
|
| 137 | + navigateTo(page) { |
| 138 | + if (this.currentPage) { |
| 139 | + this.historyStack.push(this.currentPage); |
| 140 | + } |
| 141 | + this.currentPage = page; |
| 142 | + this.forwardStack = []; |
| 143 | + console.log(`Navegando a: ${this.currentPage}`); |
| 144 | + } |
| 145 | +
|
| 146 | + goBack() { |
| 147 | + if (this.historyStack.length === 0) { |
| 148 | + console.log("No hay más páginas hacia atrás."); |
| 149 | + return; |
| 150 | + } |
| 151 | + this.forwardStack.push(this.currentPage); |
| 152 | + this.currentPage = this.historyStack.pop(); |
| 153 | + console.log(`Retrocediendo a: ${this.currentPage}`); |
| 154 | + } |
| 155 | +
|
| 156 | + goForward() { |
| 157 | + if (this.forwardStack.length === 0) { |
| 158 | + console.log("No hay más páginas hacia adelante."); |
| 159 | + return; |
| 160 | + } |
| 161 | + this.historyStack.push(this.currentPage); |
| 162 | + this.currentPage = this.forwardStack.pop(); |
| 163 | + console.log(`Avanzando a: ${this.currentPage}`); |
| 164 | + } |
| 165 | +
|
| 166 | + printStatus() { |
| 167 | + console.log(`Página actual: ${this.currentPage || "Ninguna"}`); |
| 168 | + console.log(`Historial atrás: [${this.historyStack.join(", ")}]`); |
| 169 | + console.log(`Historial adelante: [${this.forwardStack.join(", ")}]`); |
| 170 | + } |
| 171 | +} |
| 172 | +
|
| 173 | +// Configuración del intérprete |
| 174 | +const rl = readline.createInterface({ |
| 175 | + input: process.stdin, |
| 176 | + output: process.stdout, |
| 177 | +}); |
| 178 | +
|
| 179 | +const browser = new Browser(); |
| 180 | +
|
| 181 | +function showMenu() { |
| 182 | + console.log("\nAcciones disponibles:"); |
| 183 | + console.log("1. Navegar a una página (escribe el nombre de la página)."); |
| 184 | + console.log("2. Ir atrás (escribe 'atrás')."); |
| 185 | + console.log("3. Ir adelante (escribe 'adelante')."); |
| 186 | + console.log("4. Mostrar estado (escribe 'estado')."); |
| 187 | + console.log("5. Salir (escribe 'salir').\n"); |
| 188 | +} |
| 189 | +
|
| 190 | +function processInput(input) { |
| 191 | + input = input.trim().toLowerCase(); |
| 192 | +
|
| 193 | + if (input === "salir") { |
| 194 | + console.log("¡Adiós!"); |
| 195 | + rl.close(); |
| 196 | + return; |
| 197 | + } |
| 198 | +
|
| 199 | + if (input === "atrás") { |
| 200 | + browser.goBack(); |
| 201 | + } else if (input === "adelante") { |
| 202 | + browser.goForward(); |
| 203 | + } else if (input === "estado") { |
| 204 | + browser.printStatus(); |
| 205 | + } else if (input) { |
| 206 | + browser.navigateTo(input); |
| 207 | + } else { |
| 208 | + console.log("Entrada no válida."); |
| 209 | + } |
| 210 | +
|
| 211 | + startInteraction(); // Volvemos a mostrar el menú |
| 212 | +} |
| 213 | +
|
| 214 | +function startInteraction() { |
| 215 | + showMenu(); |
| 216 | + rl.question("¿Qué deseas hacer? ", processInput); |
| 217 | +} |
| 218 | +
|
| 219 | +// Iniciar la interacción |
| 220 | +console.log("¡Bienvenido al navegador interactivo!"); |
| 221 | +startInteraction();*/ |
| 222 | + |
| 223 | +/*DIFICULTAD EXTRA (opcional): |
| 224 | + * - Utilizando la implementación de cola y cadenas de texto, simula el mecanismo de una |
| 225 | + * impresora compartida que recibe documentos y los imprime cuando así se le indica. |
| 226 | + * La palabra "imprimir" imprime un elemento de la cola, el resto de palabras se |
| 227 | + * interpretan como nombres de documentos. |
| 228 | + */ |
| 229 | + |
| 230 | +/*class Impresora { |
| 231 | + constructor(){ |
| 232 | + this.queue = new Queue(); |
| 233 | + } |
| 234 | + //Añadir un documento a la cola |
| 235 | + addDocument(document){ |
| 236 | + this.queue.enqueue(document); |
| 237 | + console.log(`Documento añadido: ${document}`); |
| 238 | + } |
| 239 | + //Imprimir un documento |
| 240 | + printDocument(){ |
| 241 | + if(this.queue.isEmpty()){ |
| 242 | + console.log("No hay documentos para imprimir."); |
| 243 | + return; |
| 244 | + } |
| 245 | + const document = this.queue.dequeue(); |
| 246 | + console.log(`Imprimiendo documento: ${document}`); |
| 247 | + } |
| 248 | + //Mostrar el estado actual |
| 249 | + printStatus(){ |
| 250 | + console.log(`Documentos en cola: [${this.queue.queue.join(", ")}]`); |
| 251 | + } |
| 252 | +} |
| 253 | +
|
| 254 | +const impresora = new Impresora(); |
| 255 | +impresora.addDocument("Documento1"); |
| 256 | +impresora.addDocument("Documento2"); |
| 257 | +impresora.addDocument("Documento3"); |
| 258 | +impresora.printStatus(); |
| 259 | +impresora.printDocument(); |
| 260 | +impresora.printStatus(); |
| 261 | +impresora.printDocument();*/ |
| 262 | + |
| 263 | +const readline = require("readline"); |
| 264 | +class PrinterQueue { |
| 265 | + constructor() { |
| 266 | + this.queue = []; // Cola para los documentos |
| 267 | + } |
| 268 | + |
| 269 | + addDocument(document) { |
| 270 | + this.queue.push(document); |
| 271 | + console.log(`Documento "${document}" añadido a la cola.`); |
| 272 | + } |
| 273 | + |
| 274 | + printDocument() { |
| 275 | + if (this.queue.length === 0) { |
| 276 | + console.log("No hay documentos en la cola para imprimir."); |
| 277 | + } else { |
| 278 | + const document = this.queue.shift(); |
| 279 | + console.log(`Imprimiendo: "${document}"`); |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + showQueue() { |
| 284 | + if (this.queue.length === 0) { |
| 285 | + console.log("La cola de impresión está vacía."); |
| 286 | + } else { |
| 287 | + console.log(`Documentos en la cola: [${this.queue.join(", ")}]`); |
| 288 | + } |
| 289 | + } |
| 290 | +} |
| 291 | + |
| 292 | +// Configuración del intérprete |
| 293 | +const rl = readline.createInterface({ |
| 294 | + input: process.stdin, |
| 295 | + output: process.stdout, |
| 296 | +}); |
| 297 | + |
| 298 | +const printer = new PrinterQueue(); |
| 299 | + |
| 300 | +function showMenu() { |
| 301 | + console.log("\nAcciones disponibles:"); |
| 302 | + console.log("1. Añadir un documento (escribe el nombre del documento)."); |
| 303 | + console.log("2. Imprimir un documento (escribe 'imprimir')."); |
| 304 | + console.log("3. Ver la cola de impresión (escribe 'cola')."); |
| 305 | + console.log("4. Salir (escribe 'salir').\n"); |
| 306 | +} |
| 307 | + |
| 308 | +function processInput(input) { |
| 309 | + input = input.trim().toLowerCase(); |
| 310 | + |
| 311 | + if (input === "salir") { |
| 312 | + console.log("Cerrando impresora. ¡Adiós!"); |
| 313 | + rl.close(); |
| 314 | + return; |
| 315 | + } |
| 316 | + |
| 317 | + if (input === "imprimir") { |
| 318 | + printer.printDocument(); |
| 319 | + } else if (input === "cola") { |
| 320 | + printer.showQueue(); |
| 321 | + } else if (input) { |
| 322 | + printer.addDocument(input); |
| 323 | + } else { |
| 324 | + console.log("Entrada no válida."); |
| 325 | + } |
| 326 | + |
| 327 | + startInteraction(); // Volvemos a mostrar el menú |
| 328 | +} |
| 329 | + |
| 330 | +function startInteraction() { |
| 331 | + showMenu(); |
| 332 | + rl.question("¿Qué deseas hacer? ", processInput); |
| 333 | +} |
| 334 | + |
| 335 | +// Iniciar la interacción |
| 336 | +console.log("¡Bienvenido a la impresora compartida!"); |
| 337 | +startInteraction(); |
| 338 | + |
0 commit comments