1
1
package com .thealgorithms .datastructures .queues ;
2
2
3
+ import java .util .Iterator ;
3
4
import java .util .NoSuchElementException ;
5
+ import java .util .StringJoiner ;
4
6
5
- public class LinkedQueue {
7
+ public class LinkedQueue < T > implements Iterable < T > {
6
8
7
- class Node {
9
+ static class Node < T > {
8
10
9
- int data ;
10
- Node next ;
11
+ T data ;
12
+ Node < T > next ;
11
13
12
14
public Node () {
13
- this (0 );
15
+ this (null );
14
16
}
15
17
16
- public Node (int data ) {
18
+ public Node (T data ) {
17
19
this (data , null );
18
20
}
19
21
20
- public Node (int data , Node next ) {
22
+ public Node (T data , Node < T > next ) {
21
23
this .data = data ;
22
24
this .next = next ;
23
25
}
@@ -26,12 +28,12 @@ public Node(int data, Node next) {
26
28
/**
27
29
* Front of Queue
28
30
*/
29
- private Node front ;
31
+ private Node < T > front ;
30
32
31
33
/**
32
34
* Rear of Queue
33
35
*/
34
- private Node rear ;
36
+ private Node < T > rear ;
35
37
36
38
/**
37
39
* Size of Queue
@@ -42,7 +44,7 @@ public Node(int data, Node next) {
42
44
* Init LinkedQueue
43
45
*/
44
46
public LinkedQueue () {
45
- front = rear = new Node ();
47
+ front = rear = new Node <> ();
46
48
}
47
49
48
50
/**
@@ -58,30 +60,28 @@ public boolean isEmpty() {
58
60
* Add element to rear of queue
59
61
*
60
62
* @param data insert value
61
- * @return true if add successfully
62
63
*/
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
+
65
67
rear .next = newNode ;
66
68
rear = newNode ;
67
69
/* make rear point at last node */
68
70
size ++;
69
- return true ;
70
71
}
71
72
72
73
/**
73
74
* Remove element at the front of queue
74
75
*
75
76
* @return element at the front of queue
76
77
*/
77
- public int dequeue () {
78
+ public T dequeue () {
78
79
if (isEmpty ()) {
79
80
throw new NoSuchElementException ("queue is empty" );
80
81
}
81
- Node destroy = front .next ;
82
- int retValue = destroy .data ;
82
+ Node < T > destroy = front .next ;
83
+ T retValue = destroy .data ;
83
84
front .next = front .next .next ;
84
- destroy = null ;
85
85
/* clear let GC do it's work */
86
86
size --;
87
87
@@ -97,7 +97,7 @@ public int dequeue() {
97
97
*
98
98
* @return element at the front
99
99
*/
100
- public int peekFront () {
100
+ public T peekFront () {
101
101
if (isEmpty ()) {
102
102
throw new NoSuchElementException ("queue is empty" );
103
103
}
@@ -109,13 +109,52 @@ public int peekFront() {
109
109
*
110
110
* @return element at the front
111
111
*/
112
- public int peekRear () {
112
+ public T peekRear () {
113
113
if (isEmpty ()) {
114
114
throw new NoSuchElementException ("queue is empty" );
115
115
}
116
116
return rear .data ;
117
117
}
118
118
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
+
119
158
/**
120
159
* Return size of queue
121
160
*
@@ -129,30 +168,22 @@ public int size() {
129
168
* Clear all nodes in queue
130
169
*/
131
170
public void clear () {
132
- while (! isEmpty ()) {
171
+ while (size > 0 )
133
172
dequeue ();
134
- }
135
173
}
136
174
137
175
@ Override
138
176
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 () + ']' ;
151
182
}
152
183
153
184
/* Driver Code */
154
185
public static void main (String [] args ) {
155
- LinkedQueue queue = new LinkedQueue ();
186
+ LinkedQueue < Integer > queue = new LinkedQueue <> ();
156
187
assert queue .isEmpty ();
157
188
158
189
queue .enqueue (1 );
0 commit comments