06 由两个栈组成的队列
Table of Contents

ac

import java.util.Scanner;
import java.util.Stack;

/**
 * 1
 * 1 2 , [2 1
 *
 * 2, 1]
 *
 * 1 2 3 , [3 2 1
 */
class MyQueue{
    Stack<Integer> sta = new Stack<>();
    Stack<Integer> staTmp = new Stack<>();

    void add(int val){
        if(sta.isEmpty()){
            sta.push(val);
        }else {
            while (!sta.isEmpty()){
                Integer peekVal = sta.pop();
                staTmp.push(peekVal);
            }
            sta.push(val);
            while (!staTmp.isEmpty()){
                Integer peekVal = staTmp.pop();
                sta.push(peekVal);
            }
        }
    }

    Integer peek(){
        return sta.peek();
    }

    void poll(){
        if(sta.isEmpty()){
            return;
        }
        sta.pop();
    }

}

public class Main {

    public static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int n = sc.nextInt();
        MyQueue que = new MyQueue();
        for(int i=0;i<n;i++){
            String s = sc.next();
            if(s.equals("add")){
                int val = sc.nextInt();
                que.add(val);
            }else if(s.equals("peek")){
                int minVal = que.peek();
                System.out.println(minVal);
            }else if(s.equals("poll")){
                que.poll();
            }
        }
    }

}