java 队列和栈相互实现
一、队列实现栈
public class queue2stack {
public static void main(String[] args) {
QS qs = new QS();
qs.push("1");
qs.push("2");
qs.push("3");
System.out.println(qs.pop());
System.out.println(qs.pop());
System.out.println(qs.peek());
QS qs2 = new QS();
qs2.push("1");
qs2.push("2");
qs2.push("3");
System.out.println(qs2.pop());
System.out.println(qs2.pop());
System.out.println(qs2.peek());
}
static class QS{
private Queue queueMain = new ArrayDeque();
private Queue queueWork = new ArrayDeque();
public boolean push(Object object){
try {
queueMain.offer(object);
return true;
} catch (Exception e) {
return false;
}
}
public int size() {
return queueMain.size();
}
public Object pop(){
if(queueMain.isEmpty()) {
return null;
}
Queue temp = new ArrayDeque();
int size = queueMain.size();
for (int i = 0; i < size - 1; i ++) {
temp.offer(queueMain.poll());
}
Object o = queueMain.poll();
int size1 = temp.size();
for (int i = 0; i < size1; i ++) {
queueMain.offer(temp.poll());
}
return o;
}
public Object peek(){
if(queueMain.isEmpty()) {
return null;
}
Queue temp = new ArrayDeque();
int size = queueMain.size();
for (int i = 0; i < size - 1; i ++) {
temp.offer(queueMain.poll());
}
Object o = queueMain.peek();
temp.offer(queueMain.poll());
int size1 = temp.size();
for (int i = 0; i < size1; i ++) {
queueMain.offer(temp.poll());
}
return o;
}
public boolean empty(){
return queueMain.isEmpty();
}
/**********优化***********/
public boolean push2(Object object){
try {
if (queueMain.isEmpty() && queueWork.isEmpty()) {
queueMain.offer(object);
}
if (queueMain.isEmpty()) {
queueWork.offer(object);
}
if(queueWork.isEmpty()) {
queueMain.offer(object);
}
return true;
} catch (Exception e) {
return false;
}
}
public Object pop2(){
if(queueMain.isEmpty() && queueWork.isEmpty()) {
return null;
}
if (queueMain.isEmpty()) {
while (queueWork.size() > 1) {
queueMain.offer(queueWork.poll());
}
return queueWork.poll();
}
if (queueWork.isEmpty()) {
while (queueMain.size() > 1) {
queueWork.offer(queueMain.poll());
}
return queueMain.poll();
}
return null;
}
public Object peek2(){
if(queueMain.isEmpty() && queueWork.isEmpty()) {
return null;
}
if (queueMain.isEmpty()) {
while(queueWork.size() > 1) {
queueMain.offer(queueWork.poll());
}
Object e = queueWork.peek();
queueMain.offer(queueWork.poll());
return e;
}
if (queueWork.isEmpty()) {
while(queueMain.size() > 1) {
queueWork.offer(queueMain.poll());
}
Object e = queueMain.peek();
queueWork.offer(queueMain.poll());
return e;
}
return null;
}
}
}
二、栈实现队列
public class Stack2queue {
public static void main(String[] args) {
SQ sq = new SQ();
sq.offer("a");
sq.offer("b");
sq.offer("c");
System.out.println(sq.poll());
System.out.println(sq.poll());
sq.offer("d");
sq.offer("e");
System.out.println(sq.poll());
System.out.println(sq.peek());
System.out.println(sq.poll());
System.out.println(sq.poll());
}
static class SQ{
private Stack stackMain = new Stack();
private Stack stackWork = new Stack();
public boolean offer(Object ele) {
stackMain.push(ele);
return true;
}
public Object poll() {
if (stackWork.empty()) {
while(stackMain.size() > 0) {
stackWork.push(stackMain.pop());
}
}
if (stackWork.empty()) {
return null;
}
return stackWork.pop();
}
public Object peek() {
if (stackWork.empty()) {
while(stackMain.size() > 0) {
stackWork.push(stackMain.pop());
}
}
if (stackWork.empty()) {
return null;
}
return stackWork.peek();
}
}
}
结果自行运行测试
java 队列和栈相互实现的更多相关文章
- java 队列和栈及示例
一.栈的实现: 1.Stack实现 接口实现: class Stack<E> extends Vector<E> {......} 常用的api函数如下: boolean is ...
- Java队列与栈转换中String.Valueof()使用
1. 由 基本数据型态转换成 String String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 也就是 String.valueOf() 这个参数多载的方法 有下 ...
- 两个栈实现队列+两个队列实现栈----java
两个栈实现队列+两个队列实现栈----java 一.两个栈实现一个队列 思路:所有元素进stack1,然后所有出s ...
- 栈和队列数据结构的相互实现[LeetCode]
栈是先进后出,队列是先进后出,这里讨论一下两种数据结构之间的相互实现. 一.用两个栈实现队列 我们用一个栈来实现队列的进队操作(栈A),用另一个栈来实现队列的出队操作(栈B). 1.入队列: 把元素放 ...
- 2018.9.5 Java中使用栈来模拟队列
栈的规律是是先进后出 队列的规律是先进先出 栈模拟队列 首先我们定义两个栈,一个放数据,一个出数据,判断B栈是否有元素,有元素则直接pop:没有元素则需要我们将A里面的元素出栈然后放到B里面,再取出, ...
- 两个队列实现栈&两个栈实现队列(JAVA)
1,两个栈实现队列 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路:栈的特点时先进后出,队列的特点是先进先出. 若此时有两个队列stack1,st ...
- LeetCode--255--用队列实现栈(java版)
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...
- 剑指offer第二版面试题9:用两个队列实现栈(JAVA版)
题目:用两个队列实现栈. 分析:通过一系列的栈的压入和弹出操作来分析用队列模拟一个栈的过程,如图所示,我们先往栈内压入一个元素a.由于两个队列现在都是空,我们可以选择把a插入两个队列中的任一个.我们不 ...
- Java实现 LeetCode 225 用队列实现栈
225. 用队列实现栈 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 注意: 你只能使 ...
随机推荐
- iframe中使用模态框提交表单后,iframe加载父页面的解决方法
在iframe中使用模态框提交表单后,会出现iframe加载整个父页面的问题,如下图: 解决方法: 在form表单中添加target属性 _parent 这个属性会使目标文档载入父窗口或者包含来超链接 ...
- 牛客网暑期ACM多校训练营(第四场) G Maximum Mode 思维
链接:https://www.nowcoder.com/acm/contest/142/G来源:牛客网 The mode of an integer sequence is the value tha ...
- 创建最简单的exe形式COM组件并在MFC程序调用
来新公司学习接手新项目,拿到代码打开解决方案看到里面竟然有40几个工程,有点吃惊.具体看代码也有很多之前没见过的写法,上了几天火. 有件事就没太搞明白,按照文档的说法上层很多软件都要调用IO服务器,但 ...
- webpack多页应用架构系列(一):一步一步解决架构痛点
这系列文章讲什么? 前些时间,写过一个项目,前后端分离,没有借助任何框架,项目页面特别的多,页面都是html直接写的,许多公共html,写了好多处,有一个地方需要改就得改好多地方,js也是随意写,每个 ...
- Win7下部署Lepus企业级MySQL数据库监控
从官网下载(http://www.lepus.cc/soft/17)安装包后,解压到phpStudy的www目录下: 打开phpStudy管理界面,找到站点管理,并新增站点: 在浏览器里面打开后,报此 ...
- Git使用(一)安装配置过程-Win7
公司项目需要使用Git作为项目的代码库管理工具.正好借此机会写个安装过程 1.首先下载Git下载地址:https://git-scm.com/download/win 当前下载版本:Git-2.13. ...
- IP地址和int互转
/** * @author: yqq * @date: 2019/5/8 * @description: ip地址与int之间互换 * https://mp.weixin.qq.com/s?__biz ...
- FreeSql (二十一)查询返回数据
FreeSql 采用 ExpressionTree 优化读取速读,如果懂技术的你一定知道 .NETCore 技术下除了原生代码,最快就是 Emit 和 ExpressionTree. 项目在初期使用的 ...
- 警告:Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
执行Maven Install打包的时候,提示以下警告信息: [WARNING] Using platform encoding (GBK actually) to copy filtered res ...
- 行内元素有哪些?块级元素有哪些?空(void)元素有哪些?
CSS规范规定,每个元素都有display属性,确定该元素的类型.每个元素都有默认的display值,如div的display默认值为“block”,则为“块级”元素:span默认display属性值 ...