zoukankan      html  css  js  c++  java
  • 剑指Offer-用两个栈实现队列

    题目描述

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

    思路

    栈A用来作入队列,栈B用来出队列,当栈B为空时,栈A全部出栈到栈B,栈B再出栈(即出队列)

    代码实现

    package StackAndQueue;
    
    import java.util.Stack;
    
    /**
     * 用两个栈实现队列
     * 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
     * 思路:
     * 栈A用来作入队列,栈B用来出队列
     * 当栈B为空时,栈A全部出栈到栈B,栈B再出栈(即出队列)
     */
    public class Solution18 {
        public static void main(String[] args) {
            Solution18 solution18 = new Solution18();
            solution18.push(1);
            solution18.push(2);
            System.out.println(solution18.pop());
            solution18.pop();
        }
    
        Stack<Integer> stack1 = new Stack<Integer>();
        Stack<Integer> stack2 = new Stack<Integer>();
    
        public void push(int node) {
            stack1.push(node);//stack1负责入队
        }
    
        public int pop() {
            if (stack1.empty() && stack2.empty()) {
                throw new RuntimeException("队列为空");
            }
            if (stack2.empty()) {
                while (!stack1.empty()) {
                    stack2.push(stack1.pop());
                }
            }
            return stack2.pop();//stcak2负责出队
        }
    }
    
    
  • 相关阅读:
    Sunnypig闯三角关
    送给圣诞夜的贺卡
    uva 1592(NEERC 2009 STL)
    uva 297(传递闭包 WF 1996)
    hdu 4190(二分)
    uva 3592 (MST, kruskal)
    uva 11997 (基础数据结构)
    hdu 2680 (Dijkstra)
    hdu 4568(状态压缩dp)
    hdu 4582 (树上的贪心)
  • 原文地址:https://www.cnblogs.com/wupeixuan/p/8623077.html
Copyright © 2011-2022 走看看