Update GenericArrayListQueue.java

Documentations added
This commit is contained in:
Daher Daher 2019-05-27 13:39:33 +03:00 committed by GitHub
parent a3e0cf85b7
commit 12d240414d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,29 +2,68 @@ package DataStructures.Queues;
import java.util.ArrayList; import java.util.ArrayList;
/**
* This class implements a GenericArrayListQueue.
* <p>
* A GenericArrayListQueue data structure functions the same as any specific-typed queue.
* The GenericArrayListQueue holds elemets 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.
*
*/
public class GenericArrayListQueue<T> { public class GenericArrayListQueue<T> {
/**
* The generic ArrayList for the queue
* T is the generic element
*/
ArrayList<T> _queue = new ArrayList<T>(); ArrayList<T> _queue = new ArrayList<T>();
/**
* Checks if the queue has elements (not empty)
*
* @return True if the queue has elements. False otherwise.
*/
private boolean hasElements() { private boolean hasElements() {
return !_queue.isEmpty(); return !_queue.isEmpty();
} }
/**
* Checks what's at the front of the queue
*
* @return If queue is not empty, element at the front of the queue. Otherwise, null
*/
public T peek() { public T peek() {
T result = null; T result = null;
if(this.hasElements()) { result = _queue.get(0); } if(this.hasElements()) { result = _queue.get(0); }
return result; return result;
} }
/**
* Inserts an element of type T to the queue.
*
* @param element of type T to be added
* @return True if the element was added successfully
*/
public boolean add(T element) { public boolean add(T element) {
return _queue.add(element); return _queue.add(element);
} }
/**
* Retrieve what's at the front of the queue
*
* @return If queue is not empty, element retrieved. Otherwise, null
*/
public T poll() { public T poll() {
T result = null; T result = null;
if(this.hasElements()) { result = _queue.remove(0); } if(this.hasElements()) { result = _queue.remove(0); }
return result; return result;
} }
/**
* Main method
*
* @param args Command line arguments
*/
public static void main(String[] args) { public static void main(String[] args) {
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<Integer>(); GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<Integer>();
System.out.println("Running..."); System.out.println("Running...");