当前位置:网站首页>【栈和队列专题】—— 双队列模拟栈
【栈和队列专题】—— 双队列模拟栈
2022-04-21 13:37:00 【ReStart@XiaoZhao】
LeetCode 225 : 用队列实现栈
队列
1.一些关于队列的基本操作
- 向队列中添加元素[3种方法]
queue.add(value)
queue.offer(value)
queue.put(value)
- 删除队首元素
queue.remove()
queue.poll()
queue.take()
- 查看队首元素
queue.element()
queue.peek()
- 判断队列是否为空
queue.isEmpty()
2.队列的实现
(1)可以通过链表来实现队列
(2)可以通过Java中的LinkedList来实现队列
(3)也可以通过栈来模拟队列
队列实现栈

️ 解题思路:
(1)用队列模拟栈最重要的就是来完成元素顺序的控制,队列中怎样的元素存储顺序才能和栈相同呢?
(2)栈的主要特点就是后入先出,队列只能通过让实际后入的元素先存储到队列中,这样就满足了后入先出的性质,那么如何才能让后入的元素来先保存到队列中呢?
(3)设置两个队列,一个主队列、一个辅助队列。将新添加的元素保存到辅助队列之中,然后让主队列中的元素全部取出保存到辅助队列中,最后再交换两个队列。这样就保证了实际后入的元素先存储到了队列中。
️ 代码部分:
class MyStack {
/* 通过两个队列来模拟栈的操作,重点元素如何保证在队列中存储元素的顺序 与和栈中存储元素的顺序构成逆序关系 */
//定义两个队列
Queue<Integer> queue1;
Queue<Integer> queue2;
public MyStack() {
/*因为通过队列来模拟栈,所以构造方法中主要是完成两个队列的创建 */
queue1 = new LinkedList<>();
queue2 = new LinkedList<>();
}
public void push(int x) {
queue2.add(x);
//将主队列中的元素取出放到辅助队列中
while(!queue1.isEmpty()){
queue2.add(queue1.remove());
}
//交换两个队列
Queue temp = queue1;
queue1 = queue2;
queue2 = temp;
}
public int pop() {
/*对于队列中元素的保存顺序和栈是相同的,所以移除栈顶元素就是 删除队首元素 */
return queue1.remove();
}
public int top() {
/*与取出队首元素相同 */
return queue1.element();
}
public boolean empty() {
/*判断栈是否为空也是判断队列是否为空 */
return queue1.isEmpty();
}
}
/** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * boolean param_4 = obj.empty(); */
版权声明
本文为[ReStart@XiaoZhao]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_61323055/article/details/124315814
边栏推荐
- Review questions and answers of architectural physics and equipment for class I registered architect examination in 2022
- 运动耳机什么样的舒服、运动健身耳机推荐
- 【数字信号处理】相关系数 ( 相关系数概念 | 能量信号与功率信号 | 系统的因果性 )
- nmap使用
- 完成数亿元融资后,毫末智行计划超百城落地城市智能驾驶产品
- Mysql 浅析行锁如何减少冲突提高性能
- 万字干货!帮你深度掌握设计中的「光影」知识点
- News compendium of foreign Internet products last week - Angel
- 如何在Centos下卸载OpenJDK,安装Oracle JDK
- After the completion of hundreds of millions of yuan of financing, smart bank plans to land urban intelligent driving products in more than 100 cities
猜你喜欢

Exercise questions and answers of quality, investment and progress control in 2022 supervision engineer examination

万字干货!帮你深度掌握设计中的「光影」知识点 (下)

Installing and configuring canal

Achieve random labels, font size, color random display

Leetcode: countless denominations of coins get the option of amount (DP)

metasploit渗透

【csnote】db异常(冗余数据、修改异常、删除异常、插入异常)

Go language file operation

Stm32cupemx installation

万字干货!帮你深度掌握设计中的「光影」知识点
随机推荐
What are the futures varieties of agricultural products?
Mysql 驱动为什么要依赖 protobuf
Internet of things development practice 06 things model: how to define intelligent lights? (study notes)
一级等保怎么做?要收费吗?等保要求是什么?
Go language file operation
暴力匹配阈值的基准细胞检测方案
20210818 diary
idea自动生成单元测类
Accounting practice exercises and answers for the 2022 primary accounting title examination
MySQL analysis on how to reduce conflict and improve performance of row lock
网易数帆王佰平:我的 Envoy Maintainer 之路
What about first-class insurance? Is there a charge? What are the waiting requirements?
Leetcode: countless denominations of coins get the option of amount (DP)
百度地图开发自定义信息窗口openInfoWindow样式
The course of qiniu business school allows Xiaobai to open an account to buy national debt reverse repurchase. Is it safe?
[digital signal processing] linear constant coefficient difference equation (determine whether the system is a "linear time invariant system" according to "linear constant coefficient difference equat
[traitement du signal numérique] coefficient de corrélation (analyse conceptuelle du coefficient de corrélation | constante d'énergie du signal | séquence conjuguée | corrélation de la séquence au mêm
Programmers burst out their salary, with a monthly salary of 15000 before tax
哈夫曼編碼
S: Unit gain compensation