两个栈式实现队列

面试中常出现让你手写两个队列实现一个栈,两个栈实现一个队列的问题,很是头疼!今天就仔细将我分析,思考过的Java代码给大家分享一下:

(一)两个队列实现一个栈:


两个队列添加元素,哪个队列为空,由于在输出元素时,要进行相应元素的移动(除去尾部元素),所以要在对应不为空的队列进行元素的添加;在输出数据时,要进行两个队列的变相操作,不为空的队列要依次向为空的队列中添加元素,直到尾元素输出即可!

[java]  view plain  copy
  1. /** 
  2.  *  
  3.  * @author superYC 
  4.  * 两个队列实现一个栈 
  5.  * 
  6.  */  
  7. public class TwoQueueImplStack {  
  8.     Queue<Integer> queue1 = new ArrayDeque<Integer>();  
  9.     Queue<Integer> queue2 = new ArrayDeque<Integer>();  
  10.       
  11.     /* 
  12.      * 向栈中压入数据 
  13.      */  
  14.     public void push(Integer element){  
  15.         //两个队列都为空时,优先考虑 queue1  
  16.         if(queue1.isEmpty() && queue2.isEmpty()){  
  17.             queue1.add(element);  
  18.             return;  
  19.         }  
  20.           
  21.         //如果queue1为空,queue2有数据,直接放入queue2  
  22.         if(queue1.isEmpty()){  
  23.             queue2.add(element);  
  24.             return;  
  25.         }  
  26.           
  27.         //如果queue2为空,queue1有数据,直接放入queue1中  
  28.         if(queue2.isEmpty()){  
  29.             queue1.add(element);  
  30.             return;  
  31.         }  
  32.     }  
  33.       
  34.     /* 
  35.      * 从栈中弹出一个数据 
  36.      */  
  37.     public Integer pop(){  
  38.         //如果两个栈都为空,则没有元素可以弹出,异常  
  39.         if(queue1.isEmpty() && queue2.isEmpty()){  
  40.             try{  
  41.                 throw new Exception("satck is empty!");  
  42.             }catch(Exception e){  
  43.                 e.printStackTrace();  
  44.             }  
  45.         }  
  46.           
  47.         //如果queue1中没有元素,queue2中有元素,将其queue2中的元素依次放入queue1中,直到最后一个元素,弹出即可  
  48.         if(queue1.isEmpty()){  
  49.             while(queue2.size() > 1){  
  50.                 queue1.add(queue2.poll());  
  51.             }  
  52.             return queue2.poll();  
  53.         }  
  54.           
  55.         //如果queue2中没有元素,queue1中有元素,将其queue1中的元素依次放入queue2中,直到最后一个元素,弹出即可  
  56.         if(queue2.isEmpty()){  
  57.             while(queue1.size() > 1){  
  58.                 queue2.add(queue1.poll());  
  59.             }  
  60.             return queue1.poll();  
  61.         }  
  62.           
  63.         return (Integer)null;  
  64.     }  
  65.       
  66.     public static void main(String[] args) {  
  67.         TwoQueueImplStack qs = new TwoQueueImplStack();  
  68.         qs.push(2);  
  69.         qs.push(4);  
  70.         qs.push(7);  
  71.         qs.push(5);  
  72.         System.out.println(qs.pop());  
  73.         System.out.println(qs.pop());  
  74.           
  75.         qs.push(1);  
  76.         System.out.println(qs.pop());  
  77.     }  
  78. }  

(二)两个栈实现一个队列:


第一个栈只负责添加元素,第二个栈在弹出元素时,首先判断当前栈是否为空,若为空就直接将其第一个栈中的数据全部压入第二个栈中,然后输出栈顶元素,即可实现队列效果;若第二个栈中有数据,添加直接将其数据压入第一个栈中,输出时直接输出第二个栈顶的元素即可!

[java]  view plain  copy
  1. /** 
  2.  *  
  3.  * @author superYC 
  4.  * 两个栈实现一个队列 
  5.  *  
  6.  */  
  7. public class TwoStackImplQueue {  
  8.   
  9.     Stack<Integer> stack1 = new Stack<Integer>();  
  10.     Stack<Integer> stack2 = new Stack<Integer>();  
  11.       
  12.     /* 
  13.      * 队列的数据压入过程 
  14.      */  
  15.     public void push(Integer element){  
  16.         stack1.push(element);  
  17.     }  
  18.       
  19.     /* 
  20.      * 队列的数据弹出过程 
  21.      */  
  22.     public Integer pop(){  
  23.         if(stack2.size() <= 0){  //第二个栈为空  
  24.             while(stack1.size() > 0){    //第一个栈不为空  
  25.                 stack2.push(stack1.pop());  //将其第一个栈的数据压入第二个栈中  
  26.             }  
  27.         }  
  28.         if(stack2.isEmpty()){  
  29.             try{  
  30.                 throw new Exception("queue is empty");  
  31.             }catch(Exception e){  
  32.                 //e.printStackTrace();  
  33.             }  
  34.         }  
  35.         Integer head = stack2.pop();  
  36.         return head;  
  37.     }  
  38.       
  39.     public static void main(String[] args) {  
  40.         TwoStackImplQueue sq = new TwoStackImplQueue();  
  41.         sq.push(1);  
  42.         sq.push(3);  
  43.         sq.push(5);  
  44.         sq.push(4);  
  45.         sq.push(2);  
  46.           
  47.         System.out.println(sq.pop());  
  48.         System.out.println(sq.pop());  
  49.           
  50.         sq.push(7);  
  51.         System.out.println(sq.pop());  
  52.     }  
  53. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值