Advertisement
LEANDRONIEVA

Queue_Java_Tentor_modificado

Oct 11th, 2022
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.68 KB | None | 0 0
  1. //
  2. //Created by Julio Tentor <[email protected]>
  3. //
  4.  
  5. /*
  6. public interface Queue<E>
  7. extends Collection<E>
  8.  
  9. A collection designed for holding elements prior to processing.
  10. Besides basic Collection operations, queues provide additional insertion,
  11. extraction, and inspection operations. Each of these methods exists in
  12. two forms: one throws an exception if the operation fails, the other
  13. returns a special value (either null or false, depending on the operation).
  14.  
  15. The latter form of the insert operation is designed specifically for use
  16. with capacity-restricted Queue implementations; in most implementations,
  17. insert operations cannot fail.
  18.  
  19. Queues typically, but do not necessarily, order elements in a FIFO
  20. (first-in-first-out) manner. Among the exceptions are priority queues,
  21. which order elements according to a supplied comparator, or the elements'
  22. natural ordering, and LIFO queues (or stacks) which order the elements
  23. LIFO (last-in-first-out). Whatever the ordering used, the head of the
  24. queue is that element which would be removed by a call to remove() or
  25. poll(). In a FIFO queue, all new elements are inserted at the tail of
  26. the queue. Other kinds of queues may use different placement rules.
  27. Every Queue implementation must specify its ordering properties.
  28.  
  29. The offer method inserts an element if possible, otherwise returning
  30. false. This differs from the Collection.add method, which can fail to
  31. add an element only by throwing an unchecked exception. The offer
  32. method is designed for use when failure is a normal, rather than
  33. exceptional occurrence, for example, in fixed-capacity (or "bounded")
  34. queues.
  35.  
  36. The remove() and poll() methods remove and return the head of the queue.
  37. Exactly which element is removed from the queue is a function of the
  38. queue's ordering policy, which differs from implementation to
  39. implementation. The remove() and poll() methods differ only in their
  40. behavior when the queue is empty: the remove() method throws an
  41. exception, while the poll() method returns null.
  42.  
  43. The element() and peek() methods return, but do not remove, the head
  44. of the queue.
  45.  
  46. The Queue interface does not define the blocking queue methods, which
  47. are common in concurrent programming. These methods, which wait for
  48. elements to appear or for space to become available, are defined in
  49. the BlockingQueue interface, which extends this interface.
  50.  
  51. Queue implementations generally do not allow insertion of null elements,
  52. although some implementations, such as LinkedList, do not prohibit
  53. insertion of null. Even in the implementations that permit it, null
  54. should not be inserted into a Queue, as null is also used as a special
  55. return value by the poll method to indicate that the queue contains no
  56. elements.
  57.  
  58. Queue implementations generally do not define element-based versions of
  59. methods equals and hashCode but instead inherit the identity based
  60. versions from class Object, because element-based equality is not
  61. always well-defined for queues with the same elements but different
  62. ordering properties.
  63.  
  64. This interface is a member of the Java Collections Framework.
  65.  
  66.  
  67.  
  68. from https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/Queue.html
  69. from https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Queue.html
  70. from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/Queue.html
  71.  
  72. */
  73. public class Queue<ELEMENT> {
  74.  
  75.  //region Constants
  76.  
  77.  private final static Integer defaulDimension = 10;
  78.  
  79.  //endregion
  80.  
  81.  //region Attributes
  82.  
  83.  private ELEMENT [] data;
  84.  private int head;
  85.  private int tail;
  86.  private int count;
  87.  
  88.  //endregion
  89.  
  90.  //region Constructors
  91.  
  92.  public Queue() {
  93.      this(Queue.defaulDimension);
  94.  }
  95.  @SuppressWarnings("unchecked")
  96. public Queue(int dimension) {
  97.      this.data = (ELEMENT[]) new Object[dimension];
  98.      this.head = 0;
  99.      this.tail = 0;
  100.      this.count = 0;
  101.  }
  102.  //endregion
  103.  
  104.  //region Queue Internal Methods
  105.  private int next(int pos) {
  106.      if (++pos >= this.data.length) {
  107.          pos = 0;
  108.      }
  109.      return pos;
  110.  }
  111.  //endregion
  112.  
  113.  
  114.  //region Queue Methods
  115.  
  116.  // Operacion EnQueue en la teoría de Estructura de Datos
  117.  //
  118.  // Inserts the specified element into this queue if it is possible to do so
  119.  // immediately without violating capacity restrictions, returning true upon
  120.  // success and throwing an IllegalStateException if no space is currently
  121.  // available.
  122.  public boolean add(ELEMENT element) {
  123.  
  124.      if (this.size() >= this.data.length) {
  125.          throw new IllegalStateException("Cola llena ...");
  126.      }
  127.  
  128.      this.data[this.tail] = element;
  129.      this.tail = this.next(this.tail);
  130.      ++this.count;
  131.  
  132.      return true;
  133.  }
  134.  
  135.  // Operacion peek en la teoría de Estructura de Datos
  136.  //
  137.  // Retrieves, but does not remove, the head of this queue. This method differs
  138.  // from peek only in that it throws an exception if this queue is empty.
  139.  public ELEMENT element() {
  140.  
  141.      if (this.size() <= 0) {
  142.          throw new IllegalStateException("Cola vacía ...");
  143.      }
  144.  
  145.      return this.data[this.head];
  146.  }
  147.  
  148.  // Operacion EnQueue en la teoría de Estructura de Datos
  149.  //
  150.  // Inserts the specified element into this queue if it is possible to do so
  151.  // immediately without violating capacity restrictions. When using a
  152.  // capacity-restricted queue, this method is generally preferable to add(E),
  153.  // which can fail to insert an element only by throwing an exception.
  154.  public boolean offer(ELEMENT element) {
  155.  
  156.      if (this.size() >= this.data.length) {
  157.          return false;
  158.      }
  159.  
  160.      this.data[this.tail] = element;
  161.      this.tail = this.next(this.tail);
  162.      ++this.count;
  163.  
  164.      return true;
  165.  }
  166.  
  167.  // Retrieves, but does not remove, the head of this queue, or returns null if
  168.  // this queue is empty.
  169.  public ELEMENT peek() {
  170.      if (this.size() <= 0) {
  171.          return null;
  172.      }
  173.  
  174.      return this.data[this.head];
  175.  }
  176.  
  177.  // Operacion DeQueue en la teoría de Estructura de Datos
  178.  //
  179.  // Retrieves and removes the head of this queue, or returns null if this queue
  180.  // is empty.
  181.  public ELEMENT pool() {
  182.      if (this.size() <= 0) {
  183.          return null;
  184.      }
  185.  
  186.      ELEMENT result = this.data[this.head];
  187.      this.head = this.next(this.head);
  188.      --this.count;
  189.  
  190.      return result;
  191.  }
  192.  
  193.  // Operacion DeQueue en la teoría de Estructura de Datos
  194.  //
  195.  // Retrieves and removes the head of this queue. This method differs from poll()
  196.  // only in that it throws an exception if this queue is empty.
  197.  public ELEMENT remove() {
  198.      if (this.size() <= 0) {
  199.          throw new IllegalStateException("Cola vacía ...");
  200.      }
  201.  
  202.      ELEMENT result = this.data[this.head];
  203.      this.head = this.next(this.head);
  204.      --this.count;
  205.  
  206.      return result;
  207.  }
  208.  //endregion
  209.  
  210.  
  211.  //region Override Object basic methods
  212.  
  213.  @Override
  214.  public String toString() {
  215.  
  216.      if (this.size() <=0) {
  217.          return "";
  218.      }
  219.  
  220.      // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
  221.      StringBuilder sb = new StringBuilder();
  222.      sb.append("[" + this.data[this.head].toString());
  223.  
  224.      for (int cta = 1, pos = this.next(this.head); cta < this.size(); ++cta, pos = this.next(pos)) {
  225.          sb.append(", " + this.data[pos].toString());
  226.      }
  227.  
  228.      sb.append("]");
  229.      return sb.toString();
  230.  }
  231.  //endregion
  232.  
  233.  
  234.  //region Collection Methods
  235.  
  236.  public boolean isEmpty() {
  237.      return this.count <= 0;
  238.  }
  239.  
  240.  public boolean isFull() {
  241.      return this.count >= this.data.length;
  242.  }
  243.  
  244.  public int size() {
  245.      return this.count;
  246.  }
  247.  
  248.  public Object[] toArray() {
  249.      Object[] result = new Object[this.count];
  250.      for(int i = 0, pos = this.head, cta = this.size(); cta > 0; ++i, pos = this.next(pos), --cta) {
  251.          result[i] = this.data[pos];
  252.      }
  253.      return result;
  254.  }
  255.  //endregion
  256.  
  257.  
  258.  //region Caso Ejemplo b) Methods
  259.  
  260.  public static Queue<Object> union(Queue<?> stack1, Queue<?> stack2) {
  261.  
  262.      Queue<Object> result = new Queue<Object>(stack1.size() + stack2.size());
  263.  
  264.      for(int pos = stack1.head, cta = stack1.size(); cta > 0; pos = stack1.next(pos), --cta) {
  265.          result.offer( stack1.data[pos] );
  266.      }
  267.      for(int pos = stack2.head, cta = stack2.size(); cta > 0; pos = stack2.next(pos), --cta) {
  268.          result.offer( stack2.data[pos] );
  269.      }
  270.  
  271.      return result;
  272.  }
  273.  
  274.  public Queue<Object> union(Queue<?> stack2) {
  275.      return Queue.union(this, stack2);
  276.  }
  277.  //endregion
  278.  
  279.  //conocer si un elemento se encuentra en la cola
  280.  public boolean contains(Object element) {
  281.      if (element!=null) {
  282.          for(int i = 0, pos = this.head, cta = this.size(); cta > 0; ++i, pos = this.next(pos), --cta) {
  283.              if (element.equals(this.data[i]))return true;
  284.          }
  285.      }
  286.      
  287.      return false;
  288.  }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement