package algorithms.queueusingstacks;

import java.util.*;

public class Solution {

  private static final int ENQUEUE = 1;
  private static final int DEQUEUE = 2;
  private static final int PRINT = 3;

  private static final String WHITE_SPACE = "\\s+";

  private static Stack<Integer> head;
  private static Stack<Integer> tail;

  private static Scanner scanner = new Scanner(System.in);

  public static void main(String[] args) {
    int queries = Integer.valueOf(scanner.nextLine());
    int[][] parsedQueries = new int[queries][];

    for (int i = 0; i < queries; i++) {
      final String[] query = scanner.nextLine().split(WHITE_SPACE);
      int[] parsedQuery = new int[2];
      parsedQuery[0] = Integer.valueOf(query[0]);

      if (query.length > 1) {
        parsedQuery[1] = Integer.valueOf(query[1]);
      }

      parsedQueries[i] = parsedQuery;
    }

    final int[] result = processQueries(parsedQueries);

    Arrays.stream(result).forEach(System.out::println);
  }

  static int[] processQueries(int[][] queries) {
    List<Integer> results = new ArrayList<>();
    head = new Stack<>();
    tail = new Stack<>();

    for (int[] queryPair : queries) {
      int command = queryPair[0];
      int argument = queryPair[1];

      switch (command) {
        case ENQUEUE:
          enqueue(argument);
          break;
        case DEQUEUE:
          dequeue();
          break;
        case PRINT:
          results.add(peek());
          break;
      }
    }

    return results.stream().mapToInt(Integer::valueOf).toArray();
  }

  private static int peek() {
    return head.peek();
  }

  private static void dequeue() {
    head.pop();
  }

  private static void enqueue(int argument) {
    while (!head.empty()) {
      tail.push(head.pop());
    }

    head.push(argument);

    while (!tail.empty()) {
      head.push(tail.pop());
    }
  }
}


@@ -1574,24 +1574,72 @@
 nt peek() %7B%0A
+    if (tail.isEmpty()) %7B%0A      shift();%0A    %7D%0A%0A
     return h
@@ -1637,20 +1637,20 @@
  return 
-head
+tail
 .peek();
@@ -1693,20 +1693,68 @@
 ) %7B%0A    
-head
+if (tail.isEmpty()) %7B%0A      shift();%0A    %7D%0A%0A    tail
 .pop();%0A
@@ -1784,28 +1784,14 @@
 oid 
-enqueue(int argument
+shift(
 ) %7B%0A
@@ -1807,17 +1807,19 @@
  (!head.
-e
+isE
 mpty()) 
@@ -1859,65 +1859,59 @@
   %7D%0A
-%0A
   
-  head.push(argument);%0A%0A    while (!tail.empty()
+%7D%0A%0A  private static void enqueue(int argument
 ) %7B%0A
-  
     
@@ -1924,26 +1924,18 @@
 ush(
-tail.pop());%0A    %7D
+argument);
 %0A  %7D

fix queues using two stacks