Skip to content

Commit e756a7d

Browse files
authored
refactor: CircularQueue (#5354)
1 parent f5c0314 commit e756a7d

File tree

2 files changed

+163
-63
lines changed

2 files changed

+163
-63
lines changed

src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java

+57-63
Original file line numberDiff line numberDiff line change
@@ -2,106 +2,100 @@
22

33
// This program implements the concept of CircularQueue in Java
44
// Link to the concept: (https://en.wikipedia.org/wiki/Circular_buffer)
5-
public class CircularQueue {
6-
7-
int[] arr;
8-
int topOfQueue;
9-
int beginningOfQueue;
10-
int size;
5+
public class CircularQueue<T> {
6+
private T[] array;
7+
private int topOfQueue;
8+
private int beginningOfQueue;
9+
private final int size;
10+
private int currentSize;
1111

12+
@SuppressWarnings("unchecked")
1213
public CircularQueue(int size) {
13-
arr = new int[size];
14-
topOfQueue = -1;
15-
beginningOfQueue = -1;
14+
this.array = (T[]) new Object[size];
15+
this.topOfQueue = -1;
16+
this.beginningOfQueue = -1;
1617
this.size = size;
18+
this.currentSize = 0;
1719
}
1820

1921
public boolean isEmpty() {
20-
return beginningOfQueue == -1;
22+
return currentSize == 0;
2123
}
2224

2325
public boolean isFull() {
24-
if (topOfQueue + 1 == beginningOfQueue) {
25-
return true;
26-
} else {
27-
return topOfQueue == size - 1 && beginningOfQueue == 0;
28-
}
26+
return currentSize == size;
2927
}
3028

31-
public void enQueue(int value) {
29+
public void enQueue(T value) {
3230
if (isFull()) {
33-
System.out.println("The Queue is full!");
34-
} else if (isEmpty()) {
31+
throw new IllegalStateException("Queue is full");
32+
}
33+
if (isEmpty()) {
3534
beginningOfQueue = 0;
36-
topOfQueue++;
37-
arr[topOfQueue] = value;
38-
System.out.println(value + " has been successfully inserted!");
39-
} else {
40-
if (topOfQueue + 1 == size) {
41-
topOfQueue = 0;
42-
} else {
43-
topOfQueue++;
44-
}
45-
arr[topOfQueue] = value;
46-
System.out.println(value + " has been successfully inserted!");
4735
}
36+
topOfQueue = (topOfQueue + 1) % size;
37+
array[topOfQueue] = value;
38+
currentSize++;
4839
}
4940

50-
public int deQueue() {
41+
public T deQueue() {
42+
if (isEmpty()) {
43+
throw new IllegalStateException("Queue is empty");
44+
}
45+
T removedValue = array[beginningOfQueue];
46+
array[beginningOfQueue] = null; // Optional: Help GC
47+
beginningOfQueue = (beginningOfQueue + 1) % size;
48+
currentSize--;
5149
if (isEmpty()) {
52-
System.out.println("The Queue is Empty!");
53-
return -1;
54-
} else {
55-
int res = arr[beginningOfQueue];
56-
arr[beginningOfQueue] = Integer.MIN_VALUE;
57-
if (beginningOfQueue == topOfQueue) {
58-
beginningOfQueue = -1;
59-
topOfQueue = -1;
60-
} else if (beginningOfQueue + 1 == size) {
61-
beginningOfQueue = 0;
62-
} else {
63-
beginningOfQueue++;
64-
}
65-
return res;
50+
beginningOfQueue = -1;
51+
topOfQueue = -1;
6652
}
53+
return removedValue;
6754
}
6855

69-
public int peek() {
56+
public T peek() {
7057
if (isEmpty()) {
71-
System.out.println("The Queue is Empty!");
72-
return -1;
73-
} else {
74-
return arr[beginningOfQueue];
58+
throw new IllegalStateException("Queue is empty");
7559
}
60+
return array[beginningOfQueue];
7661
}
7762

7863
public void deleteQueue() {
79-
arr = null;
80-
System.out.println("The Queue is deleted!");
64+
array = null;
65+
beginningOfQueue = -1;
66+
topOfQueue = -1;
67+
currentSize = 0;
68+
}
69+
70+
public int size() {
71+
return currentSize;
8172
}
8273

8374
public static void main(String[] args) {
84-
CircularQueue cq = new CircularQueue(5);
85-
System.out.println(cq.isEmpty());
86-
System.out.println(cq.isFull());
75+
CircularQueue<Integer> cq = new CircularQueue<>(5);
76+
System.out.println(cq.isEmpty()); // true
77+
System.out.println(cq.isFull()); // false
8778
cq.enQueue(1);
8879
cq.enQueue(2);
8980
cq.enQueue(3);
9081
cq.enQueue(4);
9182
cq.enQueue(5);
9283

93-
System.out.println(cq.deQueue());
94-
System.out.println(cq.deQueue());
95-
System.out.println(cq.deQueue());
96-
System.out.println(cq.deQueue());
97-
System.out.println(cq.deQueue());
98-
System.out.println(cq.isFull());
99-
System.out.println(cq.isEmpty());
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
10092
cq.enQueue(6);
10193
cq.enQueue(7);
10294
cq.enQueue(8);
103-
System.out.println(cq.peek());
104-
System.out.println(cq.peek());
95+
96+
System.out.println(cq.peek()); // 6
97+
System.out.println(cq.peek()); // 6
98+
10599
cq.deleteQueue();
106100
}
107101
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.thealgorithms.datastructures.queues;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
class CircularQueueTest {
10+
11+
@Test
12+
void testEnQueue() {
13+
CircularQueue<Integer> cq = new CircularQueue<>(3);
14+
cq.enQueue(1);
15+
cq.enQueue(2);
16+
cq.enQueue(3);
17+
18+
assertEquals(1, cq.peek());
19+
assertTrue(cq.isFull());
20+
}
21+
22+
@Test
23+
void testDeQueue() {
24+
CircularQueue<Integer> cq = new CircularQueue<>(3);
25+
cq.enQueue(1);
26+
cq.enQueue(2);
27+
cq.enQueue(3);
28+
29+
assertEquals(1, cq.deQueue());
30+
assertEquals(2, cq.peek());
31+
assertFalse(cq.isFull());
32+
}
33+
34+
@Test
35+
void testIsEmpty() {
36+
CircularQueue<Integer> cq = new CircularQueue<>(3);
37+
assertTrue(cq.isEmpty());
38+
39+
cq.enQueue(1);
40+
assertFalse(cq.isEmpty());
41+
}
42+
43+
@Test
44+
void testIsFull() {
45+
CircularQueue<Integer> cq = new CircularQueue<>(2);
46+
cq.enQueue(1);
47+
cq.enQueue(2);
48+
assertTrue(cq.isFull());
49+
50+
cq.deQueue();
51+
assertFalse(cq.isFull());
52+
}
53+
54+
@Test
55+
void testPeek() {
56+
CircularQueue<Integer> cq = new CircularQueue<>(3);
57+
cq.enQueue(1);
58+
cq.enQueue(2);
59+
60+
assertEquals(1, cq.peek());
61+
assertEquals(1, cq.peek()); // Ensure peek doesn't remove the element
62+
}
63+
64+
@Test
65+
void testDeleteQueue() {
66+
CircularQueue<Integer> cq = new CircularQueue<>(3);
67+
cq.enQueue(1);
68+
cq.enQueue(2);
69+
cq.deleteQueue();
70+
71+
org.junit.jupiter.api.Assertions.assertThrows(IllegalStateException.class, cq::peek);
72+
}
73+
74+
@Test
75+
void testEnQueueOnFull() {
76+
CircularQueue<Integer> cq = new CircularQueue<>(2);
77+
cq.enQueue(1);
78+
cq.enQueue(2);
79+
80+
org.junit.jupiter.api.Assertions.assertThrows(IllegalStateException.class, () -> cq.enQueue(3));
81+
}
82+
83+
@Test
84+
void testDeQueueOnEmpty() {
85+
CircularQueue<Integer> cq = new CircularQueue<>(2);
86+
org.junit.jupiter.api.Assertions.assertThrows(IllegalStateException.class, cq::deQueue);
87+
}
88+
89+
@Test
90+
void testPeekOnEmpty() {
91+
CircularQueue<Integer> cq = new CircularQueue<>(2);
92+
org.junit.jupiter.api.Assertions.assertThrows(IllegalStateException.class, cq::peek);
93+
}
94+
95+
@Test
96+
void testSize() {
97+
CircularQueue<Integer> cq = new CircularQueue<>(3);
98+
cq.enQueue(1);
99+
cq.enQueue(2);
100+
101+
assertEquals(2, cq.size());
102+
103+
cq.deQueue();
104+
assertEquals(1, cq.size());
105+
}
106+
}

0 commit comments

Comments
 (0)