当前位置:网站首页>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;
}
}
边栏推荐
- 2022.7.22DAY612
- OVS underlying implementation principle
- Logical volume management (LVM)
- MMOE multi-objective modeling
- ARIMA model for time series analysis and prediction
- Practice of online question feedback module (XIV): realize online question answering function
- Wrong Addition
- Audio and video learning (10) -- PS streaming
- Distributed system and distributed database system (Introduction)
- Program environment and pretreatment
猜你喜欢

《门锁》引爆独居安全热议 全新海报画面令人窒息

Summary of distributed related interview questions

动态性能视图概述

JWT quick start

How to ensure the double write consistency between cache and database?

时间序列分析预测实战之ARIMA模型

Installation of Baidu flying paste deep learning framework tutorial in Anaconda

【uniapp】多种支付方式封装

利用js实现统计字符串数组中各字符串出现的次数,并将其格式化为对象数组。

Jmeter性能测试之命令行执行和生成测试报告
随机推荐
ARIMA model for time series analysis and prediction
系统架构&微服务
Comparison and difference between dependence and Association
System architecture & microservices
Table fix specific rows
Basic knowledge of convolutional neural network
Yaml language-01 (data type, array, object)
Command line execution and test report generation of JMeter performance test
OVS underlying implementation principle
OVS底层实现原理
MMOE多目标建模
Sort: merge sort and quick sort
2019中兴捧月·模型压缩方案
Unity metaverse (II), mixamo & animator hybrid tree and animation fusion
机器学习相关比赛网站
FTP service
Stm8 official library file download
JWT quick start
Libevent custom event (asynchronous)
Practice of online question feedback module (XIV): realize online question answering function