当前位置:网站首页>The idea of stack simulating queue
The idea of stack simulating queue
2022-07-26 07:55:00 【Why can't I eat mango】
The characteristic of the stack : Last in, first out
The characteristics of the queue are : fifo
Stack simulation queue , It requires elements to be first in, first out , But the characteristic of stack is last in first out , This requires two stacks to simulate queues ( Accord with the characteristic of first in first out )
1. First create two stacks ( Stack 1:in, Stack 2:out)
2. According to the characteristics of queue first in first out : Stack 1 Into the , Stack 2 Out
When you join the team , According to the characteristics of stack LIFO : Just ask for stack 2 You can only join the team when it is empty ; Otherwise, it will stack 2 Elements in stack 1,in.push(out.pop()), Until the stack 2 It's empty ( All on the stack 1 in ) To join the team in.push()
Empathy , When the team , Request stack 1 It's empty time , Before leaving the team ; Otherwise, it will stack 1 The elements in Push 2 out.push(in.pop()), Until the stack 1 It's empty ( All on the stack 2 Only in the middle can you leave the team ),out.pop( )

Code implementation :
package com.my.stack;
import java.util.Stack;
public class Test {
public static void main(String[] args) {
MyQueue<String> queue = new MyQueue<String>();
queue.offer("A1");
queue.offer("A2");
queue.offer("A3");
queue.offer("A4");
System.out.println(queue.poll());
// Traverse the queue
while(!queue.isEmpty()) {
System.out.println(queue.poll());
}
}
}
// Stack simulation queue
class MyQueue<E>{
private Stack<E> in = new Stack<E>(); // Join the team
private Stack<E> out = new Stack<E>(); // Out of the stack
// Determines if the queue is empty
public boolean isEmpty() {
return in.size()==0 && out.size()==0;
}
// 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();
}
}边栏推荐
- Shardingjdbc pit record
- The difference between ArrayList and LinkedList
- No valid host was found when setting up openstack to create an instance There are not enough hosts available. code:500
- How to close the high-level port
- ShardingSphere数据分片
- Program environment and pretreatment
- How to ensure the double write consistency between cache and database?
- Add traceid to the project log
- Installation of Baidu flying paste deep learning framework tutorial in Anaconda
- Ethernet switching security
猜你喜欢

Devaxpress.xtraeditors.datanavigator usage

PyTorch

以太网交换安全

爬虫->TpImgspider

Selenium: detailed explanation of browser crawler use (I)

Regression analysis code implementation

【每日一题】919. 完全二叉树插入器

基于Thinkphp的开源管理系统

Fang Wenshan, Jay Chou's best partner, will officially announce "Hualiu yuancosmos" on July 25

Web page basic label
随机推荐
Add traceid to the project log
Wrong Addition
动态性能视图概述
Regular expression rules and common regular expressions
Unity metaverse (II), mixamo & animator hybrid tree and animation fusion
Vscode cannot start the problem solving idea
Now developers are beginning to do testing. Will there be no software testers in the future?
Leetcode 206. reverse chain list (2022.07.25)
DADNN: Multi-Scene CTR Prediction via Domain-Aware Deep Neural Network
Program environment and pretreatment
如何关闭高位端口
OVSDB
Rack server expansion memory
Use js to count the number of occurrences of each string in the string array, and format it into an object array.
Anaconda 中安装 百度飞浆Paddle 深度学习框架 教程
《门锁》引爆独居安全热议 全新海报画面令人窒息
The difference between overloading and rewriting
元宇宙基础设施:WEB 3.0 chain33 优势分析
Enterprise private network construction and operation and maintenance
Stm8 official library file download