Alexis-Coria

Queue

Nov 8th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 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.  
  106. // Retrieves, but does not remove, the head of this queue, or returns null if
  107. // this queue is empty.
  108. public ELEMENT peek() {
  109. if (this.size() <= 0) {
  110. return null;
  111. }
  112.  
  113. return this.data[this.head];
  114. }
  115.  
  116. // Operacion DeQueue en la teoría de Estructura de Datos
  117. //
  118. // Retrieves and removes the head of this queue, or returns null if this queue
  119. // is empty.
  120. public ELEMENT pool() {
  121. if (this.size() <= 0) {
  122. return null;
  123. }
  124.  
  125. ELEMENT result = this.data[this.head];
  126. this.head = this.next(this.head);
  127. --this.count;
  128.  
  129. return result;
  130. }
  131.  
  132. // Operacion DeQueue en la teoría de Estructura de Datos
  133. //
  134. // Retrieves and removes the head of this queue. This method differs from poll()
  135. // only in that it throws an exception if this queue is empty.
  136. public ELEMENT remove() {
  137. if (this.size() <= 0) {
  138. throw new IllegalStateException("Cola vacía ...");
  139. }
  140.  
  141. ELEMENT result = this.data[this.head];
  142. this.head = this.next(this.head);
  143. --this.count;
  144.  
  145. return result;
  146. }
  147. //endregion
  148.  
  149.  
  150. //region Override Object basic methods
  151.  
  152. @Override
  153. public String toString() {
  154.  
  155. if (this.size() <=0) {
  156. return "";
  157. }
  158.  
  159. // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
  160. StringBuilder sb = new StringBuilder();
  161. sb.append("[" + this.data[this.head].toString());
  162.  
  163. for (int cta = 1, pos = this.next(this.head); cta < this.size(); ++cta, pos = this.next(pos)) {
  164. sb.append(", " + this.data[pos].toString());
  165. }
  166.  
  167. sb.append("]");
  168. return sb.toString();
  169. }
  170. //endregion
  171.  
  172.  
  173. //region Collection Methods
  174.  
  175. public boolean isEmpty() {
  176. return this.count <= 0;
  177. }
  178.  
  179. public int size() {
  180. return this.count;
  181. }
  182.  
  183. public ELEMENT[] toArray() {
  184. if (this.size() <= 0) {
  185. throw new IllegalStateException("Cola vacía ...");
  186. }
  187.  
  188. // ELEMENT [] result = (ELEMENT []) new Object[this.size()];
  189. ELEMENT [] result = (ELEMENT []) Array.newInstance(this.elementClass, this.size());
  190. for(int i = 0, pos = this.head, cta = this.size(); cta > 0; ++i, pos = this.next(pos), --cta) {
  191. result[i] = this.data[pos];
  192. }
  193. return result;
  194. }
  195. //endregion
  196.  
  197.  
  198. //region Caso Ejemplo b) Methods
  199.  
  200. public Queue<ELEMENT> union(Queue<ELEMENT> queue2) {
  201. return union(queue2, 0);
  202. }
  203.  
  204. public Queue<ELEMENT> union(Queue<ELEMENT> queue2, int moreElements) {
  205. Queue<ELEMENT> queue1 = this;
  206.  
  207. Queue<ELEMENT> result = new Queue<ELEMENT>(queue1.size() + queue2.size() + moreElements);
  208.  
  209. for(int pos = queue1.head, cta = queue1.size(); cta > 0; pos = queue1.next(pos), --cta) {
  210. result.offer( queue1.data[pos] );
  211. }
  212. for(int pos = queue2.head, cta = queue2.size(); cta > 0; pos = queue2.next(pos), --cta) {
  213. result.offer( queue2.data[pos] );
  214. }
  215.  
  216. return result;
  217.  
  218. }
  219. //endregion
  220.  
  221.  
  222. //region Iterable Methods
  223.  
  224. @Override
  225. public Iterator<ELEMENT> iterator() {
  226. return new QueueIterator(this);
  227. }
  228.  
  229. private class QueueIterator implements Iterator<ELEMENT> {
  230.  
  231. //region Attributes
  232.  
  233. private Queue<ELEMENT> itQueue;
  234. private int itCount;
  235. private int itPos;
  236.  
  237. //endregion
  238.  
  239. //region Constructor
  240.  
  241. public QueueIterator(Queue<ELEMENT> queue) {
  242. this.itQueue = queue;
  243. this.itCount = this.itQueue.size();
  244. this.itPos = this.itQueue.head;
  245. }
  246.  
  247. //endregion
  248.  
  249. @Override
  250. public boolean hasNext() {
  251. return this.itCount > 0;
  252. }
  253.  
  254. @Override
  255. public ELEMENT next() {
  256. if ( !this.hasNext() ) {
  257. throw new RuntimeException("Error en el iterador de la cola...");
  258. }
  259. ELEMENT item = this.itQueue.data[this.itPos];
  260. this.itPos = this.itQueue.next(this.itPos);
  261. --this.itCount;
  262. return item;
  263. }
  264. }
  265.  
  266. //endregion
  267.  
  268. public boolean estaLlena(){
  269. if(this.count==this.data.length){
  270. System.out.println("La cola se lleno con exito...");
  271. return false;
  272. }
  273. return true;
  274. }
  275. public boolean estaVacia(){
  276. if (this.size() <= 0) {
  277. return true;
  278. }
  279. return false;
  280. }
  281. public int dimensiones(){
  282. int dim = this.data.length;
  283. return dim;
  284.  
  285. }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment