当前位置:网站首页>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
边栏推荐
- View UI Plus 發布 1.3.1 版本,增强 TypeScript 使用體驗
- 阿里云微服务(四) Service Mesh综述以及实例Istio
- Design a key value cache to save the results of the most recent Web server queries
- Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
- 面试必备:聊聊分布式锁的多种实现!
- MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
- arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
- Record: solution of 404 error of servlet accessing database in dynamic web project
- How do architects draw system architecture blueprints?
- 【话题终结者】
猜你喜欢
《软件测试》习题答案:第一章
2-year experience summary, tell you how to do a good job in project management
架构师怎样绘制系统架构蓝图?
What are the advantages of using SQL in Excel VBA
The overseas sales of Xiaomi mobile phones are nearly 140million, which may explain why Xiaomi ov doesn't need Hongmeng
凡人修仙学指针-2
Data manipulation language (DML)
TYUT太原理工大学2022数据库大题之分解关系模式
学编程的八大电脑操作,总有一款你不会
121 distributed interview questions and answers
随机推荐
2年经验总结,告诉你如何做好项目管理
165. Compare version number - string
TYUT太原理工大学2022数据库大题之E-R图转关系模式
arduino+水位传感器+led显示+蜂鸣器报警
Relational algebra of tyut Taiyuan University of technology 2022 database
Heap sort [handwritten small root heap]
Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
西安电子科技大学22学年上学期《基础实验》试题及答案
凡人修仙学指针-2
Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
[while your roommate plays games, let's see a problem]
13 power map
记录:newInstance()过时的代替方法
Atomic and nonatomic
Application architecture of large live broadcast platform
西安电子科技大学22学年上学期《射频电路基础》试题及答案
MySQL limit x, -1 doesn't work, -1 does not work, and an error is reported
继承和多态(下)
Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
Tyut Taiyuan University of technology 2022 introduction to software engineering examination question outline