zoukankan      html  css  js  c++  java
  • 两个栈实现一个队列&&两个队列实现一个栈

    一、两个栈实现一个队列

    思想:

    入队:直接加入stack1中

    出队:若stack2中有元素,则直接出栈,否则将stack1中全部元素依次出栈加入stack2,然后stack2出栈一个元素

       若stack1和stack2中都没有元素,则提示空!

     1 public class StackQueue {
     2 
     3     Stack<Integer> stack1 = new Stack<Integer>();
     4     Stack<Integer> stack2 = new Stack<Integer>();
     5     //相当于入队操作
     6     public void push(int node){
     7         stack1.push(node);
     8     }
     9    //相当于出队操作
    10     public int pop(){
    11         //stack1和stack2都为空
    12         if(stack1.isEmpty() && stack2.isEmpty()){
    13             try{
    14                 throw new Exception("satck is empty!");
    15             }catch(Exception e){
    16                 e.printStackTrace();
    17             }
    18             System.out.println("stack is empty!");
    19         }
    20         //stack2为空情况
    21         if(stack2.empty()){
    22             while(!stack1.empty())
    23                 stack2.push(stack1.pop());
    24         }
    25         //有元素出队返回
    26         return stack2.pop();
    27 
    28     }

    二、两个队列实现一个栈

    思想:

    入栈:两个队列必有一个是空的,入栈时选择空队列进入

    出栈:将非空队列中的元素依次出队并添加到另一个队列中,剩余最后一个时则出队并输出。

       若两队列都为空,则提示为空。

     1 ublic class QueueStack
     2 {
     3     //ArrayDeque集合是Deque接口的实现类,它是一个基于数组的双端队列
     4     Queue<Integer> queue1 = new ArrayDeque<>();
     5     Queue<Integer> queue2 = new ArrayDeque<>();
     6 
     7     //向栈压入数据
     8     public void push(Integer node){
     9 
    10         //两个队列都为空时,优先考虑 queue1
    11         if(queue1.isEmpty()&&queue2.isEmpty()){
    12             queue1.add(node);
    13             return;
    14         }
    15         //如果queue1为空,queue2有数据,直接放入queue2
    16         if(queue1.isEmpty()){
    17             queue2.add(node);
    18             return;
    19         }
    20         //如果queue2为空,queue1有数据,直接放入queue1中
    21         if(queue2.isEmpty()){
    22             queue1.add(node);
    23             return;
    24         }
    25     }
    26     //从栈弹出数据
    27     public Integer pop(){
    28         //如果两个队列都为空,则抛出异常
    29         if(queue1.isEmpty()&&queue2.isEmpty()){
    30             try{
    31                 throw new Exception("stack is empty");
    32             }catch (Exception e){
    33                 e.printStackTrace();
    34             }
    35         }
    36         //如果queue1为空,queue2不为空,则将queue2中的元素依次出队到queue2,直到最后一个输出
    37         if(queue1.isEmpty()){
    38             while (queue2.size() > 1){
    39                 queue1.add(queue2.poll());
    40             }
    41             return queue2.poll();
    42         }
    43         //如果queue2为空,queue1不为空,则将queue1中的元素依次出队到queue1,直到最后一个输出
    44         if(queue2.isEmpty()){
    45             while (queue1.size() > 1){
    46                 queue2.add(queue1.poll());
    47             }
    48             return queue1.poll();
    49         }
    50         return null;
    51     }

     

  • 相关阅读:
    C#Redis哈希Hashes
    C#Redis集合set
    C#Redis列表List
    C#Redis字符串
    入门redis
    C#/Net代码精简优化技巧
    单点登录在asp.net中的简单实现
    sql注入
    数据库sql优化
    常常忘记但是很重要的sql语句
  • 原文地址:https://www.cnblogs.com/lizijiangmm/p/8690879.html
Copyright © 2011-2022 走看看