1
1
package dataStructures .queue ;
2
2
3
- import java . util . Deque ;
3
+
4
4
import java .util .ArrayDeque ;
5
+ import java .util .Deque ;
5
6
6
7
/**
7
- * Implementation of a non-increasing (decreasing) monotonic queue for certain (but common) use case:
8
- * When larger objects pushed to the queue are able to replace and represent smaller objects that come before it.
9
- * Callable methods are:
10
- * isEmpty()
11
- * max()
12
- * pop()
13
- * push()
14
- * @param <T> generic type for objects to be stored or queried
15
- *
16
- * index v Increasing queue Decreasing queue
17
- * 1 5 [5] [5]
18
- * 2 3 [3] 3 kick out 5 [5, 3] #3->5
19
- * 3 1 [1] 1 kick out 3 [5, 3, 1] #1->3
20
- * 4 2 [1, 2] #2->1 [5, 3, 2] 2 kick out 1 #2->3
21
- * 5 4 [1, 2, 4] #4->2 [5,4] 4 kick out 2, 3 #4->2
22
- */
8
+ * Implementation of a non-increasing (decreasing) monotonic queue
9
+ * for certain (but common) use case:
10
+ * When larger objects pushed to the queue are able to replace and represent
11
+ * smaller objects that come before it.
12
+ * Callable methods are:
13
+ * isEmpty()
14
+ * max()
15
+ * pop()
16
+ * push()
17
+ * <p></p>
18
+ * Movement within the monotonic-queue:
19
+ * index v Increasing queue Decreasing queue
20
+ * 1 5 [5] [5]
21
+ * 2 3 [3] 3 kick out 5 [5, 3] #3->5
22
+ * 3 1 [1] 1 kick out 3 [5, 3, 1] #1->3
23
+ * 4 2 [1, 2] #2->1 [5, 3, 2] 2 kick out 1 #2->3
24
+ * 5 4 [1, 2, 4] #4->2 [5,4] 4 kick out 2, 3 #4->2
25
+ *
26
+ * @param <T> generic type for objects to be stored or queried
27
+ */
23
28
24
29
public class MonotonicQueue <T extends Comparable <T >> {
25
30
private Deque <T > dq = new ArrayDeque <>(); // or LinkedList
26
31
27
32
/**
28
33
* Checks if queue is empty.
34
+ *
29
35
* @return boolean
30
36
*/
31
37
public boolean isEmpty () {
@@ -34,7 +40,8 @@ public boolean isEmpty() {
34
40
35
41
/**
36
42
* Push an object into the queue and maintain the non-increasing property.
37
- * @param obj to be inserted
43
+ *
44
+ * @param obj to be inserted.
38
45
*/
39
46
public void push (T obj ) {
40
47
Integer count = 0 ;
@@ -46,6 +53,7 @@ public void push(T obj) {
46
53
47
54
/**
48
55
* Returns the highest value stored in the queue.
56
+ *
49
57
* @return the first value of the queue.
50
58
*/
51
59
public T max () {
@@ -58,6 +66,8 @@ public T max() {
58
66
/**
59
67
* Returns the highest valued object in the queue; i.e the first object;
60
68
* Removal will only be done all representation of the object has been accounted for.
69
+ *
70
+ * @return The first value in the deque. (Highest valued object)
61
71
*/
62
72
public T pop () {
63
73
if (dq .isEmpty ()) {
0 commit comments