-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsolution.java
More file actions
88 lines (76 loc) · 1.89 KB
/
solution.java
File metadata and controls
88 lines (76 loc) · 1.89 KB
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
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.ArrayList;
import static java.util.Collections.*;
public class solution {
public static class Queue<T> {
private ArrayList<T> data = new ArrayList<T>();
private int size = 0;
private int begin = 0;
public boolean empty() {
return size == 0;
}
public void pushBack(T item) {
if (size + 1 > data.size()) {
if (begin == 0) {
data.addAll(nCopies(2 * (data.size() + 1), (T)null));
} else {
ArrayList<T> newData = new ArrayList<T>(2 * (data.size() + 1));
int p1begin = begin;
int p1len = size - begin;
int p2begin = 0;
int p2len = p1begin;
newData.addAll(data.subList(p1begin, p1begin + p1len));
newData.addAll(data.subList(p2begin, p2begin + p2len));
newData.addAll(nCopies(data.size() + 1, (T)null));
data = newData;
}
begin = 0;
}
int index = (begin + size) % data.size();
data.add(index, item);
size++;
}
public T popFront() {
T result = data.get(begin);
begin = (begin + 1) % data.size();
size--;
return result;
}
}
public static class UnitTest {
@Test
public void test() {
Queue<Integer> q = new Queue<Integer>();
assertEquals(q.empty(), true);
q.pushBack(1);
q.pushBack(2);
q.pushBack(3);
assertEquals(q.empty(), false);
assertEquals((int)q.popFront(), 1);
assertEquals((int)q.popFront(), 2);
assertEquals((int)q.popFront(), 3);
assertEquals(q.empty(), true);
q.pushBack(4);
assertEquals(q.empty(), false);
assertEquals((int)q.popFront(), 4);
assertEquals(q.empty(), true);
int in = 0;
int out = 0;
for (int i = 1; i < 100; i++) {
for (int j = 0; j < i; j++) {
q.pushBack(in);
in++;
}
int x = q.popFront();
assertEquals(x, out);
out++;
}
while (!q.empty()) {
int x = q.popFront();
assertEquals(x, out);
out++;
}
}
}
}