Leonel_G

Queue

Oct 12th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.73 KB | None | 0 0
  1. import java.lang.reflect.Array;
  2. import java.util.Iterator;
  3.  
  4. public class Queue<ELEMENT> implements Iterable<ELEMENT> {
  5.  
  6.     //region Constants
  7.  
  8.     protected final static Integer defaulDimension = 10;
  9.  
  10.     //endregion
  11.  
  12.     //region Attributes
  13.  
  14.     protected Class<?> elementClass;
  15.     protected ELEMENT [] data;
  16.     protected int head;
  17.     protected int tail;
  18.     protected int count;
  19.  
  20.     //endregion
  21.  
  22.     //region Constructors
  23.  
  24.     public Queue() {
  25.         this(Queue.defaulDimension);
  26.     }
  27.  
  28.     // from https://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java
  29.     @SuppressWarnings("unchecked")
  30.     public Queue(int dimension, ELEMENT... dummy) {
  31.         if (dummy.length > 0) {
  32.             throw new IllegalArgumentException("No se debe facilitar valores para dummy");
  33.         }
  34.         elementClass = dummy.getClass().getComponentType();
  35.         this.data = (ELEMENT []) Array.newInstance(this.elementClass, dimension);
  36.         this.head = 0;
  37.         this.tail = 0;
  38.         this.count = 0;
  39.     }
  40.     //endregion
  41.  
  42.     //region Queue Internal Methods
  43.     protected int next(int pos) {
  44.         if (++pos >= this.data.length) {
  45.             pos = 0;
  46.         }
  47.         return pos;
  48.     }
  49.     //endregion
  50.  
  51.  
  52.     //region Queue Methods
  53.  
  54.     // Operacion EnQueue en la teoría de Estructura de Datos
  55.     //
  56.     // Inserts the specified element into this queue if it is possible to do so
  57.     // immediately without violating capacity restrictions, returning true upon
  58.     // success and throwing an IllegalStateException if no space is currently
  59.     // available.
  60.     public boolean add(ELEMENT element) {
  61.  
  62.         if (this.size() >= this.data.length) {
  63.             throw new IllegalStateException("Cola llena ...");
  64.         }
  65.  
  66.         this.data[this.tail] = element;
  67.         this.tail = this.next(this.tail);
  68.         ++this.count;
  69.  
  70.         return true;
  71.     }
  72.  
  73.     // Operacion peek en la teoría de Estructura de Datos
  74.     //
  75.     // Retrieves, but does not remove, the head of this queue. This method differs
  76.     // from peek only in that it throws an exception if this queue is empty.
  77.     public ELEMENT element() {
  78.  
  79.         if (this.size() <= 0) {
  80.             throw new IllegalStateException("Cola vacía ...");
  81.         }
  82.  
  83.         return this.data[this.head];
  84.     }
  85.  
  86.     // Operacion EnQueue en la teoría de Estructura de Datos
  87.     //
  88.     // Inserts the specified element into this queue if it is possible to do so
  89.     // immediately without violating capacity restrictions. When using a
  90.     // capacity-restricted queue, this method is generally preferable to add(E),
  91.     // which can fail to insert an element only by throwing an exception.
  92.     public boolean offer(ELEMENT element) {
  93.  
  94.         if (this.size() >= this.data.length) {
  95.             return false;
  96.         }
  97.  
  98.         this.data[this.tail] = element;
  99.         this.tail = this.next(this.tail);
  100.         ++this.count;
  101.  
  102.         return true;
  103.     }
  104.  
  105.     // Retrieves, but does not remove, the head of this queue, or returns null if
  106.     // this queue is empty.
  107.     public ELEMENT peek() {
  108.         if (this.size() <= 0) {
  109.             return null;
  110.         }
  111.  
  112.         return this.data[this.head];
  113.     }
  114.  
  115.     // Operacion DeQueue en la teoría de Estructura de Datos
  116.     //
  117.     // Retrieves and removes the head of this queue, or returns null if this queue
  118.     // is empty.
  119.     public ELEMENT pool() {
  120.         if (this.size() <= 0) {
  121.             return null;
  122.         }
  123.  
  124.         ELEMENT result = this.data[this.head];
  125.         this.head = this.next(this.head);
  126.         --this.count;
  127.  
  128.         return result;
  129.     }
  130.  
  131.     // Operacion DeQueue en la teoría de Estructura de Datos
  132.     //
  133.     // Retrieves and removes the head of this queue. This method differs from poll()
  134.     // only in that it throws an exception if this queue is empty.
  135.     public ELEMENT remove() {
  136.         if (this.size() <= 0) {
  137.             throw new IllegalStateException("Cola vacía ...");
  138.         }
  139.  
  140.         ELEMENT result = this.data[this.head];
  141.         this.head = this.next(this.head);
  142.         --this.count;
  143.  
  144.         return result;
  145.     }
  146.     //endregion
  147.  
  148.  
  149.     //region Override Object basic methods
  150.  
  151.     @Override
  152.     public String toString() {
  153.  
  154.         if (this.size() <=0) {
  155.             return "";
  156.         }
  157.  
  158.         // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
  159.         StringBuilder sb = new StringBuilder();
  160.         sb.append("[" + this.data[this.head].toString());
  161.  
  162.         for (int cta = 1, pos = this.next(this.head); cta < this.size(); ++cta, pos = this.next(pos)) {
  163.             sb.append(", " + this.data[pos].toString());
  164.         }
  165.  
  166.         sb.append("]");
  167.         return sb.toString();
  168.     }
  169.     //endregion
  170.  
  171.  
  172.     //region Collection Methods
  173.  
  174.     public boolean isEmpty() {
  175.         return this.count <= 0;
  176.     }
  177.  
  178.     public int size() {
  179.         return this.count;
  180.     }
  181.  
  182.     public ELEMENT[] toArray() {
  183.         if (this.size() <= 0) {
  184.             throw new IllegalStateException("Cola vacía ...");
  185.         }
  186.  
  187. //        ELEMENT [] result = (ELEMENT []) new Object[this.size()];
  188.         ELEMENT [] result = (ELEMENT []) Array.newInstance(this.elementClass, this.size());
  189.         for(int i = 0, pos = this.head, cta = this.size(); cta > 0; ++i, pos = this.next(pos), --cta) {
  190.             result[i] = this.data[pos];
  191.         }
  192.         return result;
  193.     }
  194.     //endregion
  195.  
  196.  
  197.     //region Caso Ejemplo b) Methods
  198.  
  199.     public Queue<ELEMENT> union(Queue<ELEMENT> queue2) {
  200.         return union(queue2, 0);
  201.     }
  202.  
  203.     public Queue<ELEMENT> union(Queue<ELEMENT> queue2, int moreElements) {
  204.         Queue<ELEMENT> queue1 = this;
  205.  
  206.         Queue<ELEMENT> result = new Queue<ELEMENT>(queue1.size() + queue2.size() + moreElements);
  207.  
  208.         for(int pos = queue1.head, cta = queue1.size(); cta > 0; pos = queue1.next(pos), --cta) {
  209.             result.offer( queue1.data[pos] );
  210.         }
  211.         for(int pos = queue2.head, cta = queue2.size(); cta > 0; pos = queue2.next(pos), --cta) {
  212.             result.offer( queue2.data[pos] );
  213.         }
  214.  
  215.         return result;
  216.  
  217.     }
  218.     //endregion
  219.  
  220.  
  221.     //region Iterable Methods
  222.  
  223.     @Override
  224.     public Iterator<ELEMENT> iterator() {
  225.         return new QueueIterator(this);
  226.     }
  227.  
  228.     private class QueueIterator implements Iterator<ELEMENT> {
  229.  
  230.         //region Attributes
  231.  
  232.         private Queue<ELEMENT> itQueue;
  233.         private int itCount;
  234.         private int itPos;
  235.  
  236.         //endregion
  237.  
  238.         //region Constructor
  239.  
  240.         public QueueIterator(Queue<ELEMENT> queue) {
  241.             this.itQueue = queue;
  242.             this.itCount = this.itQueue.size();
  243.             this.itPos = this.itQueue.head;
  244.         }
  245.  
  246.         //endregion
  247.  
  248.         @Override
  249.         public boolean hasNext() {
  250.             return this.itCount > 0;
  251.         }
  252.  
  253.         @Override
  254.         public ELEMENT next() {
  255.             if ( !this.hasNext() ) {
  256.                 throw new RuntimeException("Error en el iterador de la cola...");
  257.             }
  258.             ELEMENT item = this.itQueue.data[this.itPos];
  259.             this.itPos = this.itQueue.next(this.itPos);
  260.             --this.itCount;
  261.             return item;
  262.         }
  263.     }
  264.  
  265.     //endregion
  266.  
  267.     public boolean estaLlena(){
  268.         if(this.count==this.data.length){
  269.             System.out.println("la cola esta llena");
  270.             return false;
  271.         }
  272.         return true;
  273.     }
  274.     public boolean estaVacia(){
  275.         if (this.size() <= 0) {
  276.             return true;
  277.         }
  278.         return false;
  279.     }
  280.     public int dimensiones(){
  281.         int dim = this.data.length;
  282.         return dim;
  283.  
  284.     }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment