|
2 | 2 |
|
3 | 3 | import java.util.Iterator;
|
4 | 4 | import java.util.NoSuchElementException;
|
5 |
| -import java.util.StringJoiner; |
6 | 5 |
|
7 | 6 | public class LinkedQueue<T> implements Iterable<T> {
|
8 | 7 |
|
9 |
| - static class Node<T> { |
10 |
| - |
| 8 | + /** |
| 9 | + * Node class representing each element in the queue. |
| 10 | + */ |
| 11 | + private static class Node<T> { |
11 | 12 | T data;
|
12 | 13 | Node<T> next;
|
13 | 14 |
|
14 |
| - Node() { |
15 |
| - this(null); |
16 |
| - } |
17 |
| - |
18 | 15 | Node(T data) {
|
19 |
| - this(data, null); |
20 |
| - } |
21 |
| - |
22 |
| - Node(T data, Node<T> next) { |
23 | 16 | this.data = data;
|
24 |
| - this.next = next; |
| 17 | + this.next = null; |
25 | 18 | }
|
26 | 19 | }
|
27 | 20 |
|
28 |
| - /** |
29 |
| - * Front of Queue |
30 |
| - */ |
31 |
| - private Node<T> front; |
32 |
| - |
33 |
| - /** |
34 |
| - * Rear of Queue |
35 |
| - */ |
36 |
| - private Node<T> rear; |
| 21 | + private Node<T> front; // Front of the queue |
| 22 | + private Node<T> rear; // Rear of the queue |
| 23 | + private int size; // Size of the queue |
37 | 24 |
|
38 | 25 | /**
|
39 |
| - * Size of Queue |
40 |
| - */ |
41 |
| - private int size; |
42 |
| - |
43 |
| - /** |
44 |
| - * Init LinkedQueue |
| 26 | + * Initializes an empty LinkedQueue. |
45 | 27 | */
|
46 | 28 | public LinkedQueue() {
|
47 |
| - |
48 |
| - front = new Node<>(); |
49 |
| - rear = front; |
| 29 | + front = null; |
| 30 | + rear = null; |
| 31 | + size = 0; |
50 | 32 | }
|
51 | 33 |
|
52 | 34 | /**
|
53 |
| - * Check if queue is empty |
| 35 | + * Checks if the queue is empty. |
54 | 36 | *
|
55 |
| - * @return true if queue is empty, otherwise false |
| 37 | + * @return true if the queue is empty, otherwise false. |
56 | 38 | */
|
57 | 39 | public boolean isEmpty() {
|
58 | 40 | return size == 0;
|
59 | 41 | }
|
60 | 42 |
|
61 | 43 | /**
|
62 |
| - * Add element to rear of queue |
| 44 | + * Adds an element to the rear of the queue. |
63 | 45 | *
|
64 |
| - * @param data insert value |
| 46 | + * @param data the element to insert. |
| 47 | + * @throws IllegalArgumentException if data is null. |
65 | 48 | */
|
66 | 49 | public void enqueue(T data) {
|
| 50 | + if (data == null) { |
| 51 | + throw new IllegalArgumentException("Cannot enqueue null data"); |
| 52 | + } |
| 53 | + |
67 | 54 | Node<T> newNode = new Node<>(data);
|
68 | 55 |
|
69 |
| - rear.next = newNode; |
| 56 | + if (isEmpty()) { |
| 57 | + front = newNode; |
| 58 | + } else { |
| 59 | + rear.next = newNode; |
| 60 | + } |
70 | 61 | rear = newNode;
|
71 |
| - /* make rear point at last node */ |
72 | 62 | size++;
|
73 | 63 | }
|
74 | 64 |
|
75 | 65 | /**
|
76 |
| - * Remove element at the front of queue |
| 66 | + * Removes and returns the element at the front of the queue. |
77 | 67 | *
|
78 |
| - * @return element at the front of queue |
| 68 | + * @return the element at the front of the queue. |
| 69 | + * @throws NoSuchElementException if the queue is empty. |
79 | 70 | */
|
80 | 71 | public T dequeue() {
|
81 | 72 | if (isEmpty()) {
|
82 |
| - throw new NoSuchElementException("queue is empty"); |
| 73 | + throw new NoSuchElementException("Queue is empty"); |
83 | 74 | }
|
84 |
| - Node<T> destroy = front.next; |
85 |
| - T retValue = destroy.data; |
86 |
| - front.next = front.next.next; |
87 |
| - /* clear let GC do it's work */ |
| 75 | + |
| 76 | + T retValue = front.data; |
| 77 | + front = front.next; |
88 | 78 | size--;
|
89 | 79 |
|
90 | 80 | if (isEmpty()) {
|
91 |
| - front = rear; |
| 81 | + rear = null; |
92 | 82 | }
|
93 | 83 |
|
94 | 84 | return retValue;
|
95 | 85 | }
|
96 | 86 |
|
97 | 87 | /**
|
98 |
| - * Peek element at the front of queue without removing |
| 88 | + * Returns the element at the front of the queue without removing it. |
99 | 89 | *
|
100 |
| - * @return element at the front |
| 90 | + * @return the element at the front of the queue. |
| 91 | + * @throws NoSuchElementException if the queue is empty. |
101 | 92 | */
|
102 | 93 | public T peekFront() {
|
103 | 94 | if (isEmpty()) {
|
104 |
| - throw new NoSuchElementException("queue is empty"); |
| 95 | + throw new NoSuchElementException("Queue is empty"); |
105 | 96 | }
|
106 |
| - return front.next.data; |
| 97 | + return front.data; |
107 | 98 | }
|
108 | 99 |
|
109 | 100 | /**
|
110 |
| - * Peek element at the rear of queue without removing |
| 101 | + * Returns the element at the rear of the queue without removing it. |
111 | 102 | *
|
112 |
| - * @return element at the front |
| 103 | + * @return the element at the rear of the queue. |
| 104 | + * @throws NoSuchElementException if the queue is empty. |
113 | 105 | */
|
114 | 106 | public T peekRear() {
|
115 | 107 | if (isEmpty()) {
|
116 |
| - throw new NoSuchElementException("queue is empty"); |
| 108 | + throw new NoSuchElementException("Queue is empty"); |
117 | 109 | }
|
118 | 110 | return rear.data;
|
119 | 111 | }
|
120 | 112 |
|
121 | 113 | /**
|
122 |
| - * Peeks the element at the index and |
123 |
| - * returns the value |
124 |
| - * @param pos at which to peek |
| 114 | + * Returns the element at the specified position (1-based index). |
| 115 | + * |
| 116 | + * @param pos the position to peek at (1-based index). |
| 117 | + * @return the element at the specified position. |
| 118 | + * @throws IndexOutOfBoundsException if the position is out of range. |
125 | 119 | */
|
126 |
| - |
127 | 120 | public T peek(int pos) {
|
128 |
| - if (pos > size) { |
129 |
| - throw new IndexOutOfBoundsException("Position %s out of range!".formatted(pos)); |
| 121 | + if (pos < 1 || pos > size) { |
| 122 | + throw new IndexOutOfBoundsException("Position " + pos + " out of range!"); |
130 | 123 | }
|
| 124 | + |
131 | 125 | Node<T> node = front;
|
132 |
| - while (pos-- > 0) { |
| 126 | + for (int i = 1; i < pos; i++) { |
133 | 127 | node = node.next;
|
134 | 128 | }
|
135 | 129 | return node.data;
|
136 | 130 | }
|
137 | 131 |
|
138 | 132 | /**
|
139 |
| - * Node iterator, allows to travel through |
140 |
| - * the nodes using for() loop or forEach(Consumer) |
| 133 | + * Returns an iterator over the elements in the queue. |
| 134 | + * |
| 135 | + * @return an iterator over the elements in the queue. |
141 | 136 | */
|
142 |
| - |
143 | 137 | @Override
|
144 | 138 | public Iterator<T> iterator() {
|
145 | 139 | return new Iterator<>() {
|
146 |
| - Node<T> node = front; |
| 140 | + private Node<T> current = front; |
147 | 141 |
|
148 | 142 | @Override
|
149 | 143 | public boolean hasNext() {
|
150 |
| - return node.next != null; |
| 144 | + return current != null; |
151 | 145 | }
|
152 | 146 |
|
153 | 147 | @Override
|
154 | 148 | public T next() {
|
155 |
| - if (hasNext()) { |
156 |
| - node = node.next; |
157 |
| - return node.data; |
| 149 | + if (!hasNext()) { |
| 150 | + throw new NoSuchElementException(); |
158 | 151 | }
|
159 |
| - throw new NoSuchElementException(); |
| 152 | + |
| 153 | + T data = current.data; |
| 154 | + current = current.next; |
| 155 | + return data; |
160 | 156 | }
|
161 | 157 | };
|
162 | 158 | }
|
163 | 159 |
|
164 | 160 | /**
|
165 |
| - * Return size of queue |
| 161 | + * Returns the size of the queue. |
166 | 162 | *
|
167 |
| - * @return size of queue |
| 163 | + * @return the size of the queue. |
168 | 164 | */
|
169 | 165 | public int size() {
|
170 | 166 | return size;
|
171 | 167 | }
|
172 | 168 |
|
173 | 169 | /**
|
174 |
| - * Clear all nodes in queue |
| 170 | + * Clears all elements from the queue. |
175 | 171 | */
|
176 | 172 | public void clear() {
|
177 |
| - while (size > 0) { |
178 |
| - dequeue(); |
179 |
| - } |
| 173 | + front = null; |
| 174 | + rear = null; |
| 175 | + size = 0; |
180 | 176 | }
|
181 | 177 |
|
| 178 | + /** |
| 179 | + * Returns a string representation of the queue. |
| 180 | + * |
| 181 | + * @return a string representation of the queue. |
| 182 | + */ |
182 | 183 | @Override
|
183 | 184 | public String toString() {
|
184 |
| - StringJoiner join = new StringJoiner(", "); // separator of ', ' |
185 |
| - Node<T> travel = front; |
186 |
| - while ((travel = travel.next) != null) { |
187 |
| - join.add(String.valueOf(travel.data)); |
| 185 | + if (isEmpty()) { |
| 186 | + return "[]"; |
| 187 | + } |
| 188 | + |
| 189 | + StringBuilder sb = new StringBuilder("["); |
| 190 | + Node<T> current = front; |
| 191 | + while (current != null) { |
| 192 | + sb.append(current.data); |
| 193 | + if (current.next != null) { |
| 194 | + sb.append(", "); |
| 195 | + } |
| 196 | + current = current.next; |
188 | 197 | }
|
189 |
| - return '[' + join.toString() + ']'; |
| 198 | + sb.append(']'); |
| 199 | + return sb.toString(); |
190 | 200 | }
|
191 | 201 |
|
192 |
| - /* Driver Code */ |
193 | 202 | public static void main(String[] args) {
|
194 | 203 | LinkedQueue<Integer> queue = new LinkedQueue<>();
|
195 | 204 | assert queue.isEmpty();
|
|
0 commit comments