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. vue学习笔记2

    Vue.js - Day2 品牌管理案例 添加新品牌 删除品牌 根据条件筛选品牌 1.x 版本中的filterBy指令,在2.x中已经被废除: filterBy - 指令 <tr v-for=& ...

  2. .NET Core 性能分析: xUnit.Performance 简介

    xunit-performance 是xUnit的一个扩展, 使用它可以对.NET Core项目进行性能测试. 官网:https://github.com/Microsoft/xunit-perfor ...

  3. Docker 搜索镜像

    文章首发个人网站: https://www.exception.site/docker/docker-search-image 本文中,您将学习 Docker 如何搜索镜像? 一.search 命令 ...

  4. Koa 中的错误处理

    不像 express 中在末尾处注册一个声明为 (err, req, res, next) 中间件的方式,koa 刚好相反,在开头进行注册. app.use(async (ctx, next) =&g ...

  5. 知识小罐头05(tomcat8请求源码分析 上)

    这一篇我们不看源码,就大概理一下Tomcat内部组成部分!前面花费了两篇博客的篇幅来说说了一般的maven web项目并部署到tomcat运行,其实都是为这篇做铺垫的! 其实我下载了tomcat7,t ...

  6. rabbitmq实现向各服务广播消息

    广播fanout 主要是将一个消息,分发到绑定了它的队列上,而这些队列如消费者自己去建立和绑定! 对生产者是解耦的 生产者不需要关心消费者有多少,消费者如果需要这种消息,只需要把队列绑定到exchan ...

  7. Wolsey "强整数规划“ 建模的+Leapms实践——无产能批量问题

    Wolsey "强整数规划“ 建模的+Leapms实践——无产能批量问题 <整数规划>[1]一书作者L. A. Wolsey对批量问题(Lot-sizing Problem)做了 ...

  8. Asp.Net Core 轻松学-利用日志监视进行服务遥测

    前言     在 Net Core 2.2 中,官方文档表示,对 EventListener 这个日志监视类的内容进行了扩充,同时赋予了跟踪 CoreCLR 事件的权限:通过跟踪 CoreCLR 事件 ...

  9. (转)使用JMeter进行Web压力测试

    使用JMeter进行压力测试 说到压力测试,一般第一反应都是LoadRunner.这个软件也确实是自动化测试的一个事实标准.无奈这个软件太过庞大,以及不能在MacOS上使用.我由于项目的需要,需要对一 ...

  10. Java中实现多线程的四种方式

    Java多线程实现方式主要有四种:继承Thread类.实现Runnable接口.实现Callable接口通过FutureTask包装器来创建Thread线程.使用ExecutorService.Cal ...