您的位置:首页 > 职场人生

剑指Offer面试题9:用两个栈实现队列

2018-01-19 18:47 337 查看
/*
* offer 6:用两个栈(先进先出)来实现一个队列(后进先出),完成队列的Push和Pop操作。 队列中的元素为int类型。
*/
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: