当前位置:网站首页>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;
}
}
边栏推荐
- 「论文笔记」Next-item Recommendations in Short Sessions
- 时间序列分析预测实战之ARIMA模型
- Anaconda 中安装 百度飞浆Paddle 深度学习框架 教程
- A tutorial for mastering MySQL database audit characteristics, implementation scheme and audit plug-in deployment
- 在线问题反馈模块实战(十四):实现在线答疑功能
- Installation of Baidu flying paste deep learning framework tutorial in Anaconda
- LeetCode剑指offer专项(一)整数
- OVS underlying implementation principle
- The analysis, solution and development of the problem of router dropping frequently
- 现在开发人员都开始做测试了,是不是以后就没有软件测试人员了?
猜你喜欢

What is bloom filter in redis series?

在线问题反馈模块实战(十四):实现在线答疑功能

Network ()

Basic knowledge of convolutional neural network

ARIMA model for time series analysis and prediction

MMOE多目标建模

Program environment and pretreatment

“尝鲜”元宇宙,周杰伦最佳拍档方文山将于7月25日官宣《华流元宇宙》

Anaconda 中安装 百度飞浆Paddle 深度学习框架 教程

MMOE multi-objective modeling
随机推荐
Use of views
Network trimming: a data driven neuron pruning approach towards efficient deep architectures paper translation / Notes
[uniapp] encapsulation of multiple payment methods
1. MySQL Architecture [MySQL advanced]
Enterprise private network construction and operation and maintenance
Tensorflow learning diary tflearn
Web page basic label
Audio and video learning (10) -- PS streaming
Use js to count the number of occurrences of each string in the string array, and format it into an object array.
DCN (deep cross network) Trilogy
Comparison and difference between dependence and Association
0 dynamic programming leetcode1567. Length of the longest subarray with positive product
Database foundation
Machine learning related competition website
IDEA settings设置快捷键实现字符串中的英文字母转大小写
Abnormal (2)
[daily question 1] 919. Complete binary tree inserter
How to close the high-level port
Como automatic test system: build process record
Pycharm common shortcut keys