##面试了 再来一个栈转队列
package com.test.scheme;
import java.util.Stack;
/**
* @Package: com.test.scheme
* @ClassName: StackToQueue
* @Description: 类描述
* @Author: bulingfeng
* @CreateDate: 2019/2/28/028 16:46
*/
public class StackToQueue<E> {
Stack<E> stack1=new Stack<>();
Stack<E> stack2=new Stack<>();
public E pop() throws Exception {
if (stack1.isEmpty())
throw new Exception("队列为空");
while (stack1.size()>1){
stack2.push(stack1.pop());
}
E e=stack1.pop();
while (stack2.size()>0){
stack1.push(stack2.pop());
}
return e;
}
public boolean push(E e){
stack1.push(e);
return true;
}
public static void main(String[] args) throws Exception {
StackToQueue<String> stack=new StackToQueue<>();
stack.push("1");
stack.push("2");
stack.push("3");
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}