当前位置:网站首页>用栈实现队列
用栈实现队列
2022-07-06 09:19:00 【[email protected]】
实现思路:
队列是先进先出(FIFO)
栈是后进先出(LIFO)
需要用两个栈来实现队列的先进先出
栈in实现push操作 栈out实现pop操作
我们push数据时先进的数据会被压入栈底,当我们重新把push进栈in的数据一个一个pop进栈out时,栈in底的元素此刻就成为了栈out顶部元素,所以pop数据时我们只需要pop栈out中的数据即完成队列的先进先出(FIFO)
入队时我们需要判断栈out是否为空,如果不为空,则需要pop栈out中的数据,如果为空,全部push进栈in
出队时我们需要判断栈in是否为空,如果不为空,则需要pop栈in中的数据,如果为空,全部push进栈out
具体代码如下:
//栈模拟队列
class MyQueue<E>{
public Stack<E> in = new Stack<E>(); // 入队栈
public Stack<E> out = new Stack<E>();// 出队栈
// 入队
public void offer(E e) {
while(!out.isEmpty()) {
in.push(out.pop());
}
in.push(e);
}
// 出队
public E poll() {
while(!in.isEmpty()) {
out.push(in.pop());
}
return out.pop();
}
}
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_49194330/article/details/124736851
边栏推荐
- Edit distance (multi-source BFS)
- Answer to "software testing" exercise: Chapter 1
- 初识C语言(下)
- 架构师怎样绘制系统架构蓝图?
- Network layer 7 protocol
- Smart classroom solution and mobile teaching concept description
- One article to get UDP and TCP high-frequency interview questions!
- Record: newinstance() obsolete replacement method
- [algorithm] sword finger offer2 golang interview question 5: maximum product of word length
- Pride-pppar source code analysis
猜你喜欢

How do architects draw system architecture blueprints?

系统设计学习(三)Design Amazon‘s sales rank by category feature
![[GNSS data processing] Helmert variance component estimation analysis and code implementation](/img/4e/ff0334cf9a2a37096778a8c5719a4e.jpg)
[GNSS data processing] Helmert variance component estimation analysis and code implementation

121道分布式面试题和答案

一文搞定 UDP 和 TCP 高频面试题!

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

Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology

How to ensure data consistency between MySQL and redis?

Interview Essentials: talk about the various implementations of distributed locks!

2年经验总结,告诉你如何做好项目管理
随机推荐
IPv6 experiment
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
Alibaba cloud microservices (IV) service mesh overview and instance istio
Error: sorting and subscript out of bounds
Alibaba cloud microservices (I) service registry Nacos, rest template and feign client
First acquaintance with C language (Part 2)
TYUT太原理工大学2022数据库大题之E-R图转关系模式
Inheritance and polymorphism (I)
Basic DOS commands
TYUT太原理工大学2022软工导论考试题型大纲
Code example of MATLAB reading GNSS observation value o file
Network layer 7 protocol
System design learning (III) design Amazon's sales rank by category feature
记录:初次cmd启动MySQL拒接访问之解决
Heap sort [handwritten small root heap]
Sharing ideas of on-chip transplantation based on rtklib source code
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
Application architecture of large live broadcast platform