当前位置:网站首页>双队列实现栈?双栈实现队列?
双队列实现栈?双栈实现队列?
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();
}
}
边栏推荐
猜你喜欢
不就是个TCC分布式事务,有那么难吗?
contentEditable属性
Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)
Win11如何获得最佳电源效率?
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~
How to reinstall Win11?One-click method to reinstall Win11
软件测试之移动APP安全测试简析,北京第三方软件检测机构分享
使用 Zadig 交付云原生微服务应用
如何优雅的消除系统重复代码
企业防护墙管理,有什么防火墙管理工具?
随机推荐
Spark Sql之join on and和where
类型“FC<Props>”的参数不能赋给类型“ForwardRefRenderFunction<unknown, Props>”的参数。 属性“defaultProps”的类型不兼容。 不
使用Ganache、web3.js和remix在私有链上部署并调用合约
一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置
在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
DVWA靶场环境搭建
Win11如何获得最佳电源效率?
如何重装Win11?一键重装Win11方法
REST会消失吗?事件驱动架构如何搭建?
CDH6 Hue to open a "ASCII" codec can 't encode characters
【ACWing】230. 排列计数
LeetCode_518_零钱兑换Ⅱ
工作5年,测试用例都设计不好?来看看大厂的用例设计总结
在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决
FAST-LIO2代码解析(二)
Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
2022第六届强网杯部分wp
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
mysql8安装make报错如何解决
security session concurrency management