当前位置:网站首页>Implement queue with stack
Implement queue with stack
2022-07-06 13:20:00 【[email protected]】
Realize the idea :
The queue is first in, first out (FIFO)
The stack is last in, first out (LIFO)
You need two stacks to realize the first in, first out of the queue
Stack in Realization push operation Stack out Realization pop operation
We push Advanced data will be pushed into the bottom of the stack , When we re push Into the stack in Data one by one pop Into the stack out when , Stack in The bottom element now becomes a stack out Top element , therefore pop We only need pop Stack out The data in the completion queue is first in first out (FIFO)
When joining the team, we need to judge the stack out Is it empty , If it's not empty , You need to pop Stack out Data in , If it is empty , All push Into the stack in
When leaving the team, we need to judge the stack in Is it empty , If it's not empty , You need to pop Stack in Data in , If it is empty , All push Into the stack out
The specific code is as follows :
// Stack simulation queue
class MyQueue<E>{
public Stack<E> in = new Stack<E>(); // Join the team
public 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);
}
// Out of the team
public E poll() {
while(!in.isEmpty()) {
out.push(in.pop());
}
return out.pop();
}
}
版权声明
本文为[[email protected] Yummy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060916019988.html
边栏推荐
- 阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
- View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
- [rtklib 2.4.3 B34] version update introduction I
- TYUT太原理工大学2022数据库大题之概念模型设计
- 初识指针笔记
- [Topic terminator]
- What are the advantages of using SQL in Excel VBA
- Rich Shenzhen people and renting Shenzhen people
- Redis介绍与使用
- Fairygui bar subfamily (scroll bar, slider, progress bar)
猜你喜欢

Smart classroom solution and mobile teaching concept description

Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche

Introduction and use of redis

Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology

TYUT太原理工大学2022数据库大题之E-R图转关系模式

Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing

(超详细二)onenet数据可视化详解,如何用截取数据流绘图

System design learning (I) design pastebin com (or Bit.ly)

继承和多态(上)

Inheritance and polymorphism (I)
随机推荐
GNSS positioning accuracy index calculation
学编程的八大电脑操作,总有一款你不会
Iterable、Collection、List 的常见方法签名以及含义
View UI plus released version 1.3.1 to enhance the experience of typescript
Tyut outline of 2022 database examination of Taiyuan University of Technology
Dark chain lock (lca+ difference on tree)
Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
View UI plus released version 1.2.0 and added image, skeleton and typography components
Quickly generate illustrations
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
TYUT太原理工大学2022“mao gai”必背
阿里云微服务(四) Service Mesh综述以及实例Istio
12 excel charts and arrays
几道高频的JVM面试题
Record: newinstance() obsolete replacement method
arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
FileInputStream和BufferedInputStream的比较
Shortest Hamilton path (pressure DP)
Data manipulation language (DML)