1. class MyStack
  2. {
  3. private Queue q1;
  4. private Queue q2;
  5.  
  6. public MyStack(int size)
  7. {
  8. this.q1 = new Queue(size);
  9. this.q2 = new Queue(size);
  10. }
  11.  
  12. public boolean isFull()
  13. {
  14. return q1.isFull();
  15. }
  16.  
  17. public boolean isEmpty()
  18. {
  19. return q1.isEmpty();
  20. }
  21.  
  22. //时间复杂度: O(n)
  23. public void push(int k) throws Exception
  24. {
  25. if(this.isFull())
  26. throw new Exception("Overflow.");
  27. else
  28. {
  29. while(!this.q1.isEmpty())
  30. this.q2.EnQueue(this.q1.DeQueue());
  31. this.q1.EnQueue(k);
  32. while(!this.q2.isEmpty())
  33. this.q1.EnQueue(this.q2.DeQueue());
  34. }
  35. }
  36.  
  37. //时间复杂度: O(1)
  38. public int pop() throws Exception
  39. {
  40. if(this.isEmpty())
  41. throw new Exception("Underflow");
  42. else
  43. return this.q1.DeQueue();
  44. }
  45. }
  46.  
  47. class Queue
  48. {
  49. private int front;
  50. private int rear;
  51. private int[] a;
  52.  
  53. public Queue(int size)
  54. {
  55. this.front = this.rear = 0;
  56. this.a = new int[size];
  57. }
  58.  
  59. public boolean isFull()
  60. {
  61. return (this.rear + 1) % this.a.length == this.front;
  62. }
  63.  
  64. public boolean isEmpty()
  65. {
  66. return this.rear == this.front;
  67. }
  68.  
  69. public void EnQueue(int k) throws Exception
  70. {
  71. //该判断是冗余的
  72. /*if(this.isFull())
  73. *
  74. throw new Exception("Overflow.");*/
  75. //else
  76. {
  77. this.a[this.rear] = k;
  78. this.rear = (this.rear + 1) % this.a.length;
  79. }
  80.  
  81. }
  82.  
  83. public int DeQueue() throws Exception
  84. {
  85. //该判断是冗余的
  86. /*if(this.isEmpty())
  87. throw new Exception("Underflow.");*/
  88. //else
  89. {
  90. int key = this.a[front];
  91. this.front = (this.front + 1) % this.a.length;
  92. return key;
  93. }
  94. }
  95.  
  96. public int getLength()
  97. {
  98. return (this.rear - this.front + this.a.length) % this.a.length;
  99. }
  100. }

使用队列实现栈(1)(Java)的更多相关文章

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

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

  2. 使用队列实现栈(2)(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. JavaSE:八种基本数据类型

    变量: 程序用来存储数据的一块内存空间,程序在运行过程中可以对其存储的数据进行改变,所以叫做变量 常量:相对于变量来说,其值是不可改变的 ​ 整数类型(byte short int long) ​ b ...

  2. 一行一行手敲webpack4配置

    代码:github 一.webpack4--基本配置 这一部分通过webpack的基本配置,使用loader对图片和样式进行打包,从而了解webpack4简单的用方法,保证自己能够配置正确,提升学习动 ...

  3. ShareIntentUtil【调用系统自带的分享的工具类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 根据参考资料的文章,整理了调用系统自带分享的工具类(实现了适配7.0FileProvider的功能),需要搭配<Android ...

  4. springboot~rabbitmq自己通过UI手动发布队列需要注意的地方

    springboot里发布队列消息 为了兼容性和可读性更好,我们一般使用json字符串做为数据载体. public void decreaseCallMonitor(CallMonitorInfo c ...

  5. confd+etcd实现高可用自动发现

    Confd是什么 Confd是一个轻量级的配置管理工具. 通过查询后端存储,结合配置模板引擎,保持本地配置最新,同时具备定期探测机制,配置变更自动reload. 对应的后端存储可以是etcd,redi ...

  6. 简易调色盘控件 for .NET(EN)

    By Conmajia Originally posted in 2012 Introduction Simple & fast implementation of a rectangular ...

  7. 推荐三个 VSCode 摸鱼插件

    周三是一周中最难以度过的一天,离上个周末过去了两天,离下个周末也还有两天.为了让各位更好地搬(mo)砖(yu),今天给大家推荐三款效(mo)率(yu)工(shen)具(qi)! 一.听歌插件 1 功能 ...

  8. JMeter主要组件介绍

    JMeter主要组件介绍   转自https://www.cnblogs.com/linbo3168/p/6023962.html 作者:linbo.yang 1.测试计划(Test Plan)是使用 ...

  9. C#属性标记Order执行顺序备忘录

    部分Attribute有实现IOrderedFilter,其执行顺序机制例子: /// <summary> /// 密码修改 /// </summary> /// <pa ...

  10. WPF 自定义 ImageButton

    控件源码: public class ImageButton : Button    {        public ImageButton() {        } public string No ...