使用队列实现栈(2)(Java)
class MyStack
{
private Queue q1;
private Queue q2; public MyStack(int size)
{
this.q1 = new Queue(size);
this.q2 = new Queue(size);
} public boolean isFull()
{
return q1.isFull();
} public boolean isEmpty()
{
return q1.isEmpty();
} //时间复杂度: O(1)
public void push(int k) throws Exception
{
if(this.q1.isFull())
throw new Exception("Overflow.");
else
this.q1.EnQueue(k);
} //时间复杂度: O(n)
public int pop() throws Exception
{
if(this.q1.isEmpty())
throw new Exception("Underflow.");
else
{
for(int i = this.q1.getLength(); i > 1; i--)
this.q2.EnQueue(this.q1.DeQueue());
int key = this.q1.DeQueue();
while(!this.q2.isEmpty())
this.q1.EnQueue(this.q2.DeQueue());
return key;
}
}
} class Queue
{
private int front;
private int rear;
private int[] a; public Queue(int size)
{
this.front = this.rear = 0;
this.a = new int[size];
} public boolean isFull()
{
return (this.rear + 1) % this.a.length == this.front;
} public boolean isEmpty()
{
return this.rear == this.front;
} public void EnQueue(int k) throws Exception
{
//该判断是冗余的
/*if(this.isFull())
*
throw new Exception("Overflow.");*/
//else
{
this.a[this.rear] = k;
this.rear = (this.rear + 1) % this.a.length;
} } public int DeQueue() throws Exception
{
//该判断是冗余的
/*if(this.isEmpty())
throw new Exception("Underflow.");*/
//else
{
int key = this.a[front];
this.front = (this.front + 1) % this.a.length;
return key;
}
} public int getLength()
{
return (this.rear - this.front + this.a.length) % this.a.length;
}
}
使用队列实现栈(2)(Java)的更多相关文章
- 剑指offer第二版面试题9:用两个队列实现栈(JAVA版)
题目:用两个队列实现栈. 分析:通过一系列的栈的压入和弹出操作来分析用队列模拟一个栈的过程,如图所示,我们先往栈内压入一个元素a.由于两个队列现在都是空,我们可以选择把a插入两个队列中的任一个.我们不 ...
- 使用队列实现栈(1)(Java)
class MyStack { private Queue q1; private Queue q2; public MyStack(int size) { this.q1 = new Queue(s ...
- 两个栈实现队列+两个队列实现栈----java
两个栈实现队列+两个队列实现栈----java 一.两个栈实现一个队列 思路:所有元素进stack1,然后所有出s ...
- 两个队列实现栈&两个栈实现队列(JAVA)
1,两个栈实现队列 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路:栈的特点时先进后出,队列的特点是先进先出. 若此时有两个队列stack1,st ...
- LeetCode--255--用队列实现栈(java版)
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...
- java 队列和栈相互实现
一.队列实现栈 public class queue2stack { public static void main(String[] args) { QS qs = new QS(); qs.pus ...
- Java实现 LeetCode 225 用队列实现栈
225. 用队列实现栈 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 注意: 你只能使 ...
- python优先队列,队列和栈
打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...
- 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...
随机推荐
- sklearn中的Pipeline
在将sklearn中的模型持久化时,使用sklearn.pipeline.Pipeline(steps, memory=None)将各个步骤串联起来可以很方便地保存模型. 例如,首先对数据进行了PCA ...
- 【最短路径Floyd算法详解推导过程】看完这篇,你还能不懂Floyd算法?还不会?
简介 Floyd-Warshall算法(Floyd-Warshall algorithm),是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法,与Dijkstra算法类似.该算法名称以 ...
- Python:用 peewee 框架连接 SQL Server
Peewee 默认支持 Sqlite.MySQL.PostgreSQL 三种数据库,如果要使用其他数据库,需要同时安装扩展库.比如 SQL Server,需要安装 peewee-mssql. 但是安装 ...
- C#——Nhibernate探索
C#—Nhibernate探索 本篇文章,让我们一起来探索Nhibernate. 首先我们去搜索Nhibernate下载地址,如下链接所示. 该版本可能是最新版,我下载的4.0.4.GA.其中GA意思 ...
- 推荐三个 VSCode 摸鱼插件
周三是一周中最难以度过的一天,离上个周末过去了两天,离下个周末也还有两天.为了让各位更好地搬(mo)砖(yu),今天给大家推荐三款效(mo)率(yu)工(shen)具(qi)! 一.听歌插件 1 功能 ...
- DS标签控件文本解析格式
DS标签控件使用DSL文本渲染引擎,支持DSL引擎代码.目前支持代码如下: <b>粗体</b> 以粗体显示 <i>斜体</i> 以斜体显示 <u& ...
- linq用法整理
linq用法整理 普通查询 var highScores = from student in students where student.ExamScores[exam] > score se ...
- revit融合
解决了嵌入部分也会布置砖胎膜或土方问题 1.需根据板往相应方向拉伸,创建拉伸体(非实例) 2.根据轮廓创建融合体 3.将两个物体融合 //创建平面 //创建草图平面,文档必须是族文档 Plane pl ...
- 关于jQuery中的选择器
1:选择器的作用 获取网页的上面的标签元素等等,然后对他进行一些列的操作(添加样式,添加行为...) 2:选择器有哪些 基本选择器,层次选择器,过滤选择器,表单选择器 一:基本选择器 基本选择器是jq ...
- Kafka学习资料
博客系列: Apache Kafka简介Apache Kafka安装和使用Apache Kafka核心概念kafka核心组件和流程—控制器kafka核心组件和流程—协调器kafka核心组件和流程—日志 ...