FacundoCruz

Cola

Oct 12th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class Cola<T> {
  4.  
  5.  //region Constantes
  6.  
  7.  private final static Integer defaulDimension = 10;
  8.  
  9.  //endregion
  10.  
  11.  //region Atributos
  12.  
  13.  private T [] data;
  14.  private int head;
  15.  private int tail;
  16.  private int count;
  17.  
  18.  //endregion
  19.  
  20.  //region Constructores
  21.  
  22.  public Cola() {
  23.      this(Cola.defaulDimension);
  24.  }
  25.  public Cola(int dimension) {
  26.      this.data = (T[]) new Object[dimension];
  27.      this.head = 0;
  28.      this.tail = 0;
  29.      this.count = 0;
  30.  }
  31.  //endregion
  32.  
  33.  //region Metodos Internos de la Cola
  34.  private int next(int pos) {
  35.      if (++pos >= this.data.length) {
  36.          pos = 0;
  37.      }
  38.      return pos;
  39.  }
  40.  //endregion
  41.  
  42.  
  43.  //region Metodos de la Cola
  44.  
  45.  // Operación EnQueue en la teoría de Estructura de Datos
  46.  //
  47.  // Inserta un elemento especificado en la cola si esto es posible de hacer entonces
  48.  // inmediatamente sin violar las restricciones, retorna verdadero sobre
  49.  // el exito y arroja una IllegalStateException si no hay espacio concurrentemente
  50.  // disponible.
  51.  public boolean add(T element) {
  52.  
  53.      if (this.size() >= this.data.length) {
  54.          throw new IllegalStateException("Cola llena ...");
  55.      }
  56.  
  57.      this.data[this.tail] = element;
  58.      this.tail = this.next(this.tail);
  59.      ++this.count;
  60.  
  61.      return true;
  62.  }
  63.  
  64.  // Operación peek en la teoría de Estructura de Datos
  65.  //
  66.  // Recupera, pero no remueve, la cabeza de la cola. Este difiere del peek
  67.  // solo en que este arroja una excepción si la cola esta vacia.
  68.  public T element() {
  69.  
  70.      if (this.size() <= 0) {
  71.          throw new IllegalStateException("Cola vacía ...");
  72.      }
  73.  
  74.      return this.data[this.head];
  75.  }
  76.  
  77.  // Operación EnQueue en la teoría de Estructura de Datos
  78.  //
  79.  // Inserta un elemento especificado en la cola si si esto es posible de hacer entonces
  80.  // inmediatamente sin violar las restricciones. Cuando se usa una
  81.  // capacidad-restringidad en cola, este metodo prefiere generalmente agregar (E),
  82.  // el cual puede fallar al insertar un elemento solo por arrojar una excepción.
  83.  public boolean offer(T element) {
  84.  
  85.      if (this.size() >= this.data.length) {
  86.          return false;
  87.      }
  88.  
  89.      this.data[this.tail] = element;
  90.      this.tail = this.next(this.tail);
  91.      ++this.count;
  92.  
  93.      return true;
  94.  }
  95.  
  96.  // Recupera, pero no remueve, la cabeza de la cola, o retorna nulo si
  97.  // la cola esta vacia.
  98.  public T peek() {
  99.      if (this.size() <= 0) {
  100.          return null;
  101.      }
  102.  
  103.      return this.data[this.head];
  104.  }
  105.  
  106.  // Operación DeQueue en la teoría de Estructura de Datos
  107.  //
  108.  // Recupera y remueve la cabeza de la cola, o retorna nulo si la cola
  109.  // esta vacia.
  110.  public T pool() {
  111.      if (this.size() <= 0) {
  112.          return null;
  113.      }
  114.  
  115.      T result = this.data[this.head];
  116.      this.head = this.next(this.head);
  117.      --this.count;
  118.  
  119.      return result;
  120.  }
  121.  
  122.  // Operación DeQueue en la teoría de Estructura de Datos
  123.  //
  124.  // Recupera y remueve la cabeza de la cola. Este difiere del poll()
  125.  // solo en que este arroja una excepción si la cola esta vacia.
  126.  public T remove() {
  127.      if (this.size() <= 0) {
  128.          throw new IllegalStateException("Cola vacía ...");
  129.      }
  130.  
  131.      T result = this.data[this.head];
  132.      this.head = this.next(this.head);
  133.      --this.count;
  134.  
  135.      return result;
  136.  }
  137.  //endregion
  138.  
  139.  
  140.  //region Override Object basic methods
  141.  
  142.  @Override
  143.  public String toString() {
  144.  
  145.      if (this.size() <=0) {
  146.          return "";
  147.      }
  148.  
  149.      // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
  150.      StringBuilder sb = new StringBuilder();
  151.      sb.append("[" + this.data[this.head].toString());
  152.  
  153.      for (int cta = 1, pos = this.next(this.head); cta < this.size(); ++cta, pos = this.next(pos)) {
  154.          sb.append(", " + this.data[pos].toString());
  155.      }
  156.  
  157.      sb.append("]");
  158.      return sb.toString();
  159.  }
  160.  //endregion
  161.  
  162.  
  163.  //region Metodos de la Colleccion
  164.  
  165.  public boolean isEmpty() {
  166.      return this.count <= 0;
  167.  }
  168.  
  169.  public int size() {
  170.      return this.count;
  171.  }
  172.  
  173.  public Object[] toArray() {
  174.      Object[] result = new Object[this.count];
  175.      for(int i = 0, pos = this.head, cta = this.size(); cta > 0; ++i, pos = this.next(pos), --cta) {
  176.          result[i] = this.data[pos];
  177.      }
  178.      return result;
  179.  }
  180.  //endregion
  181.  
  182.  
  183.  //region Caso Ejemplo b) Methods
  184.  
  185.  public static Cola<Object> union(Cola<?> stack1, Cola<?> stack2) {
  186.  
  187.      Cola<Object> result = new Cola<Object>(stack1.size() + stack2.size());
  188.  
  189.      for(int pos = stack1.head, cta = stack1.size(); cta > 0; pos = stack1.next(pos), --cta) {
  190.          result.offer( stack1.data[pos] );
  191.      }
  192.      for(int pos = stack2.head, cta = stack2.size(); cta > 0; pos = stack2.next(pos), --cta) {
  193.          result.offer( stack2.data[pos] );
  194.      }
  195.  
  196.      return result;
  197.  }
  198.  
  199.  public Cola<Object> union(Cola<?> stack2) {
  200.      return Cola.union(this, stack2);
  201.  }
  202.  //endregion
  203.  
  204.  
Add Comment
Please, Sign In to add comment