当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
一个DG环境的ORA-16047: DGID mismatch between destination setting and target database问题排查及监听VNCR特性
记录webView显示空白的又一坑
iTOP4412 HDMI显示(4.4.4_r1)
解决::Argument ‘radius‘ is required to be an integer
基于BottomNavigationView实现底部导航栏
MarkDown基础语法笔记
"Write multi tenant" implementation of Prometheus and thanos receiver
[sm8150] [pixel4] LCD driver
第三篇:docker安装mysql容器(自定义端口)
Abnormal record-13
this.getOptions is not a function
Exception record-14
ORACLE环境遇到的ORA-600 [qkacon:FJswrwo]
Abnormal record-12
Apache Atlas 编译及安装记录
10g数据库使用大内存主机时不能启动的问题
Android面试计网面经大全【持续更新中。。。】
Information:2021/9/29 10:01 - Build completed with 1 error and 0 warnings in 11s 30ms Error异常处理
Prometheus monitoring method and index interpretation of influxdb
iTOP4412内核反复重启