import java.util.*;
import java.lang.*;
public class Main {
public static void main(String args[]) {
Solution solution = new Solution();
solution.push(8);
solution.push(5);
solution.push(7);
System.out.println(solution.pop());
}
}
1.只用了Stack1
public void push(int node) {
stack1.add(node);
}
public int pop() {
int c=0;
c=stack1.get(0);
stack1.remove(0);
return c;
}
}
2.用一个栈做队列的入队操作,一个做出队操作。
class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.add(node);
}
public int pop() {
if (stack2.isEmpty())
while(!stack1.isEmpty())
stack2.push(stack1.pop());
return stack2.pop();
}
}