package hackerrank.ctci;

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

/**
 * https://www.hackerrank.com/challenges/ctci-queue-using-two-stacks
 */
public class ATaleOfTwoStacks {
    private static class MyQueue<T> {
        private Stack<T> stack1 = new Stack<>();
        private Stack<T> stack2 = new Stack<>();

        public void enqueue(T element) {
            stack1.push(element);
        }

        public void dequeue() {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
            stack2.pop();
            while (!stack2.isEmpty()) {
                stack1.push(stack2.pop());
            }
        }

        public T peek() {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
            T val = stack2.peek();
            while (!stack2.isEmpty()) {
                stack1.push(stack2.pop());
            }
            return val;
        }
    }

    public static void main(String[] args) {
        MyQueue<Integer> queue = new MyQueue<>();
        try (Scanner scan = new Scanner(System.in)) {
            int n = scan.nextInt();
            for (int i = 0; i < n; i++) {
                int operation = scan.nextInt();
                if (operation == 1) { // enqueue
                    queue.enqueue(scan.nextInt());
                } else if (operation == 2) { // dequeue
                    queue.dequeue();
                } else if (operation == 3) { // print/peek
                    System.out.println(queue.peek());
                }
            }
        }
    }
}

@@ -314,16 +314,41 @@
 ack%3C%3E();
+%0A        private T front;
 %0A%0A      
@@ -416,16 +416,42 @@
 ement);%0A
+            front = null;%0A
         
@@ -604,24 +604,114 @@
 ack2.pop();%0A
+            if (!stack2.isEmpty()) %7B%0A                front = stack2.peek();%0A            %7D%0A
             
@@ -798,32 +798,32 @@
    %7D%0A        %7D%0A%0A
-
         public T
@@ -828,24 +828,101 @@
  T peek() %7B%0A
+            if (front != null) %7B%0A                return front;%0A            %7D%0A
             

Update Tale of Two Stacks