-
Notifications
You must be signed in to change notification settings - Fork 8
/
QueueBasedOnArray.java
74 lines (61 loc) · 1.65 KB
/
QueueBasedOnArray.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
package struct.queue;
/**
* 基于数组实现的队列
*/
public class QueueBasedOnArray<T> {
// items 表示数组,capacity 表示数组大小
private T[] items;
private int capacity;
// head表示队头下标,tail表示队尾下标
private int head;
private int tail;
public QueueBasedOnArray(int capacity) {
this.capacity = capacity;
this.items = (T[]) new Object[this.capacity];
}
public boolean enqueue(T data) {
// 队列尾部没有空间了
if (tail == capacity) {
if (head == 0) {
// 整个队列都占满了
return false;
}
// 数据搬移
for (int i = head; i < tail; i++) {
items[i - head] = items[i];
}
// 搬移后更新 head 和 tail
tail = tail - head;
head = 0;
}
items[tail] = data;
tail++;
return true;
}
public T dequeue() {
if (head == tail) {
return null;
}
T value = items[head];
head++;
return value;
}
public void printAll() {
for (int i = 0; i < tail; i++) {
System.out.print(items[i] + ",");
}
System.out.println();
}
public static void main(String[] args) {
QueueBasedOnArray<Integer> queue = new QueueBasedOnArray<>(4);
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);
queue.printAll();
System.out.println(queue.dequeue());
queue.enqueue(5);
queue.printAll();
}
}