forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueTest.java
71 lines (50 loc) · 1.21 KB
/
QueueTest.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
import java.util.List;
import java.util.ArrayList;
public class QueueTest {
public static void main(String[] args) {
IQueue<Integer> intQueue = new Queue<>();
intQueue.enqueue(4);
intQueue.enqueue(5);
intQueue.enqueue(9);
System.out.println(intQueue.dequeue());
System.out.println(intQueue.size());
System.out.println(intQueue.front());
}
}
interface IQueue<T> {
/*
* 'dequeue' removes the first element from the queue and returns it
*/
T dequeue();
/*
* 'enqueue' adds an element at the end of the queue and returns the new size
*/
int enqueue(T element);
/*
* 'size' returns the size of the queue
*/
int size();
/*
* 'front' returns the first element of the queue without removing it
*/
T front();
}
class Queue<T> implements IQueue<T> {
private List<T> list;
public Queue() {
this.list = new ArrayList<>();
}
public T dequeue() {
return this.list.remove(0);
}
public int enqueue(T element) {
this.list.add(element);
return this.size();
}
public int size() {
return this.list.size();
}
public T front() {
return this.list.get(0);
}
}