You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
905 B
34 lines
905 B
2 years ago
|
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);
|
||
|
|
||
|
/* 元素出队 */
|
||
|
int poll = queue.poll();
|
||
|
System.out.println("出队元素 poll = " + poll + ",出队后 queue = " + queue);
|
||
|
|
||
|
/* 获取队列的长度 */
|
||
|
int size = queue.size();
|
||
|
System.out.println("队列长度 size = " + size);
|
||
|
|
||
|
/* 判断队列是否为空 */
|
||
|
boolean isEmpty = queue.isEmpty();
|
||
|
}
|
||
|
}
|