当前位置:网站首页>双队列实现栈?双栈实现队列?
双队列实现栈?双栈实现队列?
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();
}
}
边栏推荐
猜你喜欢

How to solve the error when mysql8 installs make

信息系统项目管理师必背核心考点(五十七)知识管理工具

Docker实践经验:Docker 上部署 mysql8 主从复制

在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决

Spark Sql之join on and和where

Get piggy homestay (short-term rental) data

Classical Literature Reading--DLO

Excel表格数据导入MySQL数据库

Axure教程-新手入门基础(小白强烈推荐!!!)

OpenCV DNN blogFromImage()详解
随机推荐
洞见云原生微服务及微服务架构浅析
软件测试之移动APP安全测试简析,北京第三方软件检测机构分享
在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
LeetCode_279_完全平方数
CDH6 Hue to open a "ASCII" codec can 't encode characters
【MySQL系列】MySQL索引事务
How to reinstall Win11?One-click method to reinstall Win11
WEB安全基础 - - - XRAY使用
【无标题】
几道关于golang并发的面试题
Docker实践经验:Docker 上部署 mysql8 主从复制
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
Flink学习第三天——一文带你了解什么是Flink流?
ICLR 2022 Best Paper: Partial Label Learning Based on Contrastive Disambiguation
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~
Docker搭建Mysql主从复制
EasyExcel的简单读取操作
Several interview questions about golang concurrency
C语言七夕来袭!是时候展现专属于程序员的浪漫了!
在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决