当前位置:网站首页>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();
}
}
边栏推荐
- Short video SEO optimization tutorial Self-media SEO optimization skills and methods
- LeetCode_322_零钱兑换
- Axure教程-新手入门基础(小白强烈推荐!!!)
- 06-SDRAM :SDRAM控制模块
- 【图像融合】基于加权和金字塔实现图像融合附matlab代码
- Short video seo search optimization main content
- DOM 基础操作
- 玩转NFT夏季:这份工具宝典值得收藏
- 微软电脑管家V2.1公测版正式发布
- 【Leetcode】470. Implement Rand10() Using Rand7()
猜你喜欢

20220725 Information update

Deliver cloud-native microservices applications with Zadig

TCP 可靠吗?为什么?

Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)

Thymeleaf简介

零基础如何学习单片机,一位入门者的进阶路径,可参考

Ansible中的任务执行控制

带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?

链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
![[Headline] Written test questions - minimum stack](/img/67/08f2be8afc780e3848371a1b5e04db.png)
[Headline] Written test questions - minimum stack
随机推荐
C语言七夕来袭!是时候展现专属于程序员的浪漫了!
els 方块变形
如何设计循环队列?快进来学习~
contentEditable属性
security CSRF漏洞保护
多御安全浏览器android版更新至1.7,改进加密协议
【MySQL系列】MySQL数据库基础
How to reinstall Win11?One-click method to reinstall Win11
【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件
一个有些意思的项目--文件夹对比工具(一)
Bean的生命周期
Docker搭建Mysql主从复制
好好活就是做有意义的事,有意义的事就是好好活
【Leetcode】479. Largest Palindrome Product
An interesting project--Folder comparison tool (1)
[头条]笔试题——最小栈
面试必问的HashCode技术内幕
众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
JSP out.print()和out.write()方法的不同之处
如何发现新的潜力项目?工具推荐