用栈实现队列
1, 用两个栈实现队列,
2, Queue<Integer> queue=new LinkedList<>();
3, push时, 直接queueIn.offer(x);
public int pop() { if(queueOut.size()!=0){ return queueOut.poll(); } while(queueIn.size()!=0){ queueOut.offer(queueIn.poll()); } return queueOut.poll(); }
public boolean empty() {
return queueIn.isEmpty()&&queueOut.isEmpty();
}
用队列实现栈
1, 用一个栈实现队列
2, Queue<Integer> queue = new LinkedList<>();
3, push时,
queue.offer(x); int size=queue.size(); for(int i =1;i<size;i++){ //最后一位不用poll() int temp = queue.poll(); queue.offer(temp); }
标签:10,队列,queueIn,随想录,queue,int,queueOut,打卡,size From: https://www.cnblogs.com/Liu5419/p/17153566.html