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. Python后台开发Django( 模板 与 值匹配 )

    模板文件(templates) 在setting.py中,设置模板存放位置 在APP中view的使用 from django.shortcuts import render #导入 def homex ...

  2. Kubernetes 笔记 08 Deployment 副本管理 重新招一个员工来填坑

    本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. Hi,大家好, ...

  3. java配置环境及安装

    1.安装JDK开发环境 首先登陆https://www.oracle.com 第一步:Products ---- Java ---- Download java(JDK) for Developers ...

  4. java~接口的共享实体使用Map后更灵活

    微服务时代的实体设计 在一个微服务时代,一个实体参数或者返回值,它可能是多服务之前共享的,而这个重复的实体你需要拷贝多份,这是违背DRP原则的,所以我们需要找一种更友好的方式来代替它,它就是Map,我 ...

  5. 认识JWT

    1. JSON Web Token是什么 JSON Web Token (JWT)是一个开放标准(RFC 7519),它定义了一种紧凑的.自包含的方式,用于作为JSON对象在各方之间安全地传输信息.该 ...

  6. javascript 倒计数功能

    最近在项目中遇到一个倒计时功能,在网上没有找到合适的,就自己写了个方法.贴在这里,权且当个记录. export const timeRun = (timeStr, callBack) => { ...

  7. ASP.NET Core 基于JWT的认证(一)

    ASP.NET Core 基于JWT的认证(一) Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计 ...

  8. git clone 指定分支

    使用Git下载指定分支命令为:git clone -b 分支名仓库地址 克隆asp.net core 2.1.6版本 git clone -b 2.1.6 https://github.com/asp ...

  9. 获取url特定参数

    获取通过url拼接的特定参数值: // 获取url指定参数 function getUrlParams(name) { var reg = new RegExp("(^|&)&quo ...

  10. ArcPy 创建图层空间索引

    使用Python脚本进行图层的空间索引的创建. 附上Python代码: # -*- coding: utf-8 -*- # nightroad import sys import arcpy relo ...