2023-01-06 03:39:19 +08:00
|
|
|
/**
|
2022-11-25 02:04:38 +08:00
|
|
|
* File: queue.java
|
|
|
|
* Created Time: 2022-11-25
|
2024-02-07 22:21:18 +08:00
|
|
|
* Author: krahets (krahets@163.com)
|
2022-11-25 02:04:38 +08:00
|
|
|
*/
|
|
|
|
|
2022-11-10 03:40:57 +08:00
|
|
|
package chapter_stack_and_queue;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class queue {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
/* 初始化队列 */
|
|
|
|
Queue<Integer> queue = new LinkedList<>();
|
|
|
|
|
|
|
|
/* 元素入队 */
|
|
|
|
queue.offer(1);
|
|
|
|
queue.offer(3);
|
|
|
|
queue.offer(2);
|
|
|
|
queue.offer(5);
|
|
|
|
queue.offer(4);
|
|
|
|
System.out.println("队列 queue = " + queue);
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
int peek = queue.peek();
|
|
|
|
System.out.println("队首元素 peek = " + peek);
|
|
|
|
|
|
|
|
/* 元素出队 */
|
2023-03-13 21:58:21 +08:00
|
|
|
int pop = queue.poll();
|
|
|
|
System.out.println("出队元素 pop = " + pop + ",出队后 queue = " + queue);
|
2022-11-10 03:40:57 +08:00
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
int size = queue.size();
|
|
|
|
System.out.println("队列长度 size = " + size);
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
boolean isEmpty = queue.isEmpty();
|
2022-11-30 02:27:26 +08:00
|
|
|
System.out.println("队列是否为空 = " + isEmpty);
|
2022-11-10 03:40:57 +08:00
|
|
|
}
|
|
|
|
}
|