-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImmQueue.java
More file actions
40 lines (32 loc) · 841 Bytes
/
ImmQueue.java
File metadata and controls
40 lines (32 loc) · 841 Bytes
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
package Stack_Queue;
import java.util.ArrayList;
import java.util.List;
public class ImmQueue<T> {
private final List<T> list;
public ImmQueue(){
list = new ArrayList<>();
}
public ImmQueue(List<T> list){
this.list = list;
}
public int size(){
return list.size();
}
public ImmQueue<T> enqueue(T item){
List<T> list = new ArrayList<>(this.list);
list.add(item);
return new ImmQueue<>(list);
}
public ImmQueue<T> dequeue(){
if (size() > 0){
List<T> list = new ArrayList<>(this.list);
list.remove(0);
return new ImmQueue<>(list);
}else {
return null;
}
}
public T peek(){
return size() > 0 ? list.get(0) : null;
}
}