Advertisement
LEANDRONIEVA

colaCircular

Oct 12th, 2022
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. public class colaCircular{
  2.         private static int MAXIMO=99;
  3.         protected int frente;
  4.         protected int fin ;
  5.         protected Object[] ListaCola;
  6.         //avanza una posicion
  7.         public int siguiente (int r)
  8.         {
  9.            
  10.         return(r+1)%MAXIMO;
  11.  
  12.         }
  13.         //inicializa la cola vacia
  14.         public colaCircular(){
  15.         frente=0;
  16.         fin=MAXIMO-1;
  17.         ListaCola= new Object [MAXIMO];
  18.         }
  19.         //operaciones de modificacion de la cola
  20.         public void insertar (Object elemento) throws Exception{
  21.             if(!colaLlena()){
  22.             fin=siguiente(fin);
  23.             ListaCola[fin]=elemento;
  24.             }else throw new Exception("Overflow en la cola");
  25.         }
  26.         public Object quitar()throws Exception{
  27.             if(!colaVacia()){
  28.             Object tm= ListaCola[frente];
  29.             frente= siguiente (frente);
  30.             return tm;
  31.             }else throw new Exception(" Cola vacia ");
  32.         }
  33.         public void borrarCola(){
  34.             frente=0;
  35.             fin=MAXIMO-1;
  36.         }
  37.         //acceso a la cola
  38.         public Object frenteCola()throws Exception{
  39.             if (!colaVacia()){
  40.             return ListaCola[frente];
  41.             }else throw new Exception(" Cola vacia ");
  42.         }
  43.         public Object finCola()throws Exception{
  44.             if (!colaVacia()){
  45.             return ListaCola[fin];
  46.             }else throw new Exception(" Cola vacia ");
  47.         }
  48.         //metodos de verificacion del estado de la cola
  49.         public boolean colaVacia(){
  50.         return frente==siguiente(fin);
  51.         }
  52.         //comprueba si esta llena
  53.         public boolean colaLlena(){
  54.         return frente==siguiente(siguiente(fin));
  55.         }
  56.         public void mostrarCola()throws Exception{
  57.             if(!colaVacia()){
  58.                 for (int i=frente;i<=fin;i++){
  59.                     System.out.print(ListaCola[i]+ " " );
  60.                 }
  61.                 System.out.println( " ");
  62.             }
  63.             else
  64.             throw new Exception(" Cola Vacia ");
  65.         }
  66.         public int tamaƱoCola()throws Exception{
  67.             if(!colaVacia()){
  68.                 return fin-frente;
  69.             }
  70.             else
  71.             throw new Exception ("Cola Vacia ");
  72.         }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement