用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )

示例 1:

输入:
["CQueue","appendTail","deleteHead","deleteHead","deleteHead"]
[[],[3],[],[],[]]
输出:[null,null,3,-1,-1]

示例 2:

输入:
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
输出:[null,-1,null,null,5,2]

提示:

import java.util.*;
class CQueue {
    private Stack<Integer> stack;
    private Stack<Integer> result;
    public CQueue() {
        stack = new Stack();
        result = new Stack();
    }
   
    public void appendTail(int value) {
        //当result为空,stack 先储存数据,在pop 到result中
        if(result.empty()){
            stack.push(value);
            while(!stack.empty()){
            result.push(stack.pop());
        }
        }
        else{
            //当result不为空,先clear stack 在将result中存有的数据pop 并重新push 到第一个stack
            // recall 这个function,使用之前的if 重新添加data
            stack.clear();
            while(!result.empty()){
                stack.push(result.pop());
            }
            appendTail(value);
        }
    }
    /*
    delete 直接从result中delete head node, 初始Stack 不做改变
    */
    public int deleteHead() {
        if(result.empty()){
            return -1;
        }
        if(result.peek()!=null){
            int value = result.pop();
            return value;
        }
        else{
            return -1;
        }
}
}