当前位置:网站首页>Two queues implement a stack
Two queues implement a stack
2022-08-07 23:20:00 【Throw off the shackles】

import java.util.LinkedList;import java.util.Queue;//Two queues implement a stack, imagine the following is a stackpublic class StackByQuene {Queue queue1=new LinkedList<>();Queue queue2=new LinkedList<>();public void push(int node) //The first push to queue2, the next push to a non-empty queue,{if(!queue1.isEmpty())queue1.offer(node);elsequeue2.offer(node);}public Integer pop() //Which is not empty, which is popped into the other, leaving the last node, which is popping{if(!queue1.isEmpty()&&queue2.isEmpty()){while(queue1.size()>1){queue2.offer( queue1.poll());}return queue1.poll();}if(queue1.isEmpty()&&!queue2.isEmpty()){while(queue2.size()>1){queue1.offer(queue2.poll());}return queue2.poll();}return null;}@Overridepublic String toString() {return "StackQuene{" +"queue1=" + queue1 +", queue2=" + queue2 +'}';}public static void main(String[] args) {StackByQuene s=new StackByQuene();s.push(1);s.push(2);s.push(3);System.out.println(s);s.pop();System.out.println(s);s.push(4);s.push(5);System.out.println(s);s.pop();System.out.println(s);}} 边栏推荐
- MYSQL高级篇----简介介绍
- Unity编辑器拓展--Inspector拓展
- C语言:打印整数二进制的奇数位和偶数位
- Unity editor extension -- Hierarchy extension
- MySQL先分组再取组内最大最小记录
- Why does PXE boot with grub2 and UEFI fails with error “File not found“ ?
- 云服务器中mongodb配置账号密码 图文讲解(全)
- PersFormer: 3D Lane Detection via Perspective Transformer and the OpenLane Benchmark 论文笔记
- VS 配置 OpenCV (亲测可用)
- 性能测试最常见的面试题分析与答案
猜你喜欢
随机推荐
高性能云原生数据对象存储MinIO实战-中
D. Chip Move(DP,优化时间和空间)
故障:不能打开 Office 应用程序,并指示有账户正在登录
Eureka Basics
我想进行炒股开户,开户安全吗
SQL堆叠注入详解
win10 install pycuda2022
【UiPath2022+C#】UiPath 练习-Excel和数据表
(二)音视频:MediaCodec编码桌面信息 完整Demo 进一步理解H264
MPLS网络向SRv6网络演进
If there is a private method in the Controller, can it be accessed successfully?
谷歌联合高校发布端到端的全景分割方法MaX-DeepLab,图像分割的伪影大幅减少且不含手动模块
Rebate 200, the number of islands
Expansion of the Unity editor - Scene view custom operations
【云原生--Kubernetes】PV、PVC
Presto Stage的生成过程
【数据挖掘】滴滴公司数据挖掘工程师笔试题
C语言:矩阵相等判定
dart 方法与属性私有化
Hands-on deep learning_target detection









