Skip to content

Commit d0cb9b9

Browse files
author
Alex Klymenko
committed
LinkedQueue
1 parent f5c0314 commit d0cb9b9

File tree

2 files changed

+281
-93
lines changed

2 files changed

+281
-93
lines changed

src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java

Lines changed: 88 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,194 +2,203 @@
22

33
import java.util.Iterator;
44
import java.util.NoSuchElementException;
5-
import java.util.StringJoiner;
65

76
public class LinkedQueue<T> implements Iterable<T> {
87

9-
static class Node<T> {
10-
8+
/**
9+
* Node class representing each element in the queue.
10+
*/
11+
private static class Node<T> {
1112
T data;
1213
Node<T> next;
1314

14-
Node() {
15-
this(null);
16-
}
17-
1815
Node(T data) {
19-
this(data, null);
20-
}
21-
22-
Node(T data, Node<T> next) {
2316
this.data = data;
24-
this.next = next;
17+
this.next = null;
2518
}
2619
}
2720

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
3724

3825
/**
39-
* Size of Queue
40-
*/
41-
private int size;
42-
43-
/**
44-
* Init LinkedQueue
26+
* Initializes an empty LinkedQueue.
4527
*/
4628
public LinkedQueue() {
47-
48-
front = new Node<>();
49-
rear = front;
29+
front = null;
30+
rear = null;
31+
size = 0;
5032
}
5133

5234
/**
53-
* Check if queue is empty
35+
* Checks if the queue is empty.
5436
*
55-
* @return true if queue is empty, otherwise false
37+
* @return true if the queue is empty, otherwise false.
5638
*/
5739
public boolean isEmpty() {
5840
return size == 0;
5941
}
6042

6143
/**
62-
* Add element to rear of queue
44+
* Adds an element to the rear of the queue.
6345
*
64-
* @param data insert value
46+
* @param data the element to insert.
47+
* @throws IllegalArgumentException if data is null.
6548
*/
6649
public void enqueue(T data) {
50+
if (data == null) {
51+
throw new IllegalArgumentException("Cannot enqueue null data");
52+
}
53+
6754
Node<T> newNode = new Node<>(data);
6855

69-
rear.next = newNode;
56+
if (isEmpty()) {
57+
front = newNode;
58+
} else {
59+
rear.next = newNode;
60+
}
7061
rear = newNode;
71-
/* make rear point at last node */
7262
size++;
7363
}
7464

7565
/**
76-
* Remove element at the front of queue
66+
* Removes and returns the element at the front of the queue.
7767
*
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.
7970
*/
8071
public T dequeue() {
8172
if (isEmpty()) {
82-
throw new NoSuchElementException("queue is empty");
73+
throw new NoSuchElementException("Queue is empty");
8374
}
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;
8878
size--;
8979

9080
if (isEmpty()) {
91-
front = rear;
81+
rear = null;
9282
}
9383

9484
return retValue;
9585
}
9686

9787
/**
98-
* Peek element at the front of queue without removing
88+
* Returns the element at the front of the queue without removing it.
9989
*
100-
* @return element at the front
90+
* @return the element at the front of the queue.
91+
* @throws NoSuchElementException if the queue is empty.
10192
*/
10293
public T peekFront() {
10394
if (isEmpty()) {
104-
throw new NoSuchElementException("queue is empty");
95+
throw new NoSuchElementException("Queue is empty");
10596
}
106-
return front.next.data;
97+
return front.data;
10798
}
10899

109100
/**
110-
* Peek element at the rear of queue without removing
101+
* Returns the element at the rear of the queue without removing it.
111102
*
112-
* @return element at the front
103+
* @return the element at the rear of the queue.
104+
* @throws NoSuchElementException if the queue is empty.
113105
*/
114106
public T peekRear() {
115107
if (isEmpty()) {
116-
throw new NoSuchElementException("queue is empty");
108+
throw new NoSuchElementException("Queue is empty");
117109
}
118110
return rear.data;
119111
}
120112

121113
/**
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.
125119
*/
126-
127120
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!");
130123
}
124+
131125
Node<T> node = front;
132-
while (pos-- > 0) {
126+
for (int i = 1; i < pos; i++) {
133127
node = node.next;
134128
}
135129
return node.data;
136130
}
137131

138132
/**
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.
141136
*/
142-
143137
@Override
144138
public Iterator<T> iterator() {
145139
return new Iterator<>() {
146-
Node<T> node = front;
140+
private Node<T> current = front;
147141

148142
@Override
149143
public boolean hasNext() {
150-
return node.next != null;
144+
return current != null;
151145
}
152146

153147
@Override
154148
public T next() {
155-
if (hasNext()) {
156-
node = node.next;
157-
return node.data;
149+
if (!hasNext()) {
150+
throw new NoSuchElementException();
158151
}
159-
throw new NoSuchElementException();
152+
153+
T data = current.data;
154+
current = current.next;
155+
return data;
160156
}
161157
};
162158
}
163159

164160
/**
165-
* Return size of queue
161+
* Returns the size of the queue.
166162
*
167-
* @return size of queue
163+
* @return the size of the queue.
168164
*/
169165
public int size() {
170166
return size;
171167
}
172168

173169
/**
174-
* Clear all nodes in queue
170+
* Clears all elements from the queue.
175171
*/
176172
public void clear() {
177-
while (size > 0) {
178-
dequeue();
179-
}
173+
front = null;
174+
rear = null;
175+
size = 0;
180176
}
181177

178+
/**
179+
* Returns a string representation of the queue.
180+
*
181+
* @return a string representation of the queue.
182+
*/
182183
@Override
183184
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;
188197
}
189-
return '[' + join.toString() + ']';
198+
sb.append(']');
199+
return sb.toString();
190200
}
191201

192-
/* Driver Code */
193202
public static void main(String[] args) {
194203
LinkedQueue<Integer> queue = new LinkedQueue<>();
195204
assert queue.isEmpty();

0 commit comments

Comments
 (0)