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