mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 17:29:31 +08:00
Enhance docs, add tests in GenericArrayListQueue
(#6016)
This commit is contained in:
@ -4,43 +4,46 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class implements a GenericArrayListQueue.
|
||||
* This class implements a GenericArrayListQueue, a queue data structure that
|
||||
* holds elements of any type specified at runtime, allowing flexibility in the type
|
||||
* of elements it stores.
|
||||
*
|
||||
* A GenericArrayListQueue data structure functions the same as any
|
||||
* specific-typed queue. The GenericArrayListQueue holds elements of types
|
||||
* to-be-specified at runtime. The elements that are added first are the first
|
||||
* to be removed (FIFO). New elements are added to the back/rear of the queue.
|
||||
* <p>The GenericArrayListQueue operates on a First-In-First-Out (FIFO) basis, where
|
||||
* elements added first are the first to be removed. New elements are added to the back
|
||||
* (or rear) of the queue, while removal of elements occurs from the front.
|
||||
*
|
||||
* @param <T> The type of elements held in this queue.
|
||||
*/
|
||||
public class GenericArrayListQueue<T> {
|
||||
|
||||
/**
|
||||
* The generic List for the queue. T is the generic element type.
|
||||
* A list that stores the queue's elements in insertion order.
|
||||
*/
|
||||
private final List<T> elementList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Checks if the queue is empty.
|
||||
*
|
||||
* @return True if the queue is empty, false otherwise.
|
||||
* @return {@code true} if the queue has no elements; {@code false} otherwise.
|
||||
*/
|
||||
private boolean isEmpty() {
|
||||
public boolean isEmpty() {
|
||||
return elementList.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the element at the front of the queue without removing it.
|
||||
* Retrieves, but does not remove, the element at the front of the queue.
|
||||
*
|
||||
* @return The element at the front of the queue, or null if the queue is empty.
|
||||
* @return The element at the front of the queue, or {@code null} if the queue is empty.
|
||||
*/
|
||||
public T peek() {
|
||||
return isEmpty() ? null : elementList.getFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts an element of type T to the back of the queue.
|
||||
* Inserts an element at the back of the queue.
|
||||
*
|
||||
* @param element the element to be added to the queue.
|
||||
* @return True if the element was added successfully.
|
||||
* @param element The element to be added to the queue.
|
||||
* @return {@code true} if the element was successfully added.
|
||||
*/
|
||||
public boolean add(T element) {
|
||||
return elementList.add(element);
|
||||
@ -49,7 +52,7 @@ public class GenericArrayListQueue<T> {
|
||||
/**
|
||||
* Retrieves and removes the element at the front of the queue.
|
||||
*
|
||||
* @return The element removed from the front of the queue, or null if the queue is empty.
|
||||
* @return The element removed from the front of the queue, or {@code null} if the queue is empty.
|
||||
*/
|
||||
public T poll() {
|
||||
return isEmpty() ? null : elementList.removeFirst();
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.thealgorithms.datastructures.queues;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@ -13,43 +14,87 @@ class GenericArrayListQueueTest {
|
||||
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
|
||||
assertTrue(queue.add(10));
|
||||
assertTrue(queue.add(20));
|
||||
assertEquals(10, queue.peek()); // Ensure the first added element is at the front
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPeek() {
|
||||
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
|
||||
assertNull(queue.peek());
|
||||
assertNull(queue.peek(), "Peek should return null for an empty queue");
|
||||
|
||||
queue.add(10);
|
||||
queue.add(20);
|
||||
|
||||
assertEquals(10, queue.peek());
|
||||
assertEquals(10, queue.peek(), "Peek should return the first element (10)");
|
||||
queue.poll();
|
||||
assertEquals(20, queue.peek());
|
||||
assertEquals(20, queue.peek(), "Peek should return the next element (20) after poll");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPoll() {
|
||||
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
|
||||
assertNull(queue.poll());
|
||||
assertNull(queue.poll(), "Poll should return null for an empty queue");
|
||||
|
||||
queue.add(10);
|
||||
queue.add(20);
|
||||
|
||||
assertEquals(10, queue.poll());
|
||||
assertEquals(20, queue.poll());
|
||||
assertNull(queue.poll());
|
||||
assertEquals(10, queue.poll(), "Poll should return and remove the first element (10)");
|
||||
assertEquals(20, queue.poll(), "Poll should return and remove the next element (20)");
|
||||
assertNull(queue.poll(), "Poll should return null when queue is empty after removals");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsEmpty() {
|
||||
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
|
||||
assertNull(queue.peek());
|
||||
assertNull(queue.poll());
|
||||
assertTrue(queue.isEmpty(), "Queue should initially be empty");
|
||||
|
||||
queue.add(30);
|
||||
assertEquals(30, queue.peek());
|
||||
assertEquals(30, queue.poll());
|
||||
assertNull(queue.peek());
|
||||
assertFalse(queue.isEmpty(), "Queue should not be empty after adding an element");
|
||||
queue.poll();
|
||||
assertTrue(queue.isEmpty(), "Queue should be empty after removing the only element");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClearQueueAndReuse() {
|
||||
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
|
||||
queue.add(5);
|
||||
queue.add(10);
|
||||
queue.poll();
|
||||
queue.poll(); // Remove all elements
|
||||
|
||||
assertTrue(queue.isEmpty(), "Queue should be empty after all elements are removed");
|
||||
assertNull(queue.peek(), "Peek should return null on an empty queue after clear");
|
||||
assertTrue(queue.add(15), "Queue should be reusable after being emptied");
|
||||
assertEquals(15, queue.peek(), "Newly added element should be accessible in the empty queue");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOrderMaintained() {
|
||||
GenericArrayListQueue<String> queue = new GenericArrayListQueue<>();
|
||||
queue.add("First");
|
||||
queue.add("Second");
|
||||
queue.add("Third");
|
||||
|
||||
assertEquals("First", queue.poll(), "Order should be maintained; expected 'First'");
|
||||
assertEquals("Second", queue.poll(), "Order should be maintained; expected 'Second'");
|
||||
assertEquals("Third", queue.poll(), "Order should be maintained; expected 'Third'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testVariousDataTypes() {
|
||||
GenericArrayListQueue<Double> queue = new GenericArrayListQueue<>();
|
||||
queue.add(1.1);
|
||||
queue.add(2.2);
|
||||
|
||||
assertEquals(1.1, queue.peek(), "Queue should handle Double data type correctly");
|
||||
assertEquals(1.1, queue.poll(), "Poll should return correct Double value");
|
||||
assertEquals(2.2, queue.peek(), "Peek should show next Double value in the queue");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEmptyPollAndPeekBehavior() {
|
||||
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
|
||||
assertNull(queue.peek(), "Peek on an empty queue should return null");
|
||||
assertNull(queue.poll(), "Poll on an empty queue should return null");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user