|
1 | 1 | package com.thealgorithms.datastructures.queues;
|
2 | 2 |
|
3 | 3 | import java.util.ArrayList;
|
| 4 | +import java.util.List; |
4 | 5 |
|
5 | 6 | /**
|
6 | 7 | * This class implements a GenericArrayListQueue.
|
|
13 | 14 | public class GenericArrayListQueue<T> {
|
14 | 15 |
|
15 | 16 | /**
|
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. |
17 | 18 | */
|
18 |
| - ArrayList<T> elementList = new ArrayList<>(); |
| 19 | + private final List<T> elementList = new ArrayList<>(); |
19 | 20 |
|
20 | 21 | /**
|
21 |
| - * Checks if the queue has elements (not empty). |
| 22 | + * Checks if the queue is empty. |
22 | 23 | *
|
23 |
| - * @return True if the queue has elements. False otherwise. |
| 24 | + * @return True if the queue is empty, false otherwise. |
24 | 25 | */
|
25 |
| - private boolean hasElements() { |
26 |
| - return !elementList.isEmpty(); |
| 26 | + private boolean isEmpty() { |
| 27 | + return elementList.isEmpty(); |
27 | 28 | }
|
28 | 29 |
|
29 | 30 | /**
|
30 |
| - * Checks what's at the front of the queue. |
| 31 | + * Returns the element at the front of the queue without removing it. |
31 | 32 | *
|
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. |
34 | 34 | */
|
35 | 35 | 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(); |
41 | 37 | }
|
42 | 38 |
|
43 | 39 | /**
|
44 |
| - * Inserts an element of type T to the queue. |
| 40 | + * Inserts an element of type T to the back of the queue. |
45 | 41 | *
|
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. |
48 | 44 | */
|
49 | 45 | public boolean add(T element) {
|
50 | 46 | return elementList.add(element);
|
51 | 47 | }
|
52 | 48 |
|
53 | 49 | /**
|
54 |
| - * Retrieve what's at the front of the queue |
| 50 | + * Retrieves and removes the element at the front of the queue. |
55 | 51 | *
|
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. |
57 | 53 | */
|
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(); |
86 | 56 | }
|
87 | 57 | }
|
0 commit comments