当前位置:网站首页>Using stack to realize queue out and in
Using stack to realize queue out and in
2022-04-23 07:10:00 【Da Linzi xiansen】
* Realize the idea : * * 1) Prepare two stacks stack1 and stack2 * 2) When in line , Data first to stack1 in push * 3) When out of queue , First judge stack2 Is it empty , * 3.1) If it is empty, it will stack1 The elements in the are in turn stack2 in push( until stack1 It's empty ), And then from stack2 A pop-up element returns * 3.2) If it's not empty , explain stack1 All the data in has been transferred to stack2, Directly from stack2 Back in . * Subsequent inserts are inserted directly into stack1 in , When stack2 After the element in is empty , then stack1 Put all the elements in stack2 in . And so on , Realization FIFO effect *
On rehuhu Code for !!
package com.lll.algorithms.collection.queue;
import java.util.Stack;
/**
* @ClassName : MyQueueWithStack
* @Description: Use the stack to realize the queue out and in
* @Author: liulianglin
* @Date: 2021/10/14 15:17
* @Version : 1.0
*
* Realize the idea :
*
* 1) Prepare two stacks stack1 and stack2
* 2) When in line , Data first to stack1 in push
* 3) When out of queue , First judge stack2 Is it empty ,
* 3.1) If it is empty, it will stack1 The elements in the are in turn stack2 in push( until stack1 It's empty ), And then from stack2 A pop-up element returns
* 3.2) If it's not empty , explain stack1 All the data in has been transferred to stack2, Directly from stack2 Back in .
* Subsequent inserts are inserted directly into stack1 in , When stack2 After the element in is empty , then stack1 Put all the elements in stack2 in . And so on , Realization FIFO effect
*
*/
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());
}
}
版权声明
本文为[Da Linzi xiansen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230605322681.html
边栏推荐
猜你喜欢
随机推荐
How does thanos configure different data retention durations for different tenants
Oracle Job定时任务的使用详解
RAC环境数据库节点参数设置不当导致监听无法连接问题排查
iTOP4412 HDMI显示(4.0.3_r1)
mysql和pg库遇到冲突数据时的两种处理方式
Error alarm of Postgres master-slave replication delay monitoring
Chaos帶你快速上手混沌工程
oracle用delete删除数据所需时间测试
利用队列实现栈
Abnormal record-21
AVD Pixel_2_API_24 is already running.If that is not the case, delete the files at C:\Users\admi
从0开始封装一套项目的网络请求框架
npm ERR code 500解决
iTOP4412无法显示开机动画(4.0.3_r1)
"Write multi tenant" implementation of Prometheus and thanos receiver
Prometheus thanos Quick Guide
pg库对姓名进行校验
Oracle RAC数据库实例启动异常问题分析IPC Send timeout
RAC环境中openssh版本对SSH互信创建的影响
ORACLE表有逻辑坏块时EXPDP导出报错排查









