当前位置:网站首页>Using queue to realize stack
Using queue to realize stack
2022-04-23 07:10:00 【Da Linzi xiansen】
Realize the idea :
1) Prepare two queues queue1, queue2( Queue feature first in first out , Stack features first in and last out ) 2) When entering the stack , Elements are inserted into queue1 in 3) Out of the stack , Do the following : 3.1) If queue2 It's empty , Will queue1 towards queue2 Copy , The last element left returns 3.2) If queue2 Not empty , Will queue2 towards queue1 Copy , The last element left returns
Go straight to the code :
import java.util.LinkedList;
import java.util.Queue;
/**
* @ClassName : MyStackWithQueue
* @Description: Stack through queue
* @Author: liulianglin
* @Date: 2021/11/17 23:36
* @Version : 1.0
*
Realize the idea :
1) Prepare two queues queue1, queue2( Queue feature first in first out , Stack features first in and last out )
2) When entering the stack , Elements are inserted into queue1 in
3) Out of the stack , Do the following :
3.1) If queue2 It's empty , Will queue1 towards queue2 Copy , The last element left returns
3.2) If queue2 Not empty , Will queue2 towards queue1 Copy , The last element left returns
*/
public class MyStackWithQueue {
private Queue<Integer> queue1 = new LinkedList<>();
private Queue<Integer> queue2 = new LinkedList<>();
/**
* Insert
* @return
*/
private boolean push(int ele){
return queue1.add(ele);
}
private Integer pop(){
if (queue2.size() == 0){
// If queue2 It's empty , Will queue1 towards queue2 Copy , The last element left returns
while (queue1.size() > 1) {
queue2.add(this.queue1.poll());
}
// Back at this point queue1 Last element of
return this.queue1.poll();
}else{
// If queue2 Not empty , Will queue2 towards queue1 Copy , The last element left returns
while(queue2.size() > 1){
queue1.add(this.queue2.poll());
}
return this.queue2.poll();
}
}
public static void main(String[] args) {
MyStackWithQueue myStackWithQueue = new MyStackWithQueue();
myStackWithQueue.push(1);
myStackWithQueue.push(2);
myStackWithQueue.push(3);
System.out.println(myStackWithQueue.pop());
System.out.println(myStackWithQueue.pop());
System.out.println(myStackWithQueue.pop());
}
}
版权声明
本文为[Da Linzi xiansen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230605322640.html
边栏推荐
- Abnormal record-13
- Exception record-5
- Error alarm of Postgres master-slave replication delay monitoring
- Django::Did you install mysqlclient?
- Chaos takes you to the chaos project quickly
- this. getOptions is not a function
- Exception record-6
- Apache SeaTunnel 2.1.0部署及踩坑
- iTOP4412 HDMI显示(4.0.3_r1)
- Android面试计网面经大全【持续更新中。。。】
猜你喜欢
随机推荐
Information:2021/9/29 10:01 - Build completed with 1 error and 0 warnings in 11s 30ms Error异常处理
oracle 修改默认临时表空间
谷歌AdMob广告学习
Itop4412 cannot display boot animation (4.0.3_r1)
从0开始封装一套项目的网络请求框架
oracle给对象重命名
組件化學習
Django::Did you install mysqlclient?
窗口分析函数LAST_VALUE,FIRST_VALUE,lag,lead
Apache SeaTunnel 2.1.0部署及踩坑
几款电纸书阅读器参数对比
Cause: dx.jar is missing
oracle中生成32位uuid
iTOP4412 HDMI显示(4.0.3_r1)
"Write multi tenant" implementation of Prometheus and thanos receiver
oracle库恢复数据
this. getOptions is not a function
org.xml.sax.SAXParseException; lineNumber: 141; columnNumber: 252; cvc-complex-type.2.4.a: 发现了以元素 ‘b
Oracle RAC数据库实例启动异常问题分析IPC Send timeout
RAC环境中openssh版本对SSH互信创建的影响









