不同的结构之间的表示:用2个?
Note:
stack1来用来一直push,加入我们push了1, 2, 3三个数,他们在stack1中的是顺序是(FILO),因此pop时,队列需要的先进先出(FIFO),无法从stack1中获取到先进的1,因此我们想pop时:
1.当stack2为空时,我们将stack1的元素全部pop出并push进stack2,则此时stack2中的元素就是FIFO顺序了,
2.当stack2不为空时,我们不将stack1中的元素pop并push进stack2(否则打破了FIFO)
import java.util.Stack
public class Solution {
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.size() <= 0) {
while (stack1.size() != 0) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}