当前位置:网站首页>利用队列实现栈
利用队列实现栈
2022-04-23 06:05:00 【大林子先森】
实现思路:
1)准备两个队列 queue1, queue2(队列特性先进先出,栈的特性先进后出) 2)入栈时,元素依次插入到queue1中 3)出栈时,做如下处理: 3.1)如果queue2为空,则将queue1向queue2拷贝,剩最后一个元素返回 3.2)如果queue2不为空,则将queue2向queue1拷贝,剩最后一个元素返回
直接上代码:
import java.util.LinkedList;
import java.util.Queue;
/**
* @ClassName : MyStackWithQueue
* @Description: 通过队列实现栈
* @Author: liulianglin
* @Date: 2021/11/17 23:36
* @Version : 1.0
*
实现思路:
1)准备两个队列 queue1, queue2(队列特性先进先出,栈的特性先进后出)
2)入栈时,元素依次插入到queue1中
3)出栈时,做如下处理:
3.1)如果queue2为空,则将queue1向queue2拷贝,剩最后一个元素返回
3.2)如果queue2不为空,则将queue2向queue1拷贝,剩最后一个元素返回
*/
public class MyStackWithQueue {
private Queue<Integer> queue1 = new LinkedList<>();
private Queue<Integer> queue2 = new LinkedList<>();
/**
* 插入
* @return
*/
private boolean push(int ele){
return queue1.add(ele);
}
private Integer pop(){
if (queue2.size() == 0){
//如果queue2为空,则将queue1向queue2拷贝,剩最后一个元素返回
while (queue1.size() > 1) {
queue2.add(this.queue1.poll());
}
//返回此时queue1的最后一个元素
return this.queue1.poll();
}else{
//如果queue2不为空,则将queue2向queue1拷贝,剩最后一个元素返回
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());
}
}
版权声明
本文为[大林子先森]所创,转载请带上原文链接,感谢
https://blog.csdn.net/liulianglin/article/details/121390957
边栏推荐
- Prometheus monitoring method and index interpretation of influxdb
- select命令产生redo日志问题的分析
- Implementation of multi tenant read and write in Prometheus cortex
- EMR Based offline data analysis - polite feedback
- RAC环境报错ORA-00239: timeout waiting for control file enqueue排查
- Abnormal record-15
- Build a cloud blog based on ECS (send blessings on the cloud Xiaobao code and draw iphone13 for free)
- Prometheus的relabel_configs和metric_relabel_configs解释及用法示例
- 异常记录-18
- JS function package foreach use return can not jump out of the outer function
猜你喜欢

Problems related to Prometheus cortex using block storage

Prometheus Cortex使用Block存储时的相关问题

Winter combat camp hands-on combat - MySQL database rapid deployment practice lead mouse cloud Xiaobao

OVS and OVS + dpdk architecture analysis

Chaos帶你快速上手混沌工程

Thanos compact component test summary (processing historical data)

PG SQL截取字符串到指定字符位置

oracle undo使用率高问题处理

Dolphinscheduler配置Datax踩坑记录

Ali vector library Icon tutorial (online, download)
随机推荐
Exception record-6
Winter combat camp hands-on combat - first understand the cloud foundation, hands-on practice ECS ECS ECS novice on the road to get the mouse cloud Xiaobao backpack shadowless
Build an OSS based image sharing website - polite feedback
Abnormal record-18
Abnormal record-21
Redis practice notes and source code analysis
TC ebpf practice
Prometheus监控influxdb的方法及指标释义
Prometheus Thanos快速指南
oracle中生成32位uuid
发布自定义插件到本地服务器
微服务架构核心概念
Apache SeaTunnel 2.1.0部署及踩坑
Alertmanager重复/缺失告警现象探究及两个关键参数group_wait和group_interval的释义
解决:You have 18 unapplied migration(s). Your project may not work properly until you apply
Dolphinscheduler集成Flink任务踩坑记录
Prometheus alarm record persistence (historical alarm saving and Statistics)
Error alarm of Postgres master-slave replication delay monitoring
Abnormal record-11
EMR Based offline data analysis - polite feedback