标签:题目 Stub while sub pop generated [] ret 一个
import java.util.Stack;
/**
* 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
* @author Sonya
*
*进队列时,直接进入栈一中,出栈时先将栈一中的所有按照出栈次序依序压入栈2中,弹出栈2首个元素
*即为第一个入队列的元素,然后再将2所有的元素依次压入栈一中
*/
public class N5_Two_stack_to_queue {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.add(node);
}
public int pop() {
int a;
while(!stack1.isEmpty()) {
stack2.add(stack1.pop());
}
a=stack2.pop();
while(!stack2.isEmpty()) {
stack1.add(stack2.pop());
}
return a;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
N5_Two_stack_to_queue n5=new N5_Two_stack_to_queue();
n5.push(1);
n5.push(2);
System.out.println(n5.pop());
n5.push(3);
System.out.println(n5.pop());
System.out.println(n5.pop());
}
}
标签:题目 Stub while sub pop generated [] ret 一个
原文地址:https://www.cnblogs.com/kexiblog/p/10848918.html