-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayQueue.java
168 lines (148 loc) · 3.44 KB
/
ArrayQueue.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package one.wangwei.algorithms.datastructures.queue.impl;
import one.wangwei.algorithms.datastructures.queue.IQueue;
import java.util.NoSuchElementException;
/**
* 数组队列
*
* @param <T>
* @author https://wangwei.one
* @date 2019/02/04
*/
public class ArrayQueue<T> implements IQueue<T> {
/**
* default array size
*/
private static final int DEFAULT_SIZE = 1024;
/**
* 元素数组
*/
private T[] array;
/**
* 队头指针下标
*/
private int front = 0;
/**
* 队尾指针下标
*/
private int rear = 0;
public ArrayQueue() {
this(DEFAULT_SIZE);
}
public ArrayQueue(int capacity) {
array = (T[]) new Object[capacity];
}
/**
* 添加队尾元素
*
* @param value
* @return
*/
@Override
public boolean offer(T value) {
if (isFull()) {
grow();
}
array[rear % array.length] = value;
rear++;
return true;
}
/**
* grow queue size doubly
*/
private void grow() {
int growSize = array.length << 1;
T[] tmpArray = (T[]) new Object[growSize];
int adjRear = rear % array.length;
int endIndex = rear > array.length ? array.length : rear;
if (adjRear < front) {
System.arraycopy(array, 0, tmpArray, array.length - adjRear, adjRear + 1);
}
System.arraycopy(array, front, tmpArray, 0, endIndex - front);
array = tmpArray;
rear = (rear - front);
front = 0;
}
/**
* 移除队头元素
*
* @return
*/
@Override
public T poll() {
if (isEmpty()) {
throw new NoSuchElementException("Queue underflow");
}
T element = array[front % array.length];
array[front % array.length] = null;
front++;
if (isEmpty()) {
// remove last element
front = rear = 0;
}
int shrinkSize = array.length >> 1;
if (shrinkSize >= DEFAULT_SIZE && size() < shrinkSize) {
shrink();
}
return element;
}
/**
* 压缩
*/
private void shrink() {
int shrinkSize = array.length >> 1;
T[] tmpArray = (T[]) new Object[shrinkSize];
int adjRear = rear % array.length;
int endIndex = rear > array.length ? array.length : rear;
if (adjRear <= front) {
System.arraycopy(array, 0, tmpArray, array.length - front, adjRear);
}
System.arraycopy(array, front, tmpArray, 0, endIndex - front);
array = null;
array = tmpArray;
rear = rear - front;
front = 0;
}
/**
* 查看队头元素
*
* @return
*/
@Override
public T peek() {
if (isEmpty()) {
throw new NoSuchElementException("Queue underflow");
}
return array[front % array.length];
}
/**
* 清除队列元素
*/
@Override
public void clear() {
array = null;
front = rear = 0;
}
/**
* 队列大小
*/
@Override
public int size() {
return rear - front;
}
/**
* 判断队列是否满
*
* @return
*/
private boolean isFull() {
return !isEmpty() && (front == (rear + 1) % array.length);
}
/**
* 判断队是否为空
*
* @return
*/
public boolean isEmpty() {
return size() <= 0;
}
}