Skip to content

Commit 39ecf70

Browse files
authored
refactor: GenericArrayListQueue (#5355)
1 parent 4c65530 commit 39ecf70

File tree

2 files changed

+72
-47
lines changed

2 files changed

+72
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.datastructures.queues;
22

33
import java.util.ArrayList;
4+
import java.util.List;
45

56
/**
67
* This class implements a GenericArrayListQueue.
@@ -13,75 +14,44 @@
1314
public class GenericArrayListQueue<T> {
1415

1516
/**
16-
* The generic ArrayList for the queue T is the generic element
17+
* The generic List for the queue. T is the generic element type.
1718
*/
18-
ArrayList<T> elementList = new ArrayList<>();
19+
private final List<T> elementList = new ArrayList<>();
1920

2021
/**
21-
* Checks if the queue has elements (not empty).
22+
* Checks if the queue is empty.
2223
*
23-
* @return True if the queue has elements. False otherwise.
24+
* @return True if the queue is empty, false otherwise.
2425
*/
25-
private boolean hasElements() {
26-
return !elementList.isEmpty();
26+
private boolean isEmpty() {
27+
return elementList.isEmpty();
2728
}
2829

2930
/**
30-
* Checks what's at the front of the queue.
31+
* Returns the element at the front of the queue without removing it.
3132
*
32-
* @return If queue is not empty, element at the front of the queue.
33-
* Otherwise, null
33+
* @return The element at the front of the queue, or null if the queue is empty.
3434
*/
3535
public T peek() {
36-
T result = null;
37-
if (this.hasElements()) {
38-
result = elementList.get(0);
39-
}
40-
return result;
36+
return isEmpty() ? null : elementList.getFirst();
4137
}
4238

4339
/**
44-
* Inserts an element of type T to the queue.
40+
* Inserts an element of type T to the back of the queue.
4541
*
46-
* @param element of type T to be added
47-
* @return True if the element was added successfully
42+
* @param element the element to be added to the queue.
43+
* @return True if the element was added successfully.
4844
*/
4945
public boolean add(T element) {
5046
return elementList.add(element);
5147
}
5248

5349
/**
54-
* Retrieve what's at the front of the queue
50+
* Retrieves and removes the element at the front of the queue.
5551
*
56-
* @return If queue is not empty, element retrieved. Otherwise, null
52+
* @return The element removed from the front of the queue, or null if the queue is empty.
5753
*/
58-
public T pull() {
59-
T result = null;
60-
if (this.hasElements()) {
61-
result = elementList.remove(0);
62-
}
63-
return result;
64-
}
65-
66-
/**
67-
* Main method
68-
*
69-
* @param args Command line arguments
70-
*/
71-
public static void main(String[] args) {
72-
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
73-
System.out.println("Running...");
74-
assert queue.peek() == null;
75-
assert queue.pull() == null;
76-
assert queue.add(1);
77-
assert queue.peek() == 1;
78-
assert queue.add(2);
79-
assert queue.peek() == 1;
80-
assert queue.pull() == 1;
81-
assert queue.peek() == 2;
82-
assert queue.pull() == 2;
83-
assert queue.peek() == null;
84-
assert queue.pull() == null;
85-
System.out.println("Finished.");
54+
public T poll() {
55+
return isEmpty() ? null : elementList.removeFirst();
8656
}
8757
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.thealgorithms.datastructures.queues;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
class GenericArrayListQueueTest {
10+
11+
@Test
12+
void testAdd() {
13+
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
14+
assertTrue(queue.add(10));
15+
assertTrue(queue.add(20));
16+
}
17+
18+
@Test
19+
void testPeek() {
20+
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
21+
assertNull(queue.peek());
22+
23+
queue.add(10);
24+
queue.add(20);
25+
26+
assertEquals(10, queue.peek());
27+
queue.poll();
28+
assertEquals(20, queue.peek());
29+
}
30+
31+
@Test
32+
void testPoll() {
33+
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
34+
assertNull(queue.poll());
35+
36+
queue.add(10);
37+
queue.add(20);
38+
39+
assertEquals(10, queue.poll());
40+
assertEquals(20, queue.poll());
41+
assertNull(queue.poll());
42+
}
43+
44+
@Test
45+
void testIsEmpty() {
46+
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
47+
assertNull(queue.peek());
48+
assertNull(queue.poll());
49+
50+
queue.add(30);
51+
assertEquals(30, queue.peek());
52+
assertEquals(30, queue.poll());
53+
assertNull(queue.peek());
54+
}
55+
}

0 commit comments

Comments
 (0)