《剑指》7

来源:互联网 发布:php 用户登录次数 编辑:程序博客网 时间:2024/05/16 09:22

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

package offer;import java.util.Stack;/** * 用两个栈来实现一个队列,完成队列的Push和Pop操作。 *  队列中的元素为int类型。 * @author WQC * */public class offer7_TwoStackImplQueue {    Stack<Integer> stack1 = new Stack<Integer>();    Stack<Integer> stack2 = new Stack<Integer>();    public void push(int node) {        stack1.push(node);    }    public int pop() {        if(stack2.isEmpty()){            if(stack1.isEmpty()){                return -1;            }            while(!stack1.isEmpty()){                stack2.push(stack1.pop());            }        }        return stack2.pop();    }}
0 0
原创粉丝点击