使用队列实现栈(1)(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(n)
- public void push(int k) throws Exception
- {
- if(this.isFull())
- throw new Exception("Overflow.");
- else
- {
- while(!this.q1.isEmpty())
- this.q2.EnQueue(this.q1.DeQueue());
- this.q1.EnQueue(k);
- while(!this.q2.isEmpty())
- this.q1.EnQueue(this.q2.DeQueue());
- }
- }
- //时间复杂度: O(1)
- public int pop() throws Exception
- {
- if(this.isEmpty())
- throw new Exception("Underflow");
- else
- return this.q1.DeQueue();
- }
- }
- 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;
- }
- }
使用队列实现栈(1)(Java)的更多相关文章
- 剑指offer第二版面试题9:用两个队列实现栈(JAVA版)
题目:用两个队列实现栈. 分析:通过一系列的栈的压入和弹出操作来分析用队列模拟一个栈的过程,如图所示,我们先往栈内压入一个元素a.由于两个队列现在都是空,我们可以选择把a插入两个队列中的任一个.我们不 ...
- 使用队列实现栈(2)(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 实现 解法二:双队列,入慢出快 思路 入栈 ...
随机推荐
- JavaSE:八种基本数据类型
变量: 程序用来存储数据的一块内存空间,程序在运行过程中可以对其存储的数据进行改变,所以叫做变量 常量:相对于变量来说,其值是不可改变的 整数类型(byte short int long) b ...
- 一行一行手敲webpack4配置
代码:github 一.webpack4--基本配置 这一部分通过webpack的基本配置,使用loader对图片和样式进行打包,从而了解webpack4简单的用方法,保证自己能够配置正确,提升学习动 ...
- ShareIntentUtil【调用系统自带的分享的工具类】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 根据参考资料的文章,整理了调用系统自带分享的工具类(实现了适配7.0FileProvider的功能),需要搭配<Android ...
- springboot~rabbitmq自己通过UI手动发布队列需要注意的地方
springboot里发布队列消息 为了兼容性和可读性更好,我们一般使用json字符串做为数据载体. public void decreaseCallMonitor(CallMonitorInfo c ...
- confd+etcd实现高可用自动发现
Confd是什么 Confd是一个轻量级的配置管理工具. 通过查询后端存储,结合配置模板引擎,保持本地配置最新,同时具备定期探测机制,配置变更自动reload. 对应的后端存储可以是etcd,redi ...
- 简易调色盘控件 for .NET(EN)
By Conmajia Originally posted in 2012 Introduction Simple & fast implementation of a rectangular ...
- 推荐三个 VSCode 摸鱼插件
周三是一周中最难以度过的一天,离上个周末过去了两天,离下个周末也还有两天.为了让各位更好地搬(mo)砖(yu),今天给大家推荐三款效(mo)率(yu)工(shen)具(qi)! 一.听歌插件 1 功能 ...
- JMeter主要组件介绍
JMeter主要组件介绍 转自https://www.cnblogs.com/linbo3168/p/6023962.html 作者:linbo.yang 1.测试计划(Test Plan)是使用 ...
- C#属性标记Order执行顺序备忘录
部分Attribute有实现IOrderedFilter,其执行顺序机制例子: /// <summary> /// 密码修改 /// </summary> /// <pa ...
- WPF 自定义 ImageButton
控件源码: public class ImageButton : Button { public ImageButton() { } public string No ...