• + 0 comments

    For Java 7.

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

    class Queue { Stack getIn = new Stack<>(); Stack getOut = new Stack<>(); int size = 0;

    Queue() {}
    
    public int size() {
        return size;
        // This cause i update size when enqueue and dequeue already
        // Of course, you can just do:  getIn.size() + getOut.size()
    }
    
    public boolean isEmpty() {
        return size == 0;
    }
    
    public void Enqueue(int value) {
        getIn.add(value);
        size++;
    
        // First, item will be contain in getIn. More detail on Dequeue.
    }
    
    public int Dequeue() {
        if (getOut.size() == 0) {
            int n = getIn.size();
            for (; n > 0; n--) {
                getOut.add(getIn.pop());
            }
        }
        size--;
        return getOut.pop();
    
        // If getOut is empty, we can take nothing out from it. So, check for more item from getIn
        // Notice: never push from getIn to getOut while getOut is not Empty. It'll break the order
    }
    
    public int peek() {
        if (getOut.size() == 0) {
            int n = getIn.size();
            for (; n > 0; n--) {
                getOut.add(getIn.pop());
            }
        }
        return getOut.peek();
    
        // Similar to dequeue
    }
    

    }

    public class Solution { public final static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int q = sc.nextInt();
    
        int ops;
        int newValue;
        Queue queue = new Queue();
        for (; q>0; q--) {
            ops = sc.nextInt();
            switch(ops) {
                case 1: // Enqueue
                    newValue = sc.nextInt();
                    queue.Enqueue(newValue);
                    break;
                case 2: // Dequeue
                    queue.Dequeue();
                    break;
                case 3: // peek and print
                    System.out.println(queue.peek());
                    break;
                default:
            }
        }
    
        sc.close();
    }