View difference between Paste ID: mJvvmDdb and 1xZSb977
SHOW: | | - or go back to the newest paste.
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
74
75
import java.util.Arrays;
76
77
public class Queue<ELEMENT> {
78
79
    //region Constants
80
81
    private final static Integer defaulDimension = 10;
82
83
    //endregion
84
85
    //region Attributes
86
87
    private ELEMENT [] data;
88
    private int head;
89
    private int tail;
90
    private int count;
91
92
    //endregion
93
94
    //region Constructors
95
96
    public Queue() {
97
        this(Queue.defaulDimension);
98
    }
99
    public Queue(int dimension) {
100
        this.data = (ELEMENT[]) new Object[dimension];
101
        this.head = 0;
102
        this.tail = 0;
103
        this.count = 0;
104
    }
105
    //endregion
106
107
    //region Queue Internal Methods
108
    private int next(int pos) {
109
        if (++pos >= this.data.length) {
110
            pos = 0;
111
        }
112
        return pos;
113
    }
114
    //endregion
115
116
117
    //region Queue Methods
118
119
    // Operacion EnQueue en la teoría de Estructura de Datos
120
    //
121
    // Inserts the specified element into this queue if it is possible to do so
122
    // immediately without violating capacity restrictions, returning true upon
123
    // success and throwing an IllegalStateException if no space is currently
124
    // available.
125
    public boolean add(ELEMENT element) {
126
127
        if (this.size() >= this.data.length) {
128
            throw new IllegalStateException("Cola llena ...");
129
        }
130
131
        this.data[this.tail] = element;
132
        this.tail = this.next(this.tail);
133
        ++this.count;
134
135
        return true;
136
    }
137
138
    // Operacion peek en la teoría de Estructura de Datos
139
    //
140
    // Retrieves, but does not remove, the head of this queue. This method differs
141
    // from peek only in that it throws an exception if this queue is empty.
142
    public ELEMENT element() {
143
144
        if (this.size() <= 0) {
145
            throw new IllegalStateException("Cola vacía ...");
146
        }
147
148
        return this.data[this.head];
149
    }
150
151
    // Operacion EnQueue en la teoría de Estructura de Datos
152
    //
153
    // Inserts the specified element into this queue if it is possible to do so
154
    // immediately without violating capacity restrictions. When using a
155
    // capacity-restricted queue, this method is generally preferable to add(E),
156
    // which can fail to insert an element only by throwing an exception.
157
    public boolean offer(ELEMENT element) {
158
159
        if (this.size() >= this.data.length) {
160
            return false;
161
        }
162
163
        this.data[this.tail] = element;
164
        this.tail = this.next(this.tail);
165
        ++this.count;
166
167
        return true;
168
    }
169
170
    // Retrieves, but does not remove, the head of this queue, or returns null if
171
    // this queue is empty.
172
    public ELEMENT peek() {
173
        if (this.size() <= 0) {
174
            return null;
175
        }
176
177
        return this.data[this.head];
178
    }
179
180
    // Operacion DeQueue en la teoría de Estructura de Datos
181
    //
182
    // Retrieves and removes the head of this queue, or returns null if this queue
183
    // is empty.
184
    public ELEMENT pool() {
185
        if (this.size() <= 0) {
186
            return null;
187
        }
188
189
        ELEMENT result = this.data[this.head];
190
        this.head = this.next(this.head);
191
        --this.count;
192
193
        return result;
194
    }
195
196
    // Operacion DeQueue en la teoría de Estructura de Datos
197
    //
198
    // Retrieves and removes the head of this queue. This method differs from poll()
199
    // only in that it throws an exception if this queue is empty.
200
    public ELEMENT remove() {
201
        if (this.size() <= 0) {
202
            throw new IllegalStateException("Cola vacía ...");
203
        }
204
205
        ELEMENT result = this.data[this.head];
206
        this.head = this.next(this.head);
207
        --this.count;
208
209
        return result;
210
    }
211
    //endregion
212
213
214
    //region Override Object basic methods
215
216
    @Override
217
    public String toString() {
218
219
        if (this.size() <=0) {
220
            return "";
221
        }
222
223
        // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
224
        StringBuilder sb = new StringBuilder();
225
        sb.append("[" + this.data[this.head].toString());
226
227
        for (int cta = 1, pos = this.next(this.head); cta < this.size(); ++cta, pos = this.next(pos)) {
228
            sb.append(", " + this.data[pos].toString());
229
        }
230
231
        sb.append("]");
232
        return sb.toString();
233
    }
234
    //endregion
235
236
237
    //region Collection Methods
238
239
    public boolean isEmpty() {
240
        return this.count <= 0;
241
    }
242
243
    public int size() {
244
        return this.count;
245
    }
246
247
    public Object[] toArray() {
248
        Object[] result = new Object[this.count];
249
        for(int i = 0, pos = this.head, cta = this.size(); cta > 0; ++i, pos = this.next(pos), --cta) {
250
            result[i] = this.data[pos];
251
        }
252
        return result;
253
    }
254
    //endregion
255
256
257
    //region Caso Ejemplo b) Methods
258
259
    public static Queue<Object> union(Queue<?> stack1, Queue<?> stack2) {
260
261
        Queue<Object> result = new Queue<Object>(stack1.size() + stack2.size());
262
263
        for(int pos = stack1.head, cta = stack1.size(); cta > 0; pos = stack1.next(pos), --cta) {
264
            result.offer( stack1.data[pos] );
265
        }
266
        for(int pos = stack2.head, cta = stack2.size(); cta > 0; pos = stack2.next(pos), --cta) {
267
            result.offer( stack2.data[pos] );
268
        }
269
270
        return result;
271
    }
272
273
    public Queue<Object> union(Queue<?> stack2) {
274
        return Queue.union(this, stack2);
275
    }
276
    //endregion
277
278
}
279