diff --git a/data_structures/Queues/GenericArrayListQueue.java b/data_structures/Queues/GenericArrayListQueue.java new file mode 100644 index 00000000..0d35227f --- /dev/null +++ b/data_structures/Queues/GenericArrayListQueue.java @@ -0,0 +1,42 @@ +import java.util.ArrayList; + +public class GenericArrayListQueue { + ArrayList _queue = new ArrayList(); + + private boolean hasElements() { + return !_queue.isEmpty(); + } + + public T peek() { + T result = null; + if(this.hasElements()) { result = _queue.get(0); } + return result; + } + + public boolean add(T element) { + return _queue.add(element); + } + + public T poll() { + T result = null; + if(this.hasElements()) { result = _queue.remove(0); } + return result; + } + + public static void main(String[] args) { + GenericArrayListQueue queue = new GenericArrayListQueue(); + System.out.println("Running..."); + assert queue.peek() == null; + assert queue.poll() == null; + assert queue.add(1) == true; + assert queue.peek() == 1; + assert queue.add(2) == true; + assert queue.peek() == 1; + assert queue.poll() == 1; + assert queue.peek() == 2; + assert queue.poll() == 2; + assert queue.peek() == null; + assert queue.poll() == null; + System.out.println("Finished."); + } +}