Cola Array

download Cola Array

If you can't read please download the document

description

tipos de colas

Transcript of Cola Array

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package colaarreglos;import java.util.Scanner;import javax.swing.JOptionPane;public class Colaarray { private final int MAXIMO = 7; private int[] V; private int inicio; private int fin; public Colaarray() { this.V = new int[MAXIMO + 1]; this.inicio = 0; this.fin = 0; } public boolean colaVacia() { return inicio == fin; } public boolean colaLlena() { return tamao() == MAXIMO - 1; } public void adicionar(int a) { if (colaLlena()) { JOptionPane.showMessageDialog(null, "no se pueden insertar mas elementos cola llena", "", JOptionPane.ERROR_MESSAGE); } else { fin = (fin + 1) % MAXIMO; V[fin] = a; } } public int Eliminar() { int a = 0; if (colaVacia()) { JOptionPane.showMessageDialog(null, "no hay elementos a eliminar de la cola", "", JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog(null, "elemento eliminado"); inicio = (inicio + 1) % MAXIMO; a = V[inicio]; } return a; } public int tamao() { return (fin - inicio + MAXIMO) % MAXIMO; } private void copiar(Colaarray B) { while (!B.colaVacia()) { adicionar(B.Eliminar()); } } public void insertar() { int maximo = Integer.parseInt(JOptionPane.showInputDialog("Nro.Elementos: ")); for (int i = 0; i < maximo; i++) { int x = Integer.parseInt(JOptionPane.showInputDialog("inserta el elemento a la cola ")); adicionar(x); } } public void imprimir() { if (colaVacia()) { JOptionPane.showMessageDialog(null, "Cola Vacia"); } else { Colaarray X = new Colaarray(); System.out.println( "\n la cola queda " ); while (!colaVacia()) { int a = Eliminar(); System.out.print( "[" + a+ "]" ); X.adicionar(a); } this.copiar(X); } } public static void main(String[] args) { int r = 0; Colaarray traer = new Colaarray(); do { r = Integer.parseInt(JOptionPane.showInputDialog("Que desesa hacer \n1.-agregar \n2.-Eliminar \n3.-Imprimir \n4.-Salir")); switch (r) { case 1: traer.insertar(); break; case 2: traer.Eliminar(); break; case 3: traer.imprimir(); break; } } while (r != 4); }}