使用队列实现栈(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 实现 解法二:双队列,入慢出快 思路 入栈 ...
随机推荐
- django框架使用mysql步骤
在创建好django项目的基础上来讲解使用orm框架 注意:首先在mysql中手动或者通过命令创建一个数据库,我先创建一个名为orm的数据库. 1:在项目文件夹中的settings.py文件中配置my ...
- 第1章 程序设计和C语言
1.1什么是计算机程序 程序,就是一组计算机能识别和执行的指令.每一条指令使计算机执行特定的操作.只要让计算机执行这个程序,计算机就会“自动地”执行各条指令,有条不紊地进行工作. 1.2什么是计算机语 ...
- 记录DEV gridview获取行列数据方法
DataRow dr = this.gridView1.GetDataRow(this.gridView1.FocusedRowHandle);//获取选中行 string str = gridVie ...
- 实现一个简单的基于Token的身份认证
这个例子是基于客户端与webapi进行进行交互的身份认证,当然也适用于其他情况下的身份认证. 简单的交互过程: 1.首先输入用户名.密码进行登录操作 2.服务器验证用户名.密码的正确性,验证通过之 ...
- RDIFramework.NET V3.3 Web版新增日程管理功能模块
功能描述 在RDIFramework.NET V3.3 Web版本我们新增了日程管理.基于月.周.日的日历视图,把安排到每一天的具体时间点,让每一天的时间都充分利用:甚至您也可以把个人非工作事项也安排 ...
- 字符型液晶屏模拟控件(En)
A replica CLCD module control. Initiated on May 5, 2012 Updated on Feb 21, 2017 Copyright 2012-2017 ...
- int16, int32, int64等类型说明
Int16 相当于 short 占2个字节 -32768 ~ 32767 Int32 相当于 int 占4个字节 -2147483648 ~ 2147483647 Int64 ...
- 纯CSS打造淘宝导航菜单栏
店铺装修-PC端-基础页-首页-装修页面:编辑“菜单”模块-显示设置,粘贴如下CSS: /* 导航条背景色*/ .skin-box-bd .menu-list{background: none rep ...
- chrome主页被篡改为hao123 win10系统
应该是开了个从流氓网站下的蓝灯,然后发现主页被篡改 尝试chrome设置修改无效,应该是快捷方式被改了 系统 win10 1.打开对应的下面两个地址,找到chrome的快捷方式,右键属性 C:\Use ...
- Nginx配置了解
安装Nginx常用编译选项说明 nginx大部分常用模块,编译时./configure --help查看,以--without开头的都是默认安装. --prefix=PATH 指定nginx的安装目录 ...