Pilas & Colas
Implementación con colas
import { SimpleNode } from "./node";
export class Queue<T> {
head: SimpleNode<T> | null;
tail: SimpleNode<T> | null;
constructor() {
this.head = null;
this.tail = null;
}
enqueue(data: T) {
if (!this.head || !this.tail) {
this.head = new SimpleNode(data);
this.tail = this.head;
return;
}
let newTail = new SimpleNode(data);
this.tail.next = newTail;
this.tail = newTail;
}
dequeue(): T | null {
if (!this.head || !this.tail) {
return null;
}
const data = this.head.data;
this.head = this.head.next;
return data;
}
clear() {
this.head = null;
this.tail = null;
}
find(predicate: (data: T) => boolean): T | null {
let current = this.head;
while (current) {
if (predicate(current.data)) {
return current.data;
}
current = current.next;
}
return null;
}
toArray(): Array<T> {
const arr: Array<T> = [];
let current = this.head;
while (current) {
arr.push(current.data);
current = current.next;
}
return arr;
}
}
| ID | CODIGO | NOMBRE | APELLIDO | SEXO | SUELDO |
|---|
Implementación con pilas
import { SimpleNode } from "./node";
export class Stack<T> {
head: SimpleNode<T> | null;
constructor() {
this.head = null;
}
push(data: T) {
if (!this.head) {
this.head = new SimpleNode(data)
return;
}
let newHead = new SimpleNode(data);
newHead.next = this.head;
this.head = newHead;
}
pop(): T | null {
if (!this.head) {
return null;
}
const data = this.head.data;
this.head = this.head.next;
return data;
}
clear() {
this.head = null;
}
find(predicate: (data: T) => boolean): T | null {
let current = this.head;
while (current) {
if (predicate(current.data)) {
return current.data;
}
current = current.next;
}
return null;
}
toArray(): Array<T> {
const arr: Array<T> = [];
let current = this.head;
while (current) {
arr.push(current.data);
current = current.next;
}
return arr;
}
}
| ID | CODIGO | NOMBRE | APELLIDO | SEXO | SUELDO |
|---|
——
Ejercicio Propuesto
Construya la solución del siguiente aplicativo usando Pilas y Colas. Su programa también tendrá la opción de consultar, actualizar y eliminar registros. Construya un aplicativo que permita guardar en una lista la información: Nombres, Apellido Paterno, Apellido Materno, Sueldo base, Ventas realizadas, Estado civil, Número de Hijos y Sueldo Neto.
El programa debe calcular para cada empleado el sueldo neto mediante la relación siguiente:
Sueldo Neto = Sueldo base + Comisión por ventas - Descuento por seguro - Descuento por impuesto
Donde: la comisión por ventas es el 5% de las ventas realizadas. El descuento por seguro se obtiene como sigue:
- Si el empleado es soltero el descuento es de S/. 100.
- Si el empleado es casado sin hijos el descuento es de S/. 120.
- Si el empleado es casado con hijos el descuento es de S/. 50 + S/. 70 por cada hijo.
Suponga que llamamos TA a la suma del Sueldo base + comisión por ventas, entonces el Descuento por el Impuesto (DI) se obtiene como sigue:
| Rango del TA | Monto de DI |
|---|---|
| 0 a 1500 | 0 |
| 1500 a 2300 | 3% del TA |
| 2301 a 3000 | 4% del TA |
| 3001 a más | 6% del TA |
El programa debe mostrar el monto total que la empresa debe pagar por concepto de sueldos, el monto total por comisiones de ventas, el monto total de los descuentos por impuesto y por seguro.
Desarrollo
El ejercicio se ha implementado 2 veces, usando pilas y colas:
Con stacks
| ID | NOMBRE | SUELDO | VENTAS | E_CIVIL | HIJOS | COMISION | DCTO_SEGURO | IMPUESTOS | SUELDO_NETO |
|---|
Con queues
| ID | NOMBRE | SUELDO | VENTAS | E_CIVIL | HIJOS | COMISION | DCTO_SEGURO | IMPUESTOS | SUELDO_NETO |
|---|
——
Go back