当前位置:网站首页>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
边栏推荐
- arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
- Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
- Network layer 7 protocol
- 初识C语言(下)
- Alibaba cloud microservices (IV) service mesh overview and instance istio
- Inheritance and polymorphism (Part 2)
- 十分鐘徹底掌握緩存擊穿、緩存穿透、緩存雪崩
- First acquaintance with C language (Part 2)
- 初识指针笔记
- MPLS experiment
猜你喜欢
TYUT太原理工大学2022数据库大题之分解关系模式
How to ensure data consistency between MySQL and redis?
Data manipulation language (DML)
Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
Quickly generate illustrations
IPv6 experiment
IPv6 experiment
Interview Essentials: talk about the various implementations of distributed locks!
几道高频的JVM面试题
System design learning (III) design Amazon's sales rank by category feature
随机推荐
165. Compare version number - string
Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology
CorelDRAW plug-in -- GMS plug-in development -- Introduction to VBA -- GMS plug-in installation -- Security -- macro Manager -- CDR plug-in (I)
Small exercise of library management system
167. Sum of two numbers II - input ordered array - Double pointers
图书管理系统小练习
Application architecture of large live broadcast platform
分支语句和循环语句
【快趁你舍友打游戏,来看道题吧】
Branch and loop statements
一文搞定 UDP 和 TCP 高频面试题!
TYUT太原理工大学2022“mao gai”必背
抽象类和接口
FileInputStream和BufferedInputStream的比较
How do architects draw system architecture blueprints?
Exception: ioexception:stream closed
Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
RTKLIB: demo5 b34f. 1 vs b33
记录:动态Web项目servlet访问数据库404错误之解决
Database operation of tyut Taiyuan University of technology 2022 database