-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.java
63 lines (54 loc) · 1.19 KB
/
Queue.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
package collections;
import java.util.Iterator;
/**
* Queue represents the FIFO (first in first out) data sructure.
*/
public class Queue<T> implements Iterable<T>, CountableCollection {
private LinkedList<T> _list;
/**
* Creates new instance of queue
*/
public Queue() {
_list = new LinkedList<>();
}
/**
* Endqueues item to queue list.
* @param data to endqueue.
* @implNote Time complexity: O(1)
*/
public void endqueue(T data) {
_list.addBack(data);
}
/**
* Dequeues item from queue list.
* @return dequeued item.
* @implNote Time complexity: O(1)
*/
public T dequeue() {
return _list.removeFront();
}
/**
* Gets item from the queue where stayed at front.
*/
public T peek() {
return _list.peekFront();
}
/**
* Checks whether queue is empty or not.
*/
@Override
public boolean isEmpty() {
return _list.isEmpty();
}
/**
* Gets the size of queue.
*/
@Override
public int size() {
return _list.size();
}
@Override
public Iterator<T> iterator() {
return _list.iterator();
}
}