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 | package io.github.jamiesona.datawhale.task04;
public interface Queue<E> {
/**
* 插入元素到队列尾部.
*
* @param val 插入的值
* @return 是否成功插入元素
*/
boolean enqueue(E val);
/**
* 删除并返回对列头部的值.
*
* @return 队列头部的值, 队列为空时返回null
*/
E dequeue();
/**
* 返回队列头部的值, 不删除.
*
* @return 队列头部的值, 队列为空时返回null
*/
E peek();
/**
* 判断队列为空
*
* @return true if empty, else false
*/
boolean isEmpty();
} |
基于数组实现的固定大小队列
和基于数组的栈的实现类似,基于数组的队列需要记录队头和队尾在数组中的索引。随着队列push和pop操作的进行,最终保存数据的部分会逐渐集中到数组后末尾部分,最后使得队尾的索引指向了数组的最后一个元素,此时就不能在向队列中插入新的元素了,但实际上在数组的前半部分可能仍然有可用的存储空间。为了解决这个问题,可以采用两种方案:
使用元素拷贝解决伪溢出
解决的思路是当队尾的索引指向数组最后一个元素时,整体将保存队列数据的部分移动到数组的起始位置,具体代码如下:
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 | package io.github.jamiesona.datawhale.task04;
public class ArrayQueue<E> extends AbstractFixedQueue<E> {
private Object[] arr;
private int head;
private int tail;
public ArrayQueue(int size) {
arr = new Object[size];
head = tail = -1;
}
@Override
public synchronized boolean enqueue(E val) {
if (this.isFull()) {
return false;
} else {
copy();
arr[++tail] = val;
return true;
}
}
@Override
public synchronized E dequeue() {
if (this.isEmpty()) {
return null;
} else {
return (E) arr[++head];
}
}
@Override
public synchronized E peek() {
if (this.isEmpty()) {
return null;
} else {
return (E) arr[head + 1];
}
}
@Override
public synchronized boolean isEmpty() {
return head == tail;
}
@Override
public synchronized boolean isFull() {
return tail - head == arr.length;
}
/**
* 队列元素拷贝.
*/
private void copy() {
if (tail == arr.length - 1) {
for (int i = 0, j = head + 1; j <= tail; i++, j++) {
arr[i] = arr[j];
}
tail = tail - head - 1;
head = -1;
}
}
public static void main(String[] args) {
AbstractFixedQueue<Integer> queue = new ArrayQueue<>(16);
for (int i = 1; i <= 16; i++) {
queue.enqueue(i);
}
System.out.println("在队列已满的情况下插入元素的结果: " + queue.enqueue(17));
System.out.println("出队一个元素: " + queue.dequeue());
System.out.println("新的队头元素: " + queue.peek());
System.out.println("出队一个元素之后再入队新元素的结果: " + queue.enqueue(17));
for (int i = 0; i < 16; i++) {
System.out.print("{出队: " + queue.dequeue() + "} ");
}
}
} |
main函数执行结果如下:
1
2
3
4
5 | 在队列已满的情况下插入元素的结果: false
出队一个元素: 1
新的队头元素: 2
出队一个元素之后再入队新元素的结果: true
{出队: 2} {出队: 3} {出队: 4} {出队: 5} {出队: 6} {出队: 7} {出队: 8} {出队: 9} {出队: 10} {出队: 11} {出队: 12} {出队: 13} {出队: 14} {出队: 15} {出队: 16} {出队: 17} |
使用循环队列解决伪溢出
在逻辑上将数组的首尾相连,当tail指向数组结尾元素时如果再向队列中插入元素,则该元素可以保存在数组的起始位置。具体实现如下:
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 | package io.github.jamiesona.datawhale.task04;
/**
* 循环队列.
*
* @param <E> 泛型参数
*/
public class CircularQueue<E> implements Queue<E> {
private Object[] arr;
private int head;
private int tail;
private int size;
private final int ARR_LEN;
public CircularQueue(int arrLen) {
ARR_LEN = arrLen;
arr = new Object[ARR_LEN];
head = tail = -1;
size = 0;
}
@Override
public boolean enqueue(E val) {
if (size == ARR_LEN) {//队列已满
return false;
} else {
tail = (tail + 1) % ARR_LEN;
arr[tail] = val;
size++;
return true;
}
}
@Override
public E dequeue() {
if (this.isEmpty()) {
return null;
} else {
size--;
head = (head + 1) % ARR_LEN;
return (E) arr[head];
}
}
@Override
public E peek() {
if (this.isEmpty()) {
return null;
} else {
int index = (head + 1) % ARR_LEN;
return (E) arr[index];
}
}
@Override
public boolean isEmpty() {
return size == 0;
}
public static void main(String[] args) {
Queue<Integer> queue = new CircularQueue<>(5);
for (int i = 1; i <= 5; i++) {
queue.enqueue(i);
}
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
queue.enqueue(6);
queue.enqueue(7);
//此时队列已满, 元素8入队失败
System.out.println(queue.enqueue(8));
while (!queue.isEmpty()) {
System.out.println(queue.dequeue());
}
//此时队列为空, 返回null
System.out.println(queue.dequeue());
}
} |
main函数执行结果: