一、队列实现栈

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 队列和栈相互实现的更多相关文章

  1. java 队列和栈及示例

    一.栈的实现: 1.Stack实现 接口实现: class Stack<E> extends Vector<E> {......} 常用的api函数如下: boolean is ...

  2. Java队列与栈转换中String.Valueof()使用

    1. 由 基本数据型态转换成 String String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 也就是 String.valueOf() 这个参数多载的方法 有下 ...

  3. 两个栈实现队列+两个队列实现栈----java

                                               两个栈实现队列+两个队列实现栈----java 一.两个栈实现一个队列 思路:所有元素进stack1,然后所有出s ...

  4. 栈和队列数据结构的相互实现[LeetCode]

    栈是先进后出,队列是先进后出,这里讨论一下两种数据结构之间的相互实现. 一.用两个栈实现队列 我们用一个栈来实现队列的进队操作(栈A),用另一个栈来实现队列的出队操作(栈B). 1.入队列: 把元素放 ...

  5. 2018.9.5 Java中使用栈来模拟队列

    栈的规律是是先进后出 队列的规律是先进先出 栈模拟队列 首先我们定义两个栈,一个放数据,一个出数据,判断B栈是否有元素,有元素则直接pop:没有元素则需要我们将A里面的元素出栈然后放到B里面,再取出, ...

  6. 两个队列实现栈&两个栈实现队列(JAVA)

    1,两个栈实现队列 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路:栈的特点时先进后出,队列的特点是先进先出. 若此时有两个队列stack1,st ...

  7. LeetCode--255--用队列实现栈(java版)

    使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...

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

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

  9. Java实现 LeetCode 225 用队列实现栈

    225. 用队列实现栈 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 注意: 你只能使 ...

随机推荐

  1. 前端开发-CSS语法标准

    一.命名规则说明: 1.命名规则说明: 所有的命名最好都小写 属性的值一定要用双引号("")括起来,且一定要有值如class="nav",id="na ...

  2. HDU-6053 TrickGCD

    题目连接: https://vjudge.net/problem/HDU-6053 Description You are given an array A , and Zhu wants to kn ...

  3. HDU 4607 Park Visit 树的最大直径

    题意: 莱克尔和她的朋友到公园玩,公园很大也很漂亮.公园包含n个景点通过n-1条边相连.克莱尔太累了,所以不能去参观所有点景点. 经过深思熟虑,她决定只访问其中的k个景点.她拿出地图发现所有景点的入口 ...

  4. hdu 3265 Posters(线段树+扫描线+面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 题意:给你一张挖了洞的墙纸贴在墙上,问你总面积有多少. 挖了洞后其实就是多了几个矩形墙纸,一张墙 ...

  5. codeforces 284 C. Cows and Sequence(线段树)

    题目链接:http://codeforces.com/contest/284/problem/C 题意:就是给出3个操作 1)是将前i 个数加x 2)在数组最后添加一个数x 3)删除数组最后的那个数 ...

  6. 深入浅出TypeScript(4)- 使用接口和类型别名

    在TypeScript中,为了可以约束对象定义,提供了两个新的特性,接口和类型别名. TypeScript中的接口 在强类型语言中,都有接口的概念,那么TypeScript中的接口是如何使用的呢? 接 ...

  7. 2018 Multi-University Training Contest 3(部分题解)

    Problem F. Grab The Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Ja ...

  8. np问题(大数阶乘取模)

    转自 np问题 题目描述: LYK 喜欢研究一些比较困难的问题,比如 np 问题. 这次它又遇到一个棘手的 np 问题.问题是这个样子的:有两个数 n 和 p,求 n 的阶乘对 p 取模后的结果. L ...

  9. 洛谷 P1059【明明的随机数】 题解

    事实上,完全可以先将输入进来带有重复的元素们保存进一个数组并对该数组进行排序,再将该数组的各个元素逐个判断是否与前一元素相同(重复与否的判断),将不重复的元素转移至另一个数组,与此同时进行对不重复元素 ...

  10. 脱离脚手架来配置、学习 webpack4.x (一)基础搭建项目

    序 现在依旧记得第一次看到webpack3.x 版本配置时候的状态  刚开始看到这些真的是一脸懵.希望这篇文章能帮到刚开始入门的同学. webpack 是什么? webpack是一个模块化打包工具,w ...