Computer Science/알고리즘

백준 Java | BOJ 10845 큐

토마토. 2023. 2. 1. 14:53
package DAY03.P10845;

import jdk.internal.util.xml.impl.Input;

import java.io.*;
import java.nio.Buffer;
import java.util.*;

public class Main {
    public static int N;
    public static  Deque<Integer> queue;
    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/DAY03/P10845/input.txt"));
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(bf.readLine());
        queue = new LinkedList<>();
        // bf.readLine();
        for (int i=0;i<N;i++){
            String command = bf.readLine();
            // push front back
            if (command.equals("pop")){
                if (queue.isEmpty()){
                    System.out.println(-1);
                } else {
                    System.out.println(queue.poll());
                }
            } else if (command.equals("size")){
                System.out.println(queue.size());
            } else if (command.equals("empty")){
                boolean result = queue.isEmpty();
                if (result){
                    System.out.println(1);
                } else {
                    System.out.println(0);
                }
            } else if (command.equals("front")) {
                if (queue.isEmpty()){
                    System.out.println(-1);
                } else {
                    System.out.println(queue.peek());
                }
            } else if (command.equals("back")) {
                if (queue.isEmpty()){
                    System.out.println(-1);
                } else {
                    System.out.println(queue.getLast());
                }
            } else {
                // split by " "
                String[] command_with_param = command.split(" ");
                String command_str = command_with_param[0];
                int param = Integer.parseInt(command_with_param[1]);
                queue.add(param);
            }
        }
    }
}