1. 概述
- 隊列:是“先進先出”的數據結構,從隊尾入隊,從隊頭出隊。
- 隊列中使用的Array,參考“數據結構–手寫動態數組”。
2. 數組隊列源碼
public interface Queue<T> {
int getSize();
boolean isEmpty();
void enqueue(T element);
T dequeue();
T getFront();
}
public class ArrayQueue<T> implements Queue<T> {
private Array<T> array;
public ArrayQueue() {
array = new Array<>();
}
public ArrayQueue(int capacity) {
array = new Array<>(capacity);
}
@Override
public int getSize() {
return array.getSize();
}
@Override
public boolean isEmpty() {
return array.isEmpty();
}
public int getCapacity() {
return array.getCapacity();
}
@Override
public void enqueue(T element) {
array.addLast(element);
}
@Override
public T dequeue() {
return array.removeFirst();
}
@Override
public T getFront() {
return array.getFirst();
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append(String.format("Queue's size: %d, capacity: %d\n", getSize(), getCapacity()));
result.append("Head[");
int size = getSize();
for (int i = 0; i < size; i++) {
result.append(array.get(i));
if (i != size -1) {
result.append(", ");
}
}
result.append("]Tail");
return result.toString();
}
}
3. 循環隊列分析及源碼
-
概述
- 目的:使用數組隊列,對於出隊操作,是O(n)的時間複雜度,當數據量過大的時候,性能較低。
- 基礎知識:
- 出隊時,不再移動元素;而是,使用front和tail來標識,隊列的頭部和尾部所在位置。
- front == tail時,代表隊列爲空。
- (tail + 1) % C == front時,代表隊列滿。 C 代表總Capacity。
-
源碼
/**
* 規則:
* 1. tail == front, 隊列爲空.
* 2. (tail + 1) % c == front, 隊列爲滿.
*
* @param <T>
*/
public class LoopQueue<T> implements Queue<T> {
private T[] data;
private int front, tail;
private int size;
private static final int DEFAULT_CAPACITY = 10;
public LoopQueue() {
this(DEFAULT_CAPACITY);
}
public LoopQueue(int capacity) {
// capacity + 1: 因爲tail所在的位置不能存儲數據, 需要浪費一個空間.
data = (T[]) new Object[capacity + 1];
front = 0;
tail = 0;
size = 0;
}
public int getCapacity() {
// 因爲tail所在的位置不能存儲數據, 需要浪費一個空間.
return data.length - 1;
}
@Override
public int getSize() {
return size;
}
@Override
public boolean isEmpty() {
return front == tail;
}
@Override
public void enqueue(T element) {
if ((tail + 1) % data.length == front) { // 數組已滿, 進行擴容.
resize(2 * getCapacity());
}
data[tail] = element;
tail = (tail + 1) % data.length;
size++;
}
@Override
public T dequeue() {
if (isEmpty()) {
throw new IllegalArgumentException("LoopQueue is empty!");
}
T oldElement = data[front];
data[front] = null;
front = (front + 1) % data.length;
size--;
if (size == getCapacity() / 4 && getCapacity() / 2 != 0) { // 數組元素較少, 進行縮容.
resize(getCapacity() / 2);
}
return oldElement;
}
@Override
public T getFront() {
if (isEmpty()) {
throw new IllegalArgumentException("LoopQueue is empty!");
}
return data[front];
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append(String.format("LoopQueue's size: %d, capacity: %d\n", getSize(), getCapacity()));
result.append("Front[");
for (int i = front; i != tail; i = (i + 1) % data.length) {
result.append(data[i]);
if ((i + 1) % data.length != tail) {
result.append(", ");
}
}
result.append("]Tail");
return result.toString();
}
private void resize(int newCapacity) {
T[] newData = (T[]) new Object[newCapacity + 1];
for (int i = 0; i < size; i++) {
// 因爲data是循環數組, 保證data下標不會越界
newData[i] = data[((i + front) % data.length)];
}
data = newData;
front = 0;
tail = size;
}
}