Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Created September 7, 2016 14:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save alvareztech/cb867cf96184795dd8228e656087100a to your computer and use it in GitHub Desktop.
Estructura de datos>Colas>Ejercicio 1
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);
}
*/
}
}
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