import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
while(!stack2.isEmpty())
stack1.push(stack2.pop());
stack1.push(node);
}
public int pop() {
while(!stack1.isEmpty())
stack2.push(stack1.pop());
return stack2.pop();
}
public static void main(String[]args){
//System.out.println("Hello");
//["PSH1","PSH2","PSH3","POP","POP","PSH4","POP","PSH5","POP","POP"]
Solution s=new Solution();
s.push(1);
s.push(2);
s.push(3);
System.out.println(s.pop());
System.out.println(s.pop());
s.push(4);
System.out.println(s.pop());
s.push(5);
System.out.println(s.pop());
System.out.println(s.pop());
}
}
利用两个栈实现一个队列
最新推荐文章于 2024-04-11 10:54:58 发布