Traductor

Google-Translate-Chinese Google-Translate-Spanish to French Google-Translate-Spanish to German Google-Translate-Spanish to Japanese Google-Translate-Spanish to English Google-Translate-Spanish to Russian Google-Translate-Spanish to Portuguese Google-Translate-Spanish to Italian

martes, 17 de noviembre de 2015

Colas en Java (ejercicio manejo de colas)

package colasjava;

/**
 *
 * @author JV PC LDU
 */
public class COLASJAVA {

    /**
     * @param args the command line arguments
     */
    
      public static void main(String[] args) {
        Cola cola = new Cola(4);
        cola.print();
        cola.remove();
        cola.insert("Hola");
        cola.insert(2);
        cola.insert("java");
        cola.insert(9);
        cola.insert(8888);
        cola.remove();
        cola.remove();
        cola.remove();
        cola.remove();
        cola.remove();
    }
    
}

CLASE COLA


public class Cola {
    private Object[] cola;
    private int frente;
    private int fin;
    
    public Prueba(int tamanio)
    {
        cola = new Object[tamanio];
        frente = fin = -1;
    }
    
    public boolean isEmpty()
    {
        return frente == fin;
    }
    
    public boolean isFull()
    {
        return fin == cola.length-1;
    }
    
    
    public void insert(Object elemento)
    {
        if(isFull())
            System.out.println("La cola esta llena, elimine datos!");
        else
            cola[++fin] = elemento;
    }
    
    public Object remove()
    {
        if(isEmpty())
        {
            System.out.println("La cola esta vacia, inserte datos!");
            return "Cola vacia";
        }
        else
            return cola[++frente];
    }
    
    
    public void print()
    {
        if(isEmpty())
            System.out.println("La cola esta vacia!");
        else if(frente == -1)
        {
            for(int i = 0; i <= fin; i++)
            {
                System.out.println(cola[i]);
            }
        }
        else
        {
            for(int j = frente; j <= fin; j++)
            {
                System.out.println(cola[j]);
            }
        }
    }

}






?

No hay comentarios.:

Publicar un comentario