Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Created September 5, 2016 00:23
Show Gist options
  • Save alvareztech/e6af1be7b3d67a9848d2ceb9fc5cff1f to your computer and use it in GitHub Desktop.
Save alvareztech/e6af1be7b3d67a9848d2ceb9fc5cff1f to your computer and use it in GitHub Desktop.
Ejemplo colas de prioridad en Java. PriorityQueue.
package tech.alvarez;
import java.util.PriorityQueue;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Persona> cola = new PriorityQueue<Persona>();
cola.add(new Persona("Daniel", 1));
cola.add(new Persona("Katherine", 3));
cola.add(new Persona("Julio", 2));
cola.add(new Persona("Maria", 1));
while (!cola.isEmpty()) {
Persona a = cola.remove();
System.out.println(a.getNombre() + " " + a.getTipo());
}
}
}
package tech.alvarez;
public class Persona implements Comparable<Persona> {
private String nombre;
private int tipo; // 1 normal, 2 tercera edad, 3 embarazada
public Persona(String nombre, int tipo) {
this.nombre = nombre;
this.tipo = tipo;
}
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 o) {
if (tipo < o.getTipo()) {
return 1;
} else if (tipo > o.getTipo()) {
return -1;
} else {
return 0;
}
}
}
@cristinabriceno96
Copy link

public class Pila {
Nodo inicio;
Nodo fin;

public Pila() {
    inicio =null;
}

public void push(int dato){
Nodo nuevo= new Nodo(dato);
nuevo.setSiguiente(inicio);
inicio=nuevo;
}

public int pop(){
Nodo aux=inicio;
inicio=inicio.getSiguiente();
aux.setSiguiente(null);
return aux.getDato();
}

public boolean vacio(){
    if(inicio==null){
        System.out.println("Esta vacio");
        return true;
    }else{
        System.out.println("No esta vacio");
        return false;
    }
}
public int size(){
    Nodo aux = inicio;
    int count = 0;
    while(aux!=null){
        aux = aux.getSiguiente();            
        count++;
    }
    return count;
}

public int tope()throws Exception{
    
    if(!vacio()){
        int tope=inicio.getDato();
        return tope;
    }else{
        
        throw new Exception("La lista esta vacia");
    }
    
}

public void imprimir(){
    Nodo aux = inicio;
    
    while(aux!=null){
        System.out.println(aux.getDato());
        aux = aux.getSiguiente();            
        
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment