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)的更多相关文章

  1. 剑指offer第二版面试题9:用两个队列实现栈(JAVA版)

    题目:用两个队列实现栈. 分析:通过一系列的栈的压入和弹出操作来分析用队列模拟一个栈的过程,如图所示,我们先往栈内压入一个元素a.由于两个队列现在都是空,我们可以选择把a插入两个队列中的任一个.我们不 ...

  2. 使用队列实现栈(1)(Java)

    class MyStack { private Queue q1; private Queue q2; public MyStack(int size) { this.q1 = new Queue(s ...

  3. 两个栈实现队列+两个队列实现栈----java

                                               两个栈实现队列+两个队列实现栈----java 一.两个栈实现一个队列 思路:所有元素进stack1,然后所有出s ...

  4. 两个队列实现栈&两个栈实现队列(JAVA)

    1,两个栈实现队列 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路:栈的特点时先进后出,队列的特点是先进先出. 若此时有两个队列stack1,st ...

  5. LeetCode--255--用队列实现栈(java版)

    使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...

  6. java 队列和栈相互实现

    一.队列实现栈 public class queue2stack { public static void main(String[] args) { QS qs = new QS(); qs.pus ...

  7. Java实现 LeetCode 225 用队列实现栈

    225. 用队列实现栈 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 注意: 你只能使 ...

  8. python优先队列,队列和栈

    打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...

  9. 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)

    目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...

随机推荐

  1. DotNetCore跨平台~EFCore2.0连接Mysql的烦恼-已解决

    回到目录 对于传统的nuget包,我们习惯上用官方或者大型组织的,因为它们比较考靠谱,但就在前两天.net core2.0发布后,我把efcore也升级到2.0了,mysql.EfCore也升级到支持 ...

  2. 跟王思聪热狗图一样大热的Redis,还不赶紧来Get一下?

    前言 不禁喊出一句ig牛逼!哈哈哈 这个话题是不是有点过时了?但说到Redis,真的是被强行灌输的,到处都会被安利Redis,吓得只会mysql和oracle的我,赶紧去get一波.. 数据库种类 关 ...

  3. 目标检测 IOU(交并比) 理解笔记

    交并比(Intersection-over-Union,IoU): 目标检测中使用的一个概念 是产生的候选框(candidate bound)与原标记框(ground truth bound)的交叠率 ...

  4. .Net Core 根据配置文件动态发布至服务器

    前言 一个软件的开发周期需要经历开发.测试.上线三个基本的阶段,同理我们在开发过程中会需要经常切换不同的运行环境..NetCore可以通过配置文件以及写入系统环境变量来自动识别站点的运行环境,保证了数 ...

  5. 贝塞尔曲线控件 for .NET (EN)

    Conmajia 2012 Updated on Feb. 18, 2018 In Photoshop, there is a very powerful feature Curve Adjust, ...

  6. C#工具:Ado.Net SqlServer数据库 MySql数据库

    数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库. SqlServer调用数据库 using System; using System.Coll ...

  7. java中求质数(素数)的问题

    这篇笔记讲讲关于java中质数的问题. 一.什么是质数(素数)? 定义:质数又称素数.一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数:否则称为合数.它可以有无限个数. 二.ja ...

  8. Ext.isNumber与Ext.isNumeric

    Ext.isNumber: Ext.isNumber(1) true Ext.isNumber(new Number(1)) false Ext.isNumber("1") fal ...

  9. java 线程方法 ---- sleep()

    class MyThread implements Runnable{ @Override public void run() { for (int i = 0; i < 5; i++){ Sy ...

  10. 【English】五、颜色相关

    一.常见颜色 黑色    black    白色    white    蓝色    blue    橙色    orange    黄色    yellow        灰色    gray   ...