Skip to content

Commit 9390307

Browse files
authored
Merge pull request mouredev#4885 from julian98789/patch-11
#8 - java
2 parents 8ec8d0a + 5731bfd commit 9390307

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
2+
import java.util.ArrayList;
3+
import java.util.List;
4+
5+
public class reto_8 {
6+
private String nombre;
7+
private int edad;
8+
private String direccion;
9+
10+
public reto_8(String nombre, int edad, String direccion) {
11+
this.nombre = nombre;
12+
this.edad = edad;
13+
this.direccion = direccion;
14+
}
15+
16+
public void imprimir() {
17+
System.out.println("Nombre: " + nombre);
18+
System.out.println("Edad: " + edad);
19+
System.out.println("Dirección: " + direccion);
20+
}
21+
22+
// Implementación de la pila (stack)
23+
class Stack<T> {
24+
private List<T> elementos = new ArrayList<>();
25+
26+
// Agregar elementos
27+
public void agregar(T elemento) {
28+
elementos.add(elemento);
29+
}
30+
31+
public void eliminar() {
32+
if (elementos.isEmpty()) {
33+
throw new RuntimeException("La pila está vacía");
34+
} else {
35+
elementos.remove(elementos.size() - 1);
36+
}
37+
}
38+
39+
public int numeroElementos() {
40+
return elementos.size();
41+
}
42+
43+
public void mostrarElementos() {
44+
System.out.println("Mostrar elementos de la pila: " + elementos);
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return elementos.toString();
50+
}
51+
}
52+
53+
// Implementación de la cola (queue) genérica
54+
class Queue<T> {
55+
private List<T> elementos = new ArrayList<>();
56+
57+
public void agregar(T elemento) {
58+
elementos.add(elemento);
59+
}
60+
61+
public void eliminar() {
62+
if (elementos.isEmpty()) {
63+
throw new RuntimeException("La cola está vacía");
64+
} else {
65+
elementos.remove(0);
66+
}
67+
}
68+
69+
public int numeroElementos() {
70+
return elementos.size();
71+
}
72+
73+
public void mostrarElementos() {
74+
System.out.println("Mostrando elementos: " + elementos);
75+
}
76+
77+
@Override
78+
public String toString() {
79+
return elementos.toString();
80+
}
81+
}
82+
}
83+
84+
class Prueba {
85+
public static void main(String[] args) {
86+
reto_8 prueba = new reto_8("Julian", 19, "Carrera 101a");
87+
prueba.imprimir();
88+
89+
System.out.println("\n****** Pila ******\n");
90+
91+
reto_8.Stack<String> pila = prueba.new Stack<>();
92+
pila.agregar("Elemento 1");
93+
pila.agregar("Elemento 2");
94+
pila.agregar("Elemento 3");
95+
System.out.println("Agregar elemento: " + pila);
96+
97+
pila.eliminar();
98+
System.out.println("Eliminar elemento: " + pila);
99+
100+
int num = pila.numeroElementos();
101+
System.out.println("Número de elementos en la pila: " + num);
102+
103+
pila.mostrarElementos();
104+
105+
System.out.println("\n****** Cola ******\n");
106+
107+
reto_8.Queue<String> cola = prueba.new Queue<>();
108+
cola.agregar("Elemento 1");
109+
cola.agregar("Elemento 2");
110+
cola.agregar("Elemento 3");
111+
System.out.println("Agregar elemento: " + cola);
112+
113+
cola.eliminar();
114+
System.out.println("Eliminar elemento: " + cola);
115+
116+
int numCola = cola.numeroElementos();
117+
System.out.println("Número de elementos en la cola: " + numCola);
118+
119+
cola.mostrarElementos();
120+
}
121+
}

0 commit comments

Comments
 (0)