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() – 返回栈是否为空 注意: 你只能使 ...
随机推荐
- 🕸捕获与改写HTTPS请求
前言 本文站在 macOS 用户的角度下,分享一下对 HTTPS 进行请求拦截.对响应进行修改的经验. 要注意的是,本文介绍的工具虽然一定程度上对 Windows 用户也适用 ,但并非所有工具都是免费 ...
- PHP学习—了解篇
了解PHP 了解神器:PhpStudy 一键搭建PHP环境 语法: PHP是一种可以嵌套在HTML页面的脚本语言 嵌套HTML文件: <!DOCTYPE html> <html& ...
- .NET Core 学习资料精选:进阶
.NET 3.0 这个月就要正式发布了,对于前一篇博文<.NET Core 学习资料精选:入门>大家学的可还开心?这是本系列的第二篇文章:进阶篇,喜欢的园友速度学起来啊. 对于还在使用传统 ...
- iOS Autoresizing Autolayout Size classes
Autoresizing:出现最早,仅仅能够针对父控件做约束(注意:要关闭Autolayout&Size classes才能够看到Autoresizing) 代码对应: UIView.h中的a ...
- python控制窗口缩放
import win32gui import win32con import time # 使用之前先打开一个记事本 notepad = win32gui.FindWindow("Notep ...
- 求大的组合数模板 利用Lucas定理
Lucas定理:A.B是非负整数,p是质数.A B写成p进制:A=a[n]a[n-1]…a[0],B=b[n]b[n-1]…b[0]. 则组合数C(A,B)与C(a[n],b[n])C(a[n-1], ...
- POJ 2491 Scavenger Hunt map
Scavenger Hunt Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2848 Accepted: 1553 De ...
- Codeforces Round #480 (Div. 2) A. Links and Pearls
题目地址:http://codeforces.com/contest/980/problem/A 官方题解: 我的理解:o表示珍珠,-表示链子,给一串字符串你可以任意重组这条项链(不能删去),判断这条 ...
- hdu 3265 Posters(线段树+扫描线+面积并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 题意:给你一张挖了洞的墙纸贴在墙上,问你总面积有多少. 挖了洞后其实就是多了几个矩形墙纸,一张墙 ...
- 【Nginx】基础学习概览【汇总】
一.Nginx 简介安装启动 二.Nginx的应用场景 三.Nginx中的配置命令 四.实现动态负载均衡 五.四层负载均衡 六.主从热备 七.动静分离 一.Nginx 简介安装启动 [Nginx]简介 ...