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<>();
        private T front;

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

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

        public T peek() {
            if (front != null) {
                return front;
            }
            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());
                }
            }
        }
    }
}

@@ -318,33 +318,8 @@
 %3E();
-%0A        private T front;
 %0A%0A  
@@ -395,34 +395,8 @@
 t);%0A
-            front = null;%0A
     
@@ -442,37 +442,34 @@
             
-while
+if
  (!stack
 1.isEmpty())
@@ -448,33 +448,33 @@
       if (!stack
-1
+2
 .isEmpty()) %7B%0A  
@@ -494,34 +494,21 @@
  stack2.
-push(stack1.
 pop()
-)
 ;%0A      
@@ -518,35 +518,22 @@
    %7D
+ else %7B
 %0A      
-      stack2.pop();%0A
     
@@ -534,36 +534,37 @@
             
-  if
+while
  (!stack
 2.isEmpty())
@@ -543,33 +543,33 @@
    while (!stack
-2
+1
 .isEmpty()) %7B%0A  
@@ -586,15 +586,11 @@
     
-front =
+   
  sta
@@ -598,29 +598,32 @@
 k2.p
-eek
+ush(stack1.pop
 ()
+)
 ;%0A     
-    
-   %7D%0A
     
@@ -633,36 +633,9 @@
     
- while (!stack2.isEmpty()) %7B
+%7D
 %0A   
@@ -643,36 +643,24 @@
             
-stack1.push(
 stack2.pop()
@@ -651,33 +651,32 @@
     stack2.pop()
-)
 ;%0A            %7D%0A
@@ -732,21 +732,25 @@
 if (
-front != null
+!stack2.isEmpty()
 ) %7B%0A
@@ -776,13 +776,21 @@
 urn 
-front
+stack2.peek()
 ;%0A  
@@ -792,33 +792,44 @@
 );%0A            %7D
-%0A
+ else %7B%0A    
             whil
@@ -860,32 +860,36 @@
                 
+    
 stack2.push(stac
@@ -903,32 +903,36 @@
 ));%0A            
+    
 %7D%0A            T 
@@ -933,127 +933,34 @@
     
-T val = stack2.peek();%0A            while (!stack2.isEmpty()) %7B%0A                stack1.push(stack2.pop());%0A            %7D
+    return stack2.peek();%0A
 %0A   
@@ -972,19 +972,9 @@
     
-return val;
+%7D
 %0A   

Solve A Tale of Two Stacks