当前位置:网站首页>Stack simulation queue
Stack simulation queue
2022-07-25 22:48:00 【Cool hair】
The characteristic of the stack : Last in, first out (Last In First Out)
The characteristics of the queue are : fifo (First In First Out)
The idea of simulating queues with stacks : First define two stacks in and out, With the stack in To store elements , Stack out To output elements , Press into the stack in order when entering the team in in , From the stack in Pop up and press into the stack out in , According to the characteristics of stack , Stack in order out Just get the elements out of the stack . The specific implementation is as follows :
public static void main(String[] args) {
MyQueue<String> queue = new MyQueue<String>();
queue.offer("A1");
queue.offer("A2");
queue.offer("A3");
queue.offer("A4");
queue.offer("A5");
System.out.println(queue.poll() + " Out of the team ");
// Traverse
while (!queue.isEmpty()) {
System.out.println(queue.poll());
}
}
}
// Use stack to simulate queue
class MyQueue<E> {
private Stack<E> in = new Stack<E>(); // Join the team
private Stack<E> out = new Stack<E>(); // Out of the stack
// The team
public void offer(E e) {
while (!out.isEmpty()) {
in.push(out.pop());
}
in.push(e);
}
public boolean isEmpty() {
return in.size() == 0 && out.size() == 0;
}
// Out of the team
public E poll() {
while (!in.isEmpty()) {
out.push(in.pop());
}
return out.pop();
}Be careful : Make sure to stack when joining the team out There's no element in it , That is, put them all on the stack in in , When leaving the team, you should also ensure that the stack in There are no elements in it , That is, all out of the team !!
边栏推荐
- Experiment 1, experiment 2 and Experiment 3 of assembly language and microcomputer principle: branch program design / loop program design / subroutine design
- Hcie is finally in hand, and the road begins
- 【集训DAY11】Nescafe【贪心】
- [training Day11] Calc [mathematics]
- 分享两个音乐播放地址
- Websocket summary
- Why is Google's internal tools not suitable for you?
- [training day15] simple calculation [tree array] [mathematics]
- ribbon 执行逻辑源码解析
- [training day15] good name [hash]
猜你喜欢

我们为什么要推出Getaverse?

武汉理工大学第三届程序设计竞赛 B-拯救DAG王国(拓扑性质处理可达性统计问题)

冯诺依曼体系结构

【集训DAY13】Out race【数学】【动态规划】

Madness. Smbms (supermarket order management system)

Anaconda~Upload did not complete.

XSS tool beef XSS installation and use

Summary 1 - deep learning - basic knowledge learning

Compiler introduction

JSON object
随机推荐
Anaconda~Upload did not complete.
Select structure if branch structure
软件测试 pytest pytest的命名规则 用例的前后置 conftest.py 定制allure报告 @pytest.mark.parametrize()装饰器作数据驱动
MatrixCube揭秘102——300行实现的完整分布式存储系统MatrixKV
Simple setting of drop-down triangle
Domain oriented model programming
Compiler introduction
Recyclerview computehorizontalscrollextend computehorizontalscrollrange computehorizontalscroll for calculating the sliding distance
721. Account consolidation ●●, and collection
[training Day12] tree! Tree! Tree! [greed] [minimum spanning tree]
Use of qvariant
Matrix of C language
2020-09-17
C语言逆序打印字符串的两种方法
武汉理工大学第三届程序设计竞赛 B-拯救DAG王国(拓扑性质处理可达性统计问题)
Solve several common problems
【集训DAY15】Boring【树形DP】
We media people must have four resource tools, each of which is very practical
Force deduction solution summary 919 complete binary tree inserter
Google analyzes how UA can be transferred to the latest version of GA4