当前位置:网站首页>利用栈实现队列的出队入队
利用栈实现队列的出队入队
2022-04-23 06:05:00 【大林子先森】
* 实现思路: * * 1)准备两个栈stack1和stack2 * 2)入队列时,数据先向stack1中push * 3)出队列时,先判断stack2是否是空的, * 3.1)如果是空的就将stack1中的元素依次向stack2中push(直至stack1为空),然后从stack2中弹出一个元素返回 * 3.2)如果不是空的,说明stack1中的数据全部转到了stack2,直接从stack2中返回。 * 后续插入直接插入到stack1中,当stack2中的元素空了之后,再将stack1中的元素全部放入stack2中。以此循环往复,实现FIFO效果 *
上rehuhu的代码!!
package com.lll.algorithms.collection.queue;
import java.util.Stack;
/**
* @ClassName : MyQueueWithStack
* @Description: 利用栈实现队列的出队入队
* @Author: liulianglin
* @Date: 2021/10/14 15:17
* @Version : 1.0
*
* 实现思路:
*
* 1)准备两个栈stack1和stack2
* 2)入队列时,数据先向stack1中push
* 3)出队列时,先判断stack2是否是空的,
* 3.1)如果是空的就将stack1中的元素依次向stack2中push(直至stack1为空),然后从stack2中弹出一个元素返回
* 3.2)如果不是空的,说明stack1中的数据全部转到了stack2,直接从stack2中返回。
* 后续插入直接插入到stack1中,当stack2中的元素空了之后,再将stack1中的元素全部放入stack2中。以此循环往复,实现FIFO效果
*
*/
public class MyQueueWithStack<T> {
private Stack<T> stack1 = new Stack<>();
private Stack<T> stack2 = new Stack<>();
public boolean mpush(T t){
return stack1.push(t)==null ? false:true;
}
public T mpop(){
if (stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
public static void main(String[] args) {
MyQueueWithStack<Integer> myQueueWithStack = new MyQueueWithStack<>();
myQueueWithStack.mpush(1);
myQueueWithStack.mpush(3);
myQueueWithStack.mpush(9);
System.out.println(myQueueWithStack.mpop());
System.out.println(myQueueWithStack.mpop());
System.out.println(myQueueWithStack.mpop());
}
}
版权声明
本文为[大林子先森]所创,转载请带上原文链接,感谢
https://blog.csdn.net/liulianglin/article/details/121390644
边栏推荐
- pycharm Install packages failed
- oracle计算两日期相差多少秒,分钟,小时,天
- Error alarm of Postgres master-slave replication delay monitoring
- When switch case, concatenate the error case and if of the conventional judgment expression and use L
- Exception record-7
- EMR Based offline data analysis - polite feedback
- Practice using polardb and ECs to build portal websites
- RAC环境集群组件gpnp未启动成功问题分析
- Abnormal record-22
- ORACLE表有逻辑坏块时EXPDP导出报错排查
猜你喜欢

使用prom-label-proxy实现Prometheus Thanos的基于标签的多租户读

Analysis of Rdam principle

Chaos vous emmène au projet chaos.

Oracle Job定时任务的使用详解

Chaos带你快速上手混沌工程

阿里矢量库的图标使用教程(在线,下载)

冬季实战营动手实战-上云必备环境准备,动手实操快速搭建LAMP环境 领鼠标 云小宝 背包 无影

qs.stringify 接口里把入参转为&连接的字符串(配合application/x-www-form-urlencoded请求头)

qs. In the stringify interface, the input parameter is converted into a & connected string (with the application / x-www-form-urlencoded request header)

Redis practice notes and source code analysis
随机推荐
OVS and OVS + dpdk architecture analysis
冬季实战营动手实战-上云必备环境准备,动手实操快速搭建LAMP环境 领鼠标 云小宝 背包 无影
Abnormal record-22
异常记录-10
switch case时连写常规判断表达式的错误 case和if并用 l
try catch 不能捕获异步错误
VirtualBox如何修改“网络地址转换(NAT)”网络模式下分配给虚拟机的IP网段
关于我
10g数据库使用大内存主机时不能启动的问题
阿里云日志服务sls的典型应用场景
How does VirtualBox modify the IP network segment assigned to the virtual machine in the "network address translation (NAT)" network mode
RAC环境alert日志报错Drop transient type: SYSTP2JW0acnAurDgU1sBqMBryw==的排查
发布自定义插件到本地服务器
常用于融合去重的窗口函数row_number
异常记录-15
Oracle Job定时任务的使用详解
Abnormal record-20
基于ECS搭建云上博客(体验有礼)
Exception record-9
基於ECS搭建雲上博客(雲小寶碼上送祝福,免費抽iphone13任務詳解)