当前位置:网站首页>队列(堆栈)
队列(堆栈)
2022-07-21 17:49:00 【海口无鱼】
java.util.Queue 队列接口
- Queue继承自Collection,所以队列本身也是一种集合
- 队列可以保存一组元素,但是特点是存取元素必须遵循先进先出原则
- 常用实现类:java.util.LinkedList
offer方法:入队操作,元素会被追加到队列末尾
poll方法:出队操作,获取队首元素后该元素从队列中被删除
peek方法:引用队首元素,获取后该元素仍然在队列中
Queue<String> queue = new LinkedList<>();
queue.offer("one");
queue.offer("two");
queue.offer("three");
System.out.println(queue); //[one, two, three]
String str = queue.poll();
System.out.println(str); //one
System.out.println(queue); // [two, three]
str =queue.peek();
System.out.println(str); //two
System.out.println(queue); // [two, three]
栈结构
- 栈可以保存一组元素,但是存取元素必须遵循先进后出通常使用栈队列结构完成前进"后退"这样的功能使用
- Deque双端队列如果从同一侧做入队操作就实现了栈结构,因此Deque也称为栈提供它出入栈的经典方法:push,pop
push:入栈操作元素会被追加到栈首
pop:出栈操作,获取栈首元素后该元素从栈中被删除
Deque<String> stack = new LinkedList<>();
stack.push("one");
stack.push("two");
stack.push("three");
stack.push("four");
System.out.println(stack);
String str = stack.pop();
System.out.println(str); //[four, three, two, one]
System.out.println(stack); //[three, two, one]
双端队列接口:java.util.Deque
- Deque继承自Queue.特点是队列两端都可以做入队操作
- 常用的实现类:java.util.LinkedList
Deque<String>deque = new LinkedList<>();
deque.offer("one");
deque.offer("two");
deque.offer("three");
deque.offer("four");
System.out.println(deque);
deque.offerFirst("five"); //从队首方向入队
deque.offerLast("six"); //从队尾方向入队,与offer一致
String str = deque.poll(); //从队首方向出队
str = deque.pollFirst(); //从队首方向出队
str = deque.pollLast(); //从队尾方向出队
边栏推荐
- 单细胞论文记录(part18)--Spectral clustering based on learning similarity matrix
- 线程池七大参数
- 快乐数~~~(其实我一点都不快乐) && 丑数
- Shortcut keys for command line editing and operation skills commands
- was安装器启动报错
- 线性代数学习笔记——第二十五讲——向量在轴上的投影
- ettercap详细使用教程
- 前辈的前后台分离介绍
- MySQL多表查询
- Ctrip spark multi tenant query service evolution, Apache Kyuubi can be expected in the future
猜你喜欢
解决按钮UIButton重复点击问题
好轮子收藏:一个支持几乎所有流行格式的图像加载库stb_image.h
携程 Spark 多租户查询服务演进,Apache Kyuubi 未来可期
LeetCode342:4的幂
QT configuration opencv (II): successful
Introduction to class loader
Implementation of recommendation system collaborative filtering in spark
Server memory failure prediction can actually do this!
LeetCode703:数据流中的第 K 大元素
包装类(类型之间的相互转化)
随机推荐
List 转字符串后去掉前后 [ ]
[dry goods] obstacles and solutions to knowledge sharing
Login shell and non login shell
【干货】知识共享的障碍及解决方法
Leetcode703: the k-th element in data flow
Gradle 15分钟入门教程
拼写单词~
String 类
携程 Spark 多租户查询服务演进,Apache Kyuubi 未来可期
dtcloud 使用自定义字体
leetCode-468: 验证IP地址
创建新节点的点击事件
js执行机制
Go zero micro service practical series (III. API definition and table structure design)
ettercap详细使用教程
编辑器公式
Leetcode342:4 power
面试题 01.02. 判定是否互为字符重排
solo 文章 推送到社区端失败
MMDrawerController 获取当前VC进行Push和Pop