02-队列
02-队列
标准库中的队列
Java的标准库中,队列接口Queue定义了以下几个方法:
int size() : 获取队列长度
boolean offer(E) : 添加元素到队尾,返回false
boolean add(E) : throw Exception
E poll() : 获取队首元素并从队列中删除,返回null
E remove() : throw Exception
E peek() : 获取队首元素并不从队列中删除,返回null
E element() : throw Exception
queue.contains() : 通过遍历队列来查找指定元素,返回true/false
一些实现类型:
- LinkedList : 不限制容量,双向链表结构
- ArrayBlockingQueue : 具有固定的容量,由数组支持
- LinkedBlockingQueue:可以指定最大容量,也可以默认;基于链表实现
- PriorityQueue : 不限制容量;基于优先级堆实现;最小或最大元素始终位于队列的头部
双向队列Deque:扩展自Queue
- offerFirst(),offerLast()等
- 实现类有: LinkedList,ArrayDeque
算法题中的队列:
1
2
3
4
5
6
7
8
9
//单调队列
Deque<Integer> deque = new LinkedList<>();
deque.peekLast();
deque.pollLast();
deque.offerLast(i);
deque.peekFirst();
deque.pollFirst();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//优先级队列
//小顶堆(默认)
PriorityQueue queue = new PriorityQueue<Integer>(
new Comparator<Integer>(){
@Override
public int compare(Integer o1,Integer o2){
return o1 - o2;
}
}
);
//大顶堆
PriorityQueue queue = new PriorityQueue<Integer>(
new Comparator<Integer>(){
@Override
public int compare(Integer o1,Integer o2){
return o2 - o1;
}
}
);
数组模拟队列
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
package queue;
import java.util.Scanner;
public class ArrayQueueDemo {
public static void main(String[] args) {
ArrayQueue arrayQueue = new ArrayQueue(3);
char key = ' ';
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while(loop) {
System.out.println("s(show):显示队列");
System.out.println("e(exit):退出程序");
System.out.println("a(add):添加数据到队列");
System.out.println("g(get):从队列取出数据");
System.out.println("h(head):查看队列头的数据");
key = scanner.next().charAt(0); //接受一个字符
switch(key) {
case 's':
arrayQueue.showQueue();
break;
case 'a':
System.out.println("请输入一个数字");
int value = scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try {
int res = arrayQueue.getQueue();
System.out.printf("取出的数据是 %d\n",res);
}catch(Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int head = arrayQueue.headQueue();
System.out.printf("队列头数据为%d\n", head);
}catch(Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
}
}
}
}
class ArrayQueue{
private int maxSize;
private int front;
private int rear;
private int[] arr;
public ArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
front = -1; //指向队列头的前一个位置
rear = -1; //指向队列尾部
}
//判满
public boolean isFull() {
return rear == maxSize - 1;
}
//判空
public boolean isEmpty() {
return front == rear;
}
//添加数据到队列
public void addQueue(int n) {
if(isFull()) {
System.out.println("队列满,无法加入数据");
return;
}
rear++;
arr[rear] = n;
}
//出队列
public int getQueue() {
if(isEmpty()) {
throw new RuntimeException("队列空,无法取数据");
}
front++;
return arr[front];
}
//显示队列所有数据
public void showQueue() {
if(isEmpty()) {
System.out.println("队列空,没有数据");
return;
}
for(int i = 0;i<arr.length;i++) {
System.out.printf("arr[%d]=%d\n",i,arr[i]);
}
}
//显示队列头数据
public int headQueue() {
if(isEmpty()) {
throw new RuntimeException("队列空,无头数据");
}
return arr[front+1];
}
}
This post is licensed under CC BY 4.0 by the author.