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() – 返回栈是否为空 注意: 你只能使 ...
随机推荐
- python requests接口测试系列:连接mysql,获取mysql查询的值作为接口的入参
主要思路: 连接mysql数据库,这里数据库需要使用Proxifier来设置代理,然后才能正常连接 获取mysql数据库中某一数据,作为接口的参数信息 将接口返回结果保存至csv数据表中 # -*- ...
- EF的3种开发模式
那么明显开发模式是三种. 即:DateBase First(数据库优先).Model First(模型优先)和Code First(代码优先). 当然,如果把Code First模式的两种具体方式独立 ...
- Vim高手,从来不用鼠标2——替换、撤销、缩进、查找
本文章原创首发于公众号:编程三分钟 vim 替换.撤销.缩进.查找 上一次我们掌握了移动.跳转.定位.操作(删除.复制.粘贴),基本使用vim脱离鼠标完全是可以做到的了.速记如下: 移动: h,l,j ...
- JMeter简介及使用JMeter来访问网站
参考: http://jmeter.apache.org/ http://blog.chinaunix.net/uid-26884465-id-3416869.html http://www.ltes ...
- 同步vmware虚拟机和主机的时间
1. 打开虚拟机->设置 2. 选择选项标签页,选中VMware Tools,勾选“将客户机时间与主机同步”
- CF EDU - E. Lomsat gelral 树上启发式合并
学习:http://codeforces.com/blog/entry/44351 E. Lomsat gelral 题意: 给定一个以1为根节点的树,每个节点都有一个颜色,问每个节点的子树中,颜色最 ...
- POJ-1469 COURSES ( 匈牙利算法 dfs + bfs )
题目链接: http://poj.org/problem?id=1469 Description Consider a group of N students and P courses. Each ...
- CodeForces 1107 - G Vasya and Maximum Profit 线段树
题目传送门 题解: 枚举 r 的位置. 线段树每个叶子节点存的是对应的位置到当前位置的价值. 每次往右边移动一个r的话,那么改变的信息有2个信息: 1. sum(a-ci) 2.gap(l, r) 对 ...
- lightoj 1140 - How Many Zeroes?(数位dp)
Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...
- CSS特效集锦:视觉魔法的碰撞与融合(二)
引言 长久以来,我认识到.CSS,是存在极限的.正如曾经替你扛下一切的那个男人,也总有他眼含热泪地拼上一切,却也无法帮你做到的事情,他只能困窘地让你看到他的无能为力,怅然若失. 然后和曾经他成长的时代 ...