-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleQueue.java
61 lines (53 loc) · 1.17 KB
/
SimpleQueue.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
import java.util.LinkedList;
/**
* Simple Queue (FIFO) based on LinkedList.
*/
public class SimpleQueue<E> {
private LinkedList<E> list = new LinkedList<E>();
/**
* Puts object in queue.
*/
public void put(E o) {
list.addLast(o);
}
/**
* Returns an element (object) from queue.
*
* @return element from queue or <code>null</code> if queue is empty
*/
public E get() {
if (list.isEmpty()) {
return null;
}
return list.removeFirst();
}
/**
* Returns all elements from the queue and clears it.
*/
public Object[] getAll() {
Object[] res = new Object[list.size()];
for (int i = 0; i < res.length; i++) {
res[i] = list.get(i);
}
list.clear();
return res;
}
/**
* Peeks an element in the queue. Returned elements is not removed from the queue.
*/
public E peek() {
return list.getFirst();
}
/**
* Returns <code>true</code> if queue is empty, otherwise <code>false</code>
*/
public boolean isEmpty() {
return list.isEmpty();
}
/**
* Returns queue size.
*/
public int size() {
return list.size();
}
}