Java编程用两个栈实现队列代码分享
作者:diu_brother 时间:2023-03-22 01:05:05
题目:用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
经典题,不多说,直接上代码
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
int pop = stack2.pop();
while(!stack2.isEmpty()){
stack1.push(stack2.pop());
}
return pop;
}
}
来源:http://blog.csdn.net/diu_brother/article/details/52558144
标签:java,算法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 动态数组的实现示例
2022-02-27 07:05:25
![](https://img.aspxhome.com/file/2023/6/63636_0s.gif)
SpringBoot使用POI进行Excel下载
2022-06-14 13:16:13
解决Callable的对象中,用@Autowired注入别的对象失败问题
2023-11-29 13:23:46
java实现导出Excel的功能
2022-07-17 15:57:28
![](https://img.aspxhome.com/file/2023/2/101902_0s.png)
Spring MVC注解式开发使用详解
2021-07-08 23:27:50
![](https://img.aspxhome.com/file/2023/2/129232_0s.jpg)
C# 嵌入dll 的方法
2022-11-06 08:39:10
![](https://img.aspxhome.com/file/2023/1/116591_0s.png)
Android自定义有限制区域图例角度自识别涂鸦工具类中篇
2021-06-16 16:21:46
![](https://img.aspxhome.com/file/2023/8/138508_0s.jpg)
JavaWeb实现多文件上传及zip打包下载
2023-11-16 16:41:25
Java 常量池详解之字符串常量池实现代码
2022-09-09 22:12:03
![](https://img.aspxhome.com/file/2023/7/66937_0s.jpg)
自己写的java日志类和方法代码分享
2021-08-18 19:34:13
详解关于AndroidQ获取不到imsi解决方案
2023-02-21 14:23:50
浅析Java常用API(Scanner,Random)匿名对象
2023-05-04 09:42:39
Java通过jersey实现客户端图片上传示例
2022-09-17 15:44:24
![](https://img.aspxhome.com/file/2023/4/86804_0s.jpg)
关于RxJava的一些特殊用法小结
2022-06-13 13:31:54
java实现简易超市管理系统 附源码下载
2021-11-05 18:58:30
springboot 返回json格式数据时间格式配置方式
2023-12-17 23:27:10
Java SpringMVC异步处理详解
2021-08-10 15:03:58
![](https://img.aspxhome.com/file/2023/0/62050_0s.png)
Android实现Tab布局的4种方式(Fragment+TabPageIndicator+ViewPager)
2023-02-26 10:45:41
![](https://img.aspxhome.com/file/2023/6/114336_0s.gif)
vista和win7在windows服务中交互桌面权限问题解决方法:穿透Session 0 隔离
2021-06-16 04:05:47
![](https://img.aspxhome.com/file/2023/0/72480_0s.png)
SpringBoot中配置Web静态资源路径的方法
2023-10-26 03:49:21
![](https://img.aspxhome.com/file/2023/4/72564_0s.png)