剑指offer:4. 用两个栈实现队列

4. 用两个栈实现队列

题目描述

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

思路:1,整体思路是元素先依次进入栈1,再从栈1依次弹出到栈2,然后弹出栈2顶部的元素,整个过程就是一个队列的先进先出
2,但是在交换元素的时候需要判断两个栈的元素情况:
“进队列时”,队列中是还还有元素,若有,说明栈2中的元素不为空,此时就先将栈2的元素倒回到栈1 中,保持在“进队列状态”。
“出队列时”,将栈1的元素全部弹到栈2中,保持在“出队列状态”。
所以要做的判断是,进时,栈2是否为空,不为空,则栈2元素倒回到栈1,出时,将栈1元素全部弹到栈2中,直到栈1为空。

Java实现:

import java.util.Stack;

/**
 * @Classname Queue
 * @Description TODO
 * @Date 2019/6/3 10:06
 * @Created by 超
 */
public class Queue {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();

    public void  push(int node) {
        stack1.push(node);
    }

    public int pop() {
        while (! stack1.isEmpty()) {
            stack2.push(stack1.pop());
        }
        int first = stack2.pop();
        while (! stack2.isEmpty()) {
            stack1.push(stack2.pop());
        }
        return first;
    }

    public void push1(int node) {
        stack1.push(node);
    }

    public int pop1() {
        if (stack1.isEmpty() && stack2.isEmpty()) {
            throw new RuntimeException("Queue is Empty!");
        }
        if (stack2.empty()) {
            while (! stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
}

不足之处欢迎批评讨论!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章