当前位置:网站首页>Stack simulation queue
Stack simulation queue
2022-07-26 07:53:00 【I like iced black tea】
queue (Queue) Characteristics : fifo FIFO Stack (Stack) Characteristics : First in, then out FILO
To use a stack to simulate a queue , You need to use two stack spaces to store data , data structure as follows :

Because the characteristic of stack is First in, then out , The characteristics of the queue are fifo , So we need to Define two stacks , Stack (in) Used for The team operation , Stack (out) Used for Out of the team operation . First put the data that needs to be queued into the stack (in), The first data entered is stored at the bottom of the stack , The last entered data is stored at the top of the stack , To get out of the team, you need to stack (in) Data in the stack again (out) So that the first stack (in) The data is on the stack (out) The position in the is the top of the stack , The data that is finally put into the stack is on the stack (out) At the bottom of the stack , Finally on the stack (out) Carry out stack out operation , You can realize the queue out operation .
In the process of entering and leaving the team , What we should pay attention to is : When you join the team , Need to judge stack (out) Is it empty , If it is empty , Directly on the stack (in) Just join the team , If not be empty , You also need to stack (out) Store the data in the stack first (in) Inside , In the process of joining the team . When leaving the team, you need to judge the stack (in) Is it empty , If it is empty , Directly on the stack (out) Just go out of the queue , If not be empty , You also need to stack (in) Store the data in the stack first (out) Inside , Out of line operation .

Code implementation :
public class Main {
public static void main(String[] args) {
MyQueue<String> queue = new MyQueue<String>();
queue.offer("A1");
System.out.println(queue.poll()+" Out of the team ");
queue.offer("A2");
System.out.println(queue.poll()+" Out of the team ");
System.out.println(queue);
// Traverse MyQueue
while(!queue.isEmpty()) {
System.out.println(queue.poll());
}
}
}
// Stack simulation queue
class MyQueue<E>{
private Stack<E> in = new Stack<E>();
private Stack<E> out = new Stack<E>();
// The team
public void offer(E e) {
while(!out.isEmpty()) {
in.push(out.pop());
}
// Add a new element
in.push(e);
}
// Out of the team
public E poll() {
while(!in.isEmpty()) {
out.push(in.pop());
}
return out.pop();
}
// Determines if the queue is empty
public boolean isEmpty() {
if(in.size()==0 && out.size()==0) {
return true;
}
return false;
}
}
边栏推荐
- Network ()
- Open source management system based on ThinkPHP
- 现在开发人员都开始做测试了,是不是以后就没有软件测试人员了?
- AQS implementation principle
- Stm8 official library file download
- Speech at 2021 global machine learning conference
- NLP natural language processing - Introduction to machine learning and natural language processing (3)
- Polymorphism, final and interface
- utils 连接池
- DADNN: Multi-Scene CTR Prediction via Domain-Aware Deep Neural Network
猜你喜欢

NLP natural language processing - Introduction to machine learning and natural language processing (3)

Dynamic performance view overview

The analysis, solution and development of the problem of router dropping frequently

Interview question set

PXE efficient batch network installation

系统架构&微服务

基于Thinkphp的开源管理系统

DADNN: Multi-Scene CTR Prediction via Domain-Aware Deep Neural Network

JWT快速入门

Pycharm common shortcut keys
随机推荐
Idea shortcut key
20220209 create a basic Servlet
OVS底层实现原理
Machine learning related competition website
Stm8 official library file download
The analysis, solution and development of the problem of router dropping frequently
WCF deployed on IIS
Add traceid to the project log
PXE efficient batch network installation
Introduction to C language (8)
HOT100 hash
Establishment and use of openstack cloud platform
Use of views
Meta universe infrastructure: analysis of the advantages of Web 3.0 chain33
Regression analysis code implementation
Tensorflow learning diary tflearn
深度学习模型部署
ShardingSphere数据分片
Unity Metaverse(二)、Mixamo & Animator 混合树与动画融合
How to ensure the double write consistency between cache and database?