FacuValverdi

EdD-TP03-colaCircular

Oct 21st, 2022 (edited)
999
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. //VALVERDI, FACUNDO LAUTARO
  2. //Clase colaCircular se utiliza para resolver todos los ejercicios del TP03
  3. public class colaCircular {
  4.     private int fin;
  5.     private int frente;
  6.     private int cantidad;
  7.     private final int capacidad=100; // OJO con la capacidad!!!!!
  8.     private int[] contenedor;
  9.    
  10.     public colaCircular(){
  11.         this.fin=0;
  12.         this.frente=0;
  13.         this.cantidad=0;
  14.         this.contenedor=new int[capacidad];
  15.     }
  16.     public colaCircular(int capacidad){
  17.         this.fin=0;
  18.         this.frente=0;
  19.         this.cantidad=0;
  20.         this.contenedor=new int[capacidad];
  21.     }
  22.     public boolean colaVacia() {
  23.         return this.cantidad <=0;
  24.     }
  25.     public boolean colaLlena() {
  26.         return this.cantidad== this.capacidad;
  27.     }
  28.    
  29.     public int siguiente(int pos) {
  30.         ++pos;
  31.         if(pos>capacidad) {
  32.             pos=0;
  33.         }
  34.         return pos;
  35.     }
  36.     public void encolar(int numero) {
  37.         if(colaLlena()) {
  38.             throw new RuntimeException("La cola esta llena");
  39.         }
  40.         contenedor[this.fin]=numero;
  41.         this.fin=siguiente(this.fin);
  42.         ++this.cantidad;
  43.     }
  44.    
  45.     public int desencolar() {
  46.         if(colaVacia()) {
  47.             throw new RuntimeException("La cola esta vacia");
  48.         }
  49.         int numero=this.contenedor[this.frente];
  50.         this.frente=siguiente(frente);
  51.         cantidad--;
  52.         return numero;
  53.     }
  54.     public int frenteCola() {
  55.         if (!colaVacia()){
  56.         return contenedor[frente];
  57.         }else throw new RuntimeException("Cola vacia ");
  58.     }
  59.     public int finCola() {
  60.         if (!colaVacia()){
  61.         return contenedor[fin];
  62.         }else throw new RuntimeException("Cola vacia ");
  63.     }
  64.     public void mostrarCola(){
  65.         int aux=this.frente;
  66.          System.out.print("[");
  67.         while(this.frente!=this.fin) {
  68.             System.out.print(" "+frenteCola());
  69.             this.frente++;
  70.         }
  71.         System.out.print("]");
  72.         System.out.println(" ");
  73.         this.frente=aux;
  74.     }
  75. /*
  76.      public static colaCircular quitarRepetidoEn2Colas(colaCircular cola1,colaCircular cola2) {
  77.             colaCircular auxCola= new colaCircular();
  78.             int numCola1=0;
  79.             int numCola2=0;
  80.             int auxFrente=cola2.frente;
  81.             int auxFin=cola2.fin;
  82.             while(!cola1.colaVacia()) {
  83.                 numCola1=cola1.desencolar();
  84.                 while(cola2.frente<auxFin) {
  85.                     numCola2=cola2.desencolar();
  86.                     if(numCola1 != numCola2) {
  87.                         cola2.encolar(numCola2);   
  88.                     }else {
  89.                         auxCola.encolar(numCola2);
  90.                         numCola1=cola1.desencolar();
  91.                     }
  92.                
  93.             }
  94.                 auxCola.encolar(numCola1);      
  95. }
  96.            
  97.             return auxCola;
  98.      }
  99. */
  100.        public static colaCircular quitarRepetido(colaCircular cola) {
  101.             colaCircular auxCola= new colaCircular();
  102.             int numCola=0, num2Cola=0;
  103.             int auxFrente=cola.frente;
  104.             int auxFin=cola.fin;
  105.             while(!cola.colaVacia()) {
  106.                 numCola=cola.desencolar();
  107.                 while(cola.frente<auxFin) {
  108.                     num2Cola=cola.desencolar();
  109.                     if (numCola!= num2Cola)
  110.                     {
  111.                         cola.encolar(num2Cola);
  112.                     }
  113.                 }
  114.                 auxFin=cola.fin;
  115.                 auxCola.encolar(numCola);
  116.             }
  117.             return auxCola;  
  118.         }
  119.    
  120.     public static colaCircular unirColas(colaCircular cola1,colaCircular cola2) {
  121.         colaCircular aux= new colaCircular();
  122.         while(!cola1.colaVacia()) {
  123.             aux.encolar(cola1.desencolar());
  124.         }
  125.         while(!cola2.colaVacia()) {
  126.             aux.encolar(cola2.desencolar());
  127.         }
  128.         return aux;
  129.     }
  130. }
  131.  
Tags: colaCircular
Add Comment
Please, Sign In to add comment