当前位置:网站首页>Using two stacks to implement queues [two first in first out is first in first out]
Using two stacks to implement queues [two first in first out is first in first out]
2022-06-27 23:58:00 【REN_ Linsen】
Queues are implemented with two stacks
Preface
The queue is characterized by first in, first out , The stack is characterized by last in, first out , If LIFO and then new stack , That's first in, first out .
One 、 Queues are implemented with two stacks

Two 、 Two in and out to get the queue
package everyday.simple;
import java.util.Stack;
// Two stacks realize the first in, first out of queues .
public class CQueue {
/* target: Use a LIFO stack Implement a first in first out queue . The way in and out of the stack and the way in the queue just want to change , If you put the elements in the stack on another stack , Then the order of this stack , Namely The first element enters the stack 1 The order of . */
Stack<Integer> in, out;
public CQueue() {
in = new Stack<>();
out = new Stack<>();
}
public void appendTail(int value) {
in.push(value);
}
public int deleteHead() {
// if in return, First of all , Make a good arrangement for the location of conditional visits , Try not to nest if and if else; second , Combine blocks of code that do the same thing under different conditions .
// Both stacks are empty
if (out.isEmpty() && in.isEmpty()) return -1;
// in There are elements in the stack , hold in All stack elements are taken out
if (out.isEmpty()) while (!in.isEmpty()) out.push(in.pop());
// out There are elements in the stack at this time , Just get it out of the stack .
return out.pop();
}
}
summary
reference
边栏推荐
- [sword finger offer] 47 Maximum value of gifts
- webserver流程图——搞懂webserver各模块间调用关系
- What if Fiddler fails to listen to the interface
- 安全省油環保 駱駝AGM啟停電池魅力十足
- ClickOnce error deploying ClickOnce application - the reference in the manifest does not match the identity of the downloaded assembly
- How to solve the problem that the browser developed with CeF3 does not support flash
- Excel print settings public header
- 如何找到外文文献对应的中文文献?
- 赛尔笔记|视频文本预训练简述
- Zero foundation self-study SQL course | if function
猜你喜欢

安全省油环保 骆驼AGM启停电池魅力十足

【PCL自学:Segmentation3】基于PCL的点云分割:区域增长分割

C language character pointer and string initialization

c语言之字符串数组

ASP. Net warehouse purchase, sales and inventory ERP management system source code ERP applet source code

零基础自学SQL课程 | CASE函数

C WinForm reads the resources picture

MySQL企业级参数调优实践分享

Windows环境下的ELK——Logstash+Mysql(4)

【Try to Hack】veil-evasion免杀
随机推荐
Golang - the difference between new and make
Transmitting and receiving antenna pattern
c语言-日期格式化[通俗易懂]
[try to hack] kill evaluation
Mise en œuvre du pool de Threads: les sémaphores peuvent également être considérés comme de petites files d'attente
Safe, fuel-efficient and environment-friendly camel AGM start stop battery is full of charm
[microservices sentinel] sentinel data persistence
Is not null and in SQL= Difference between null
【AI应用】NVIDIA Tesla V100S-PCIE-32GB的详情参数
[AI application] detailed parameters of NVIDIA geforce RTX 1080ti
Course strategy sharing plan of Zhejiang University
Super outline exercises
积分体系和营销活动结合在一起有哪些玩法
Zero foundation self-study SQL course | complete collection of date functions in SQL
Usage of vivado vio IP
单细胞数据清洗的这5个步骤你会做吗?
十大券商注册账户安全吗,会有风险吗?
golang使用mongo-driver操作——查(数组相关)
NDSS 2022 received list
发射,接收天线方向图