代码
import java.util.LinkedList;
class MyStack {
public LinkedList<Integer> queue;
public LinkedList<Integer> temp;
public MyStack() {
queue=new LinkedList<>();
temp=new LinkedList<>();
}
public void push(int x) {
queue.addLast(x);
}
public int pop() {
while(queue.size()!=1)
{
temp.addLast(queue.pollFirst());
}
int res=queue.pollFirst();
while(!temp.isEmpty())
queue.addLast(temp.pollFirst());
return res;
}
public int top() {
while(queue.size()!=1)
{
temp.addLast(queue.pollFirst());
}
int res=queue.pollFirst();
if(!temp.isEmpty())
while(!temp.isEmpty())
queue.addLast(temp.pollFirst());
queue.addLast(res);
return res;
}
public boolean empty() {
return queue.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/