当前位置:网站首页>双队列实现栈?双栈实现队列?
双队列实现栈?双栈实现队列?
2022-08-01 23:58:00 【陈亦康】
双队列实现栈
单个队列是无法实现栈的,双队列是可以的,如下分析(例如出栈)
注意:
- 哪个队列不为空就放入哪个将元素放入哪个队列
- 若两个都为空,任选一个队列放入即可
如下代码:
class MyStack {
public Queue<Integer> q1;
public Queue<Integer> q2;
public int usedSize;
public MyStack() {
q1 = new LinkedList<>();
q2 = new LinkedList<>();
}
//压栈
public void push(int x) {
//哪个队列不为空,往那里里面放,都为空就放q1
if(!q1.isEmpty()){
q1.offer(x);
}
else if(!q2.isEmpty()){
q2.offer(x);
}
else{
q1.offer(x);
}
usedSize++;
}
//出栈
public int pop() {
//哪个队列不为空,就从哪里取元素放到另一个队列里
if(empty()){
return -1;
}
if(!q1.isEmpty()){
//转移usedSize - 1个元素到另一个队列
//for(int i = 0; i < usedSize - 1; i++)//不能这样写,usedSize会随着出栈不断减少
int size = usedSize;
for(int i = 0; i < size - 1; i++){
q2.offer(q1.poll());
}
usedSize--;
return q1.poll();
}
else{
int size = usedSize;
for(int i = 0; i < size - 1; i++){
q1.offer(q2.poll());
}
usedSize--;
return q2.poll();
}
}
public int top() {
//哪个队列不为空,就从哪里取元素放到另一个队列里
if(empty()){
return -1;
}
int tmp = 0;
if(!q1.isEmpty()){
int size = usedSize;
for(int i = 0; i < size - 1; i++){
q2.offer(q1.poll());
}
tmp = q1.peek();
q2.offer(q1.poll());
}
else{
int size = usedSize;
for(int i = 0; i < size - 1; i++){
q1.offer(q2.poll());
}
tmp = q2.peek();
q1.offer(q2.poll());
}
return tmp;
}
//判空
public boolean empty() {
return usedSize == 0;
}
}
双栈实现队列
单个栈是无法实现队列的,双栈是可以的,如下分析(例如出队)
注意:
- 出队之前一定要先检查s2中是否有元素,若有直接弹出即可,若没有再将s1全部压入s2中
- 检查队列首元素也是如此(peek)
代码如下:
class MyQueue {
public Stack<Integer> s1;//入栈
public Stack<Integer> s2;//出栈
public MyQueue() {
this.s1 = new Stack<>();
this.s2 = new Stack<>();
}
//进队
public void push(int x) {
s1.push(x);
}
//出队
public int pop() {
if(empty()) {
return -1;
}
//s2如同蓄水池,一旦要出队列,就全压s1
//前提一定要是s2为空!若不为空,再压入s2,顺序就不一样!
if(s2.empty()){
while(!s1.empty()) {
s2.push(s1.pop());
}
return s2.pop();
}
//如果s2里有元素,就直接弹出
return s2.pop();
}
public int peek() {
if(empty()) {
return -1;
}
//s2如同蓄水池,一旦要出队列,就全压s1
//前提一定要是s2为空!若不为空,再压入s2,顺序就不一样!
if(s2.empty()){
while(!s1.empty()) {
s2.push(s1.pop());
}
return s2.peek();
}
//如果s2里有元素,就直接弹出
return s2.peek();
}
public boolean empty() {
return s1.empty() && s2.empty();
}
}
边栏推荐
- Quartus uses tcl files to quickly configure pins
- numpy.isclose
- Appears in oozie on CDH's hue, error submitting Coordinator My Schedule
- Deliver cloud-native microservices applications with Zadig
- FAST-LIO2 code analysis (2)
- 面试必问的HashCode技术内幕
- 辛普森悖论
- 技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
- 类型“FC<Props>”的参数不能赋给类型“ForwardRefRenderFunction<unknown, Props>”的参数。 属性“defaultProps”的类型不兼容。 不
- 【Leetcode】475. Heaters
猜你喜欢
SphereEx苗立尧:云原生架构下的Database Mesh研发实践
Study Notes: The Return of Machine Learning
Flink Yarn Per Job - CliFrontend
认识USB、Type-C、闪电、雷电接口
ICLR 2022最佳论文:基于对比消歧的偏标签学习
background-image使用
【MySQL系列】MySQL数据库基础
Zadig 面向开发者的自测联调子环境技术方案详解
Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers
mysql8安装make报错如何解决
随机推荐
C language Qixi is coming!It's time to show the romance of programmers!
程序员还差对象?new一个就行了
Docker实践经验:Docker 上部署 mysql8 主从复制
oozie startup error on cdh's hue, Cannot allocate containers as requested resource is greater than maximum allowed
带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~
WEB安全基础 - - - XRAY使用
QML package management
类型“FC<Props>”的参数不能赋给类型“ForwardRefRenderFunction<unknown, Props>”的参数。 属性“defaultProps”的类型不兼容。 不
为什么要使用MQ消息中间件?这几个问题必须拿下
【Leetcode】2360. Longest Cycle in a Graph
2022第六届强网杯部分wp
【Leetcode】1206. Design Skiplist
Docker搭建Mysql主从复制
字节跳动面试官:请你实现一个大文件上传和断点续传
async和await用法介绍
在MySQL中使用MD5加密【入门体验】
LeetCode_518_零钱兑换Ⅱ
Cash Ⅱ LeetCode_518_ change
Convert LocalDateTime to Date type