-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueArray.java
47 lines (44 loc) · 1.11 KB
/
QueueArray.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
public class QueueArray<T>{
private int N, begin = -1, end = -1;
private T[] data = (T[]) new Object[2];
public QueueArray(){}
public QueueArray(QueueArray<T> that){
this.data = (T[]) new Object[that.data.length];
this.N = that.N;
for(int i = 0; i < N; ++i){
this.data[i] = that.data[i];
}
}
public void enqueue(T x){
if(N == data.length) doubleSize();
data[(++end) % data.length] = x;
N++;
}
public T dequeue(){
if(N == 0) throw new IllegalStateException();
T retVal;
if(N == data.length / 4) halfSize();
retVal = data[(++begin) % data.length];
N--;
return retVal;
}
private void doubleSize(){
T[] newData = (T[]) new Object[data.length * 2];
for(int i = (begin + 1) % data.length, j = 0; j < N; i = (i + 1) % data.length, j++){
newData[j] = data[i];
}
begin = -1; end = N-1;
data = newData;
}
private void halfSize(){
T[] newData = (T[]) new Object[data.length / 2];
for(int i = (begin + 1) % data.length, j = 0; j < N; i = (i + 1) % data.length, j++){
newData[j] = data[i];
}
begin = -1; end = N-1;
data = newData;
}
public int getSize(){
return N;
}
}