package stackqueue;
import java.util.Stack;
/**
* @Classname StackQueue
* @Description ç¨ä¸¤ä¸ªæ ï¼å®ç°éåçä»éå°¾æå
¥å
ç´ offer()
* åä»é头æåºå
ç´ poll()
* @Date 2019/7/2 21:26
* @Created by xww
*/
public class StackQueue {
private Stack stack1 = new Stack<>();
private Stack stack2 = new Stack<>();
public void offer(T data){
stack1.push(data);
}
public T poll(){
if(!stack2.isEmpty()){
return stack2.pop();
}else if(!stack1.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
return stack2.pop();
}
return null;
}
public static void main(String[] args){
StackQueue myQueue = new StackQueue<>();
System.out.println(myQueue.poll());
myQueue.offer(1);
myQueue.offer(2);
myQueue.offer(3);
System.out.println(myQueue.poll());
System.out.println(myQueue.poll());
myQueue.offer(4);
System.out.println(myQueue.poll());
System.out.println(myQueue.poll());
System.out.println(myQueue.poll());
}
}