Java怎么用两个栈实现队列和用两个队列实现一个栈
发表于:2025-02-16 作者:千家信息网编辑
千家信息网最后更新 2025年02月16日,这篇文章主要讲解了"Java怎么用两个栈实现队列和用两个队列实现一个栈",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java怎么用两个栈实现队列和用两
千家信息网最后更新 2025年02月16日Java怎么用两个栈实现队列和用两个队列实现一个栈
这篇文章主要讲解了"Java怎么用两个栈实现队列和用两个队列实现一个栈",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java怎么用两个栈实现队列和用两个队列实现一个栈"吧!
import java.util.ArrayList;import java.util.List;import java.util.Stack; /* * Q 57 用两个栈实现队列 */public class QueueImplementByTwoStacks { private Stackstack1; private Stack stack2; QueueImplementByTwoStacks(){ stack1=new Stack (); stack2=new Stack (); } public Integer poll(){ Integer re=null; if(!stack2.empty()){//如果stack2不为空则弹出栈顶元素 re=stack2.pop(); }else{//如果stack2为空,将stack1中的元素弹出,依次压栈到stack2中,那么stack1栈底的元素就会成为stack2栈顶的元素,从而达到stack1先进先出的队列顺序 while(!stack1.empty()){ re=stack1.pop(); stack2.push(re); } if(!stack2.empty()){ re=stack2.pop(); } } return re; } public Integer offer(int o){//每次只从stack1压栈 stack1.push(o); return o; } public static void main(String[] args) { QueueImplementByTwoStacks queue=new QueueImplementByTwoStacks(); List re=new ArrayList (); queue.offer(1); queue.offer(2); queue.offer(3); re.add(queue.poll()); queue.offer(4); re.add(queue.poll()); queue.offer(5); re.add(queue.poll()); re.add(queue.poll()); re.add(queue.poll()); System.out.println(re.toString()); }}
import java.util.LinkedList;/* * Q 57 用两个队列实现一个栈 */public class StackImplementByTwoQueues { //use 'queue1' and 'queue2' as a queue.That means only use the method 'addLast' and 'removeFirst'. private LinkedListqueue1; private LinkedList queue2; StackImplementByTwoQueues(){ queue1=new LinkedList (); queue2=new LinkedList (); } public Integer pop(){//队列1不为空,队列2为空,将队列顺序读到队列2中,则最后一个元素就是需要弹出的值 Integer re=null; if(queue1.size()==0&&queue2.size()==0){ return null; } if(queue2.size()==0){ while(queue1.size()>0){ re=queue1.removeFirst();//等同re=queue1.remove(); if(queue1.size()!=0){//do not add the last element of queue1 to queue2 queue2.addLast(re); } } }else if (queue1.size()==0){ while(queue2.size()>0){ re=queue2.removeFirst(); if(queue2.size()!=0){//do not add the last element of queue2 to queue1 queue1.addLast(re); } } } return re; } public Integer push(Integer o){ if(queue1.size()==0&&queue2.size()==0){ queue1.addLast(o);//queue2.addLast(o); is also ok } if(queue1.size()!=0){ queue1.addLast(o);//等同queue1.=add(0) }else if(queue2.size()!=0){ queue2.addLast(o); } return o; } public static void main(String[] args) { StackImplementByTwoQueues stack=new StackImplementByTwoQueues(); int tmp=0; stack.push(1); stack.push(2); stack.push(3); tmp=stack.pop(); System.out.println(tmp);//3 stack.push(4); tmp=stack.pop(); System.out.println(tmp);//4 tmp=stack.pop(); System.out.println(tmp);//2 stack.push(5); stack.push(6); tmp=stack.pop(); System.out.println(tmp);//6 tmp=stack.pop(); System.out.println(tmp);//5 tmp=stack.pop(); System.out.println(tmp);//1 }}
感谢各位的阅读,以上就是"Java怎么用两个栈实现队列和用两个队列实现一个栈"的内容了,经过本文的学习后,相信大家对Java怎么用两个栈实现队列和用两个队列实现一个栈这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
队列
两个
元素
学习
内容
就是
顺序
先进
思路
情况
文章
更多
知识
知识点
篇文章
跟着
问题
先出
实践
推送
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
靶向数据库
餐饮食品安全大数据库
数据库部署在存储服务器上
学生关于网络安全的法律法规
佛山风云网络技术有限公司
银行的软件开发中心
数据库换系统有什么影响
数据库 linux 监听
中国哲学社科数据库
网络安全隔离业绩
01年的热血传奇有几个服务器
抖音协议软件开发工作室
php投票的数据库格式
vb 如何做数据库备份
cs包括数据库
深圳华辰软件开发 培训
网络安全大学的排名
方舟瓦尔盖罗服务器建家推荐
ddr3服务器内存
各种网络安全法规
西门子smart可以连数据库吗
国家学生网络安全法心得体会
思科网络技术期末考试a卷
手机联不上服务器的原因
做服务器中的管理员
运维与网络安全
网络安全与科技创新
携手共护推动网络安全行稳致远
汉阳区有软件开发公司吗
mac怎么访问本地数据库