zoukankan      html  css  js  c++  java
  • java基础编程——用两个栈来实现一个队列

    题目描述

    用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

    题目代码

    /**
     *  <分析>:
     *  入队:将元素进栈A
     *  出队:判断栈B是否为空,
     *  如果为空,则将栈A中所有元素pop,并push进栈B,栈B出栈;
     *  如果不为空,栈B直接出栈。
     * Created by YuKai Fan on 2018/8/20.
     */
    public class TwoStackToAchieveQueue {
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
    
        public static void main(String[] args) {
            TwoStackToAchieveQueue twtaq = new TwoStackToAchieveQueue();
            twtaq.push(1);
            twtaq.push(2);
            twtaq.push(3);
            System.out.println(twtaq.pop());
            System.out.println(twtaq.pop());
            twtaq.push(4);
            System.out.println(twtaq.pop());
            System.out.println(twtaq.pop());
            twtaq.push(5);
            System.out.println(twtaq.pop());
    
        }
        /*
        push方法,直接将值push到stack1中
         */
        public void push(int node) {
                stack1.push(node);
        }
    
        /*
        pop方法,先判断stack2是否为空,
        如果不为空的话,直接返回stack2的栈顶,
        如果为空,就将stack1中的元素,压入stack2中,然后返回弹出stack2的元素
    
        为什么要判断是否为空,这里用main方法中的数据来解释?
        当向stack1中压入三个数时,此时stack1中的数据结构为
        |3|
        |2|
        |1|
        之后执行两次pop操作,因为stack2为空,所以将stack1中的值压入stack2中。此时stack1中的元素为空,stack2的数据结构为:
        |1|
        |2|
        |3|
        执行两次结束后返回stack2中的栈顶元素,所以打印出来1,2。此时stack2的数据结构为:
        |3|
        再往下执行push(4),此时stack1中的数据结构为:
        |4|
        在执行一次pop,但是此时stack2是不为空的,如果不加入判断,就会将4压入栈顶,在pop的话,出来的就是4而不是3,所以需要判断stack2是否为空,如果不为空就直接弹出栈顶元素
        所以此时应该打印3
        接下来有执行一次pop,此时stack2由于上一次的pop操作,此时为空,所以将stack1中的4弹出并压入stack2,然后在弹出stack2的元素,所以此时打印4
        最后的两次操作也是同样的道理
         */
        public int pop() {
            if (stack1.empty() && stack2.empty()) {
                throw new RuntimeException("Queue is empty!");
            }
            if (stack2.empty()) {
                int l = stack1.size();
                for (int i = 0; i < l; i++) {
                    stack2.push(stack1.pop());
                }
            }
            return stack2.pop();
        }
    }

    题目延伸

    用两个队列实现一个栈的功能?要求给出算法和思路!

    /**
     * 将queue1用作进栈出栈,queue2作为一个中转站
     * 入栈时,直接压入queue1中出栈时,先将queue1中的元素除最后一个元素外依次出队列,并压入队列queue2中,
     * 将留在queue1中的最后一个元素出队列即为出栈元素,最后还要把queue2中的元素再次压入queue1中
     *
     * Created by YuKai Fan on 2018/8/20.
     */
    public class TwoQueueToStack {
        LinkedList<Integer> queue1 = new LinkedList<>();
        LinkedList<Integer> queue2 = new LinkedList<>();
    
        public static void main(String[] args) throws Exception {
            TwoQueueToStack tqts = new TwoQueueToStack();
            tqts.push(1);
            tqts.push(2);
            tqts.push(3);
            tqts.push(4);
            tqts.push(5);
            System.out.println(tqts.pop());
            System.out.println(tqts.pop());
            System.out.println(tqts.pop());
            System.out.println(tqts.pop());
            System.out.println(tqts.pop());
        }
    
        public void push(int node) {
            queue1.addLast(node);
        }
    
        public int pop() throws Exception {
            if (queue1.isEmpty() && queue2.isEmpty()) {
                throw new Exception("queue is empty");
            }
            if (queue1.size() == 1) {
                return queue1.poll();
            } else {
                //如果在出列queue1最后一个元素时,又在queue1入列元素就会,发生元素顺序错乱
                while (queue1.size() != 1) {
                    queue2.add(queue1.poll());
                }
                int m = queue1.poll();
                while (!queue2.isEmpty()) {
                    queue1.add(queue2.poll());
                }
                return m;
            }
        }
    
    }
  • 相关阅读:
    Linux环境下安装Tigase XMPP Server
    虚拟机几种网络连接方式的区别
    MySQL修改root密码
    Eclipse使用多个Console
    Eclipse导入Java项目时“No projects are found to import”错误的处理
    GitHub上下载源代码的方法
    Tigase XMPP Server的安装
    MySQL的安装与配置
    Windows下查看JDK是否安装以及安装路径
    archive for required library...
  • 原文地址:https://www.cnblogs.com/FanJava/p/9506048.html
Copyright © 2011-2022 走看看