Skip to content

Commit 3e9dd77

Browse files
authored
Make LinkedQueue generic (#3909)
1 parent dd949e9 commit 3e9dd77

File tree

2 files changed

+95
-35
lines changed

2 files changed

+95
-35
lines changed

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

+66-35
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
package com.thealgorithms.datastructures.queues;
22

3+
import java.util.Iterator;
34
import java.util.NoSuchElementException;
5+
import java.util.StringJoiner;
46

5-
public class LinkedQueue {
7+
public class LinkedQueue<T> implements Iterable<T> {
68

7-
class Node {
9+
static class Node<T> {
810

9-
int data;
10-
Node next;
11+
T data;
12+
Node<T> next;
1113

1214
public Node() {
13-
this(0);
15+
this(null);
1416
}
1517

16-
public Node(int data) {
18+
public Node(T data) {
1719
this(data, null);
1820
}
1921

20-
public Node(int data, Node next) {
22+
public Node(T data, Node<T> next) {
2123
this.data = data;
2224
this.next = next;
2325
}
@@ -26,12 +28,12 @@ public Node(int data, Node next) {
2628
/**
2729
* Front of Queue
2830
*/
29-
private Node front;
31+
private Node<T> front;
3032

3133
/**
3234
* Rear of Queue
3335
*/
34-
private Node rear;
36+
private Node<T> rear;
3537

3638
/**
3739
* Size of Queue
@@ -42,7 +44,7 @@ public Node(int data, Node next) {
4244
* Init LinkedQueue
4345
*/
4446
public LinkedQueue() {
45-
front = rear = new Node();
47+
front = rear = new Node<>();
4648
}
4749

4850
/**
@@ -58,30 +60,28 @@ public boolean isEmpty() {
5860
* Add element to rear of queue
5961
*
6062
* @param data insert value
61-
* @return true if add successfully
6263
*/
63-
public boolean enqueue(int data) {
64-
Node newNode = new Node(data);
64+
public void enqueue(T data) {
65+
Node<T> newNode = new Node<>(data);
66+
6567
rear.next = newNode;
6668
rear = newNode;
6769
/* make rear point at last node */
6870
size++;
69-
return true;
7071
}
7172

7273
/**
7374
* Remove element at the front of queue
7475
*
7576
* @return element at the front of queue
7677
*/
77-
public int dequeue() {
78+
public T dequeue() {
7879
if (isEmpty()) {
7980
throw new NoSuchElementException("queue is empty");
8081
}
81-
Node destroy = front.next;
82-
int retValue = destroy.data;
82+
Node<T> destroy = front.next;
83+
T retValue = destroy.data;
8384
front.next = front.next.next;
84-
destroy = null;
8585
/* clear let GC do it's work */
8686
size--;
8787

@@ -97,7 +97,7 @@ public int dequeue() {
9797
*
9898
* @return element at the front
9999
*/
100-
public int peekFront() {
100+
public T peekFront() {
101101
if (isEmpty()) {
102102
throw new NoSuchElementException("queue is empty");
103103
}
@@ -109,13 +109,52 @@ public int peekFront() {
109109
*
110110
* @return element at the front
111111
*/
112-
public int peekRear() {
112+
public T peekRear() {
113113
if (isEmpty()) {
114114
throw new NoSuchElementException("queue is empty");
115115
}
116116
return rear.data;
117117
}
118118

119+
/**
120+
* Peeks the element at the index and
121+
* returns the value
122+
* @param pos at which to peek
123+
*/
124+
125+
public T peek(int pos) {
126+
if (pos > size)
127+
throw new IndexOutOfBoundsException(
128+
"Position %s out of range!".formatted(pos));
129+
Node<T> node = front;
130+
while (pos-- > 0)
131+
node = node.next;
132+
return node.data;
133+
}
134+
135+
/**
136+
* Node iterator, allows to travel through
137+
* the nodes using for() loop or forEach(Consumer)
138+
*/
139+
140+
@Override
141+
public Iterator<T> iterator() {
142+
return new Iterator<>() {
143+
144+
Node<T> node = front;
145+
146+
@Override
147+
public boolean hasNext() {
148+
return node.next != null;
149+
}
150+
151+
@Override
152+
public T next() {
153+
return (node = node.next).data;
154+
}
155+
};
156+
}
157+
119158
/**
120159
* Return size of queue
121160
*
@@ -129,30 +168,22 @@ public int size() {
129168
* Clear all nodes in queue
130169
*/
131170
public void clear() {
132-
while (!isEmpty()) {
171+
while (size > 0)
133172
dequeue();
134-
}
135173
}
136174

137175
@Override
138176
public String toString() {
139-
if (isEmpty()) {
140-
return "[]";
141-
}
142-
StringBuilder builder = new StringBuilder();
143-
Node cur = front.next;
144-
builder.append("[");
145-
while (cur != null) {
146-
builder.append(cur.data).append(", ");
147-
cur = cur.next;
148-
}
149-
builder.replace(builder.length() - 2, builder.length(), "]");
150-
return builder.toString();
177+
StringJoiner join = new StringJoiner(", "); // separator of ', '
178+
Node<T> travel = front;
179+
while ((travel = travel.next) != null)
180+
join.add(String.valueOf(travel.data));
181+
return '[' + join.toString() + ']';
151182
}
152183

153184
/* Driver Code */
154185
public static void main(String[] args) {
155-
LinkedQueue queue = new LinkedQueue();
186+
LinkedQueue<Integer> queue = new LinkedQueue<>();
156187
assert queue.isEmpty();
157188

158189
queue.enqueue(1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.thealgorithms.datastructures.queues;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
8+
class LinkedQueueTest {
9+
@Test
10+
public void testQue() {
11+
LinkedQueue<Integer> queue = new LinkedQueue<>();
12+
for (int i = 1; i < 5; i++)
13+
queue.enqueue(i);
14+
15+
assertEquals(queue.peekRear(), 4);
16+
assertEquals(queue.peek(2), 2);
17+
18+
assertEquals(queue.peek(4), 4);
19+
20+
final int[] element = { 1 };
21+
22+
// iterates over all the elements present
23+
// as in the form of nodes
24+
queue.forEach(integer -> {
25+
if (element[0]++ != integer)
26+
throw new AssertionError();
27+
});
28+
}
29+
}

0 commit comments

Comments
 (0)