用两个栈实现队列

来源:互联网 发布:一键手绘图的软件 编辑:程序博客网 时间:2024/06/11 14:59

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

/** * 解法一:将数据元素压入第一个栈,完成入队操作; *      当第一个栈不为空时, 将第一个栈的所有数据元素出栈并压入第二个栈中,然后弹出第二个栈的栈顶元素,完成出队操作; *      当第二个栈不为空时,将第二个栈的所有数据元素出栈并压入第一个栈中(新压入数据元素在第一个栈中) * */public class Solution {    Stack<Integer> stack1 = new Stack<>();    Stack<Integer> stack2 = new Stack<>();    public static void main(String[] args) {        Solution solution = new Solution();        solution.push(1);        solution.push(2);        solution.push(3);        System.out.println(solution.pop());        System.out.println(solution.pop());        System.out.println(solution.pop());    }    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 class Solution {    Stack<Integer> stack1 = new Stack<>();    Stack<Integer> stack2 = new Stack<>();    public static void main(String[] args) {        Solution solution = new Solution();        solution.push(1);        solution.push(2);        solution.push(3);        System.out.println(solution.pop());        System.out.println(solution.pop());        System.out.println(solution.pop());    }    public void push(int node) {        // 将数据元素压入第一个栈,完成入队操作        stack1.push(node);    }    public int pop() {        if (stack1.empty() && stack2.empty()) {            throw new RuntimeException("Queue is empty!");        }        // 当第二个栈为空时,将第一个栈中的数据元素弹出并压入第二个栈中        if (stack2.empty()) {            while (!stack1.empty()) {                stack2.push(stack1.pop());            }        }        // 当第二个栈不为空时,弹出第二个栈的栈顶元素(新入栈的数据元素暂存于第一个栈中),完成出队操作        return stack2.pop();    }}
/** * 解法三:数据元素从第一个栈进入队列,完成入队操作; *      当第二个栈为空时,将第一个栈中的所有数据元素弹出并压入第二个栈,然后弹出第二个栈栈顶元素,完成出队操作; *      当第二个栈不为空时,将第二个栈的栈顶元素弹出并压入第一个栈,然后从第一个栈中弹出并返回该数据元素,完成出队操作 */public class Solution {    Stack<Integer> stack1 = new Stack<Integer>();    Stack<Integer> stack2 = new Stack<Integer>();    public static void main(String[] args) {        Solution solution = new Solution();        solution.push(1);        solution.push(2);        solution.push(3);        System.out.println(solution.pop());        System.out.println(solution.pop());        System.out.println(solution.pop());    }    public void push(int node) {        // 数据元素从第一个栈进入队列,完成入队操作        stack1.add(node);    }    public int pop() {        // 当第二个栈为空时,将第一个栈中的所有数据元素弹出并压入第二个栈,然后弹出第二个栈栈顶元素,完成出队操作        if (stack2.size() == 0) {            while (stack1.size() != 0) {                stack2.push(stack1.pop());            }            return stack2.pop();            // 当第二个栈不为空时,将第二个栈的栈顶元素弹出并压入第一个栈,然后从第一个栈中弹出并返回该数据元素,完成出队操作        } else {            stack1.push(stack2.pop());            return stack1.pop();        }    }}
原创粉丝点击