1
1
package com .thealgorithms .datastructures .queues ;
2
2
3
- // This program implements the concept of CircularQueue in Java
4
- // Link to the concept: (https://en.wikipedia.org/wiki/Circular_buffer)
3
+ /**
4
+ * The CircularQueue class represents a generic circular queue data structure that uses an array to
5
+ * store elements. This queue allows efficient utilization of space by wrapping around the array,
6
+ * thus avoiding the need to shift elements during enqueue and dequeue operations.
7
+ *
8
+ * <p>When the queue reaches its maximum capacity, further enqueues will raise an exception.
9
+ * Similarly, attempts to dequeue or peek from an empty queue will also result in an exception.
10
+ *
11
+ * <p>Reference: <a href="https://en.wikipedia.org/wiki/Circular_buffer">Circular Buffer</a>
12
+ *
13
+ * <p>Usage Example:
14
+ * <pre>
15
+ * CircularQueue<Integer> queue = new CircularQueue<>(3);
16
+ * queue.enQueue(1);
17
+ * queue.enQueue(2);
18
+ * queue.enQueue(3);
19
+ * queue.deQueue(); // Removes 1
20
+ * queue.enQueue(4); // Wraps around and places 4 at the position of removed 1
21
+ * </pre>
22
+ *
23
+ * @param <T> the type of elements in this queue
24
+ */
5
25
public class CircularQueue <T > {
6
26
private T [] array ;
7
27
private int topOfQueue ;
8
28
private int beginningOfQueue ;
9
29
private final int size ;
10
30
private int currentSize ;
11
31
32
+ /**
33
+ * Constructs a CircularQueue with a specified capacity.
34
+ *
35
+ * @param size the maximum number of elements this queue can hold
36
+ * @throws IllegalArgumentException if the size is less than 1
37
+ */
12
38
@ SuppressWarnings ("unchecked" )
13
39
public CircularQueue (int size ) {
40
+ if (size < 1 ) {
41
+ throw new IllegalArgumentException ("Size must be greater than 0" );
42
+ }
14
43
this .array = (T []) new Object [size ];
15
44
this .topOfQueue = -1 ;
16
45
this .beginningOfQueue = -1 ;
17
46
this .size = size ;
18
47
this .currentSize = 0 ;
19
48
}
20
49
50
+ /**
51
+ * Checks if the queue is empty.
52
+ *
53
+ * @return {@code true} if the queue is empty; {@code false} otherwise
54
+ */
21
55
public boolean isEmpty () {
22
56
return currentSize == 0 ;
23
57
}
24
58
59
+ /**
60
+ * Checks if the queue is full.
61
+ *
62
+ * @return {@code true} if the queue has reached its maximum capacity; {@code false} otherwise
63
+ */
25
64
public boolean isFull () {
26
65
return currentSize == size ;
27
66
}
28
67
68
+ /**
69
+ * Adds a new element to the queue. If the queue is full, an exception is thrown.
70
+ *
71
+ * @param value the element to be added to the queue
72
+ * @throws IllegalStateException if the queue is already full
73
+ */
29
74
public void enQueue (T value ) {
30
75
if (isFull ()) {
31
76
throw new IllegalStateException ("Queue is full" );
@@ -38,12 +83,18 @@ public void enQueue(T value) {
38
83
currentSize ++;
39
84
}
40
85
86
+ /**
87
+ * Removes and returns the element at the front of the queue.
88
+ *
89
+ * @return the element at the front of the queue
90
+ * @throws IllegalStateException if the queue is empty
91
+ */
41
92
public T deQueue () {
42
93
if (isEmpty ()) {
43
94
throw new IllegalStateException ("Queue is empty" );
44
95
}
45
96
T removedValue = array [beginningOfQueue ];
46
- array [beginningOfQueue ] = null ; // Optional: Help GC
97
+ array [beginningOfQueue ] = null ; // Optional: Nullify to help garbage collection
47
98
beginningOfQueue = (beginningOfQueue + 1 ) % size ;
48
99
currentSize --;
49
100
if (isEmpty ()) {
@@ -53,49 +104,35 @@ public T deQueue() {
53
104
return removedValue ;
54
105
}
55
106
107
+ /**
108
+ * Returns the element at the front of the queue without removing it.
109
+ *
110
+ * @return the element at the front of the queue
111
+ * @throws IllegalStateException if the queue is empty
112
+ */
56
113
public T peek () {
57
114
if (isEmpty ()) {
58
115
throw new IllegalStateException ("Queue is empty" );
59
116
}
60
117
return array [beginningOfQueue ];
61
118
}
62
119
120
+ /**
121
+ * Deletes the entire queue by resetting all elements and pointers.
122
+ */
63
123
public void deleteQueue () {
64
124
array = null ;
65
125
beginningOfQueue = -1 ;
66
126
topOfQueue = -1 ;
67
127
currentSize = 0 ;
68
128
}
69
129
130
+ /**
131
+ * Returns the current number of elements in the queue.
132
+ *
133
+ * @return the number of elements currently in the queue
134
+ */
70
135
public int size () {
71
136
return currentSize ;
72
137
}
73
-
74
- public static void main (String [] args ) {
75
- CircularQueue <Integer > cq = new CircularQueue <>(5 );
76
- System .out .println (cq .isEmpty ()); // true
77
- System .out .println (cq .isFull ()); // false
78
- cq .enQueue (1 );
79
- cq .enQueue (2 );
80
- cq .enQueue (3 );
81
- cq .enQueue (4 );
82
- cq .enQueue (5 );
83
-
84
- System .out .println (cq .deQueue ()); // 1
85
- System .out .println (cq .deQueue ()); // 2
86
- System .out .println (cq .deQueue ()); // 3
87
- System .out .println (cq .deQueue ()); // 4
88
- System .out .println (cq .deQueue ()); // 5
89
-
90
- System .out .println (cq .isFull ()); // false
91
- System .out .println (cq .isEmpty ()); // true
92
- cq .enQueue (6 );
93
- cq .enQueue (7 );
94
- cq .enQueue (8 );
95
-
96
- System .out .println (cq .peek ()); // 6
97
- System .out .println (cq .peek ()); // 6
98
-
99
- cq .deleteQueue ();
100
- }
101
138
}
0 commit comments