Created
September 7, 2016 14:37
Star
You must be signed in to star a gist
Estructura de datos>Colas>Ejercicio 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez; | |
import java.util.LinkedList; | |
import java.util.PriorityQueue; | |
import java.util.Queue; | |
public class Main { | |
public static void main(String[] args) { | |
Queue<Persona> colaPrioridad = new PriorityQueue<Persona>(); | |
colaPrioridad.add(new Persona("Luis", 1)); | |
colaPrioridad.add(new Persona("Juan", 2)); | |
colaPrioridad.add(new Persona("Luisa", 1)); | |
colaPrioridad.add(new Persona("Pedro", 2)); | |
colaPrioridad.add(new Persona("Maria", 3)); | |
int n = colaPrioridad.size(); | |
for (int i = 0; i < n; i++) { | |
Persona p = colaPrioridad.remove(); | |
System.out.println("Nombre: " + p.getNombre()); | |
} | |
/* | |
Queue<Persona> colaSimple = new LinkedList<Persona>(); | |
Persona p1 = new Persona("Maria"); | |
colaSimple.add(p1); | |
colaSimple.add(new Persona("Juan")); | |
colaSimple.add(new Persona("Luis")); | |
int n = colaSimple.size(); | |
for (int i = 0; i < n; i++) { | |
Persona p = colaSimple.remove(); | |
System.out.println("Nombre: " + p.getNombre()); | |
colaSimple.add(p); | |
} | |
*/ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez; | |
public class Persona implements Comparable<Persona> { | |
private String nombre; | |
private int tipo; // 1 normal, 2 adulto mayor, 3 embarazada | |
public Persona(String nombre, int tipo) { | |
this.nombre = nombre; | |
this.tipo = tipo; | |
} | |
public Persona(String nombre) { | |
this.nombre = nombre; | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public void setNombre(String nombre) { | |
this.nombre = nombre; | |
} | |
public int getTipo() { | |
return tipo; | |
} | |
public void setTipo(int tipo) { | |
this.tipo = tipo; | |
} | |
@Override | |
public int compareTo(Persona b) { | |
if (this.tipo == b.tipo) { | |
return 0; | |
} else { | |
if (this.tipo > b.tipo) { | |
return -1; | |
} else { | |
return 1; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment