Enhance docs, add tests in GenericArrayListQueue (#6016)

This commit is contained in:
Hardik Pawar
2024-10-26 13:48:38 +05:30
committed by GitHub
parent ab9a55272d
commit e1a0155966
2 changed files with 74 additions and 26 deletions

View File

@ -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();