当前位置:网站首页>Double queue implementation stack?Dual stack implementation queue?
Double queue implementation stack?Dual stack implementation queue?
2022-08-02 00:15:00 【Chen Yikang】
双队列实现栈
A single queue cannot implement a stack,Double queue is ok,如下分析(例如出栈)
注意:
- Which queue is not empty put the element into which queue
- 若两个都为空,You can put it in any queue
如下代码:
class MyStack {
public Queue<Integer> q1;
public Queue<Integer> q2;
public int usedSize;
public MyStack() {
q1 = new LinkedList<>();
q2 = new LinkedList<>();
}
//压栈
public void push(int x) {
//哪个队列不为空,Put it in there,Leave it all emptyq1
if(!q1.isEmpty()){
q1.offer(x);
}
else if(!q2.isEmpty()){
q2.offer(x);
}
else{
q1.offer(x);
}
usedSize++;
}
//出栈
public int pop() {
//哪个队列不为空,Just take the element from there and put it in another queue
if(empty()){
return -1;
}
if(!q1.isEmpty()){
//转移usedSize - 1个元素到另一个队列
//for(int i = 0; i < usedSize - 1; i++)//不能这样写,usedSizeIt will continue to decrease as the stack is popped
int size = usedSize;
for(int i = 0; i < size - 1; i++){
q2.offer(q1.poll());
}
usedSize--;
return q1.poll();
}
else{
int size = usedSize;
for(int i = 0; i < size - 1; i++){
q1.offer(q2.poll());
}
usedSize--;
return q2.poll();
}
}
public int top() {
//哪个队列不为空,Just take the element from there and put it in another queue
if(empty()){
return -1;
}
int tmp = 0;
if(!q1.isEmpty()){
int size = usedSize;
for(int i = 0; i < size - 1; i++){
q2.offer(q1.poll());
}
tmp = q1.peek();
q2.offer(q1.poll());
}
else{
int size = usedSize;
for(int i = 0; i < size - 1; i++){
q1.offer(q2.poll());
}
tmp = q2.peek();
q1.offer(q2.poll());
}
return tmp;
}
//判空
public boolean empty() {
return usedSize == 0;
}
}
双栈实现队列
A single stack cannot implement a queue,Dual stack is possible,如下分析(例如出队)
注意:
- Be sure to check before leaving the teams2中是否有元素,If there is, it can pop up directly,If not agains1全部压入s2中
- The same goes for checking the head of the queue(peek)
代码如下:
class MyQueue {
public Stack<Integer> s1;//入栈
public Stack<Integer> s2;//出栈
public MyQueue() {
this.s1 = new Stack<>();
this.s2 = new Stack<>();
}
//进队
public void push(int x) {
s1.push(x);
}
//出队
public int pop() {
if(empty()) {
return -1;
}
//s2like a cistern,Once out of the queue,full pressures1
//前提一定要是s2为空!若不为空,再压入s2,The order is different!
if(s2.empty()){
while(!s1.empty()) {
s2.push(s1.pop());
}
return s2.pop();
}
//如果s2里有元素,pop up directly
return s2.pop();
}
public int peek() {
if(empty()) {
return -1;
}
//s2like a cistern,Once out of the queue,full pressures1
//前提一定要是s2为空!若不为空,再压入s2,The order is different!
if(s2.empty()){
while(!s1.empty()) {
s2.push(s1.pop());
}
return s2.peek();
}
//如果s2里有元素,pop up directly
return s2.peek();
}
public boolean empty() {
return s1.empty() && s2.empty();
}
}
边栏推荐
- 认识USB、Type-C、闪电、雷电接口
- 信息系统项目管理师必背核心考点(五十七)知识管理工具
- Using the "stack" fast computing -- reverse polish expression
- EasyExcel的简单读取操作
- PHP从txt文件中读取数据的方法
- 检查 Oracle 版本的 7 种方法
- yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
- 使用Ganache、web3.js和remix在私有链上部署并调用合约
- 一文概览最实用的 DeFi 工具
- background-image使用
猜你喜欢
Collection of NFT tools
security CSRF漏洞保护
WEB安全基础 - - - XRAY使用
Short video seo search optimization main content
使用 Zadig 交付云原生微服务应用
使用Ganache、web3.js和remix在私有链上部署并调用合约
SphereEx苗立尧:云原生架构下的Database Mesh研发实践
Short video SEO search operation customer acquisition system function introduction
DOM 基础操作
回顾历史5次经济衰退时期:这一次可能会有何不同?
随机推荐
【Leetcode】478. Generate Random Point in a Circle(配数学证明)
【MySQL系列】MySQL索引事务
Excel表格数据导入MySQL数据库
OpenCV DNN blogFromImage() detailed explanation
How to reinstall Win11?One-click method to reinstall Win11
security 会话并发管理
当奈飞的NFT忘记了Web2的业务安全
Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers
JSP如何使用page指令让JSP文件支持中文编码呢?
asyncawait和promise的区别
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
security session concurrency management
【Leetcode】475. Heaters
[头条]笔试题——最小栈
在MySQL中使用MD5加密【入门体验】
Study Notes: The Return of Machine Learning
Arduino 基础语法
Deliver cloud-native microservices applications with Zadig
PHP从txt文件中读取数据的方法
利用“栈”快速计算——逆波兰表达式