백준: 큐

김아무개·2023년 4월 7일
0

백준

목록 보기
11/17

내 코드

import java.io.*;
import java.util.LinkedList;

public class Main {
    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            LinkedList<String> q = new LinkedList<>();
            int n = Integer.parseInt(br.readLine());
            
            while (n-- > 0) {
                String line = br.readLine();
                
                if (line.contains("push")) {
                    q.offer(line.split(" ")[1]);
                    
                } else if ("pop".equals(line)) {
                    System.out.println(q.isEmpty() ? -1 : q.poll());
                    
                } else if ("size".equals(line)) {
                    System.out.println(q.size());
                    
                } else if ("empty".equals(line)) {
                    System.out.println(q.isEmpty() ? 1 : 0);
                    
                } else if ("front".equals(line)) {
                    System.out.println(q.isEmpty() ? -1 : q.getFirst());
                    
                } else if ("back".equals(line)) {
                    System.out.println(q.isEmpty() ? -1 : q.getLast());
                }
            }
        } catch (IOException e) {
            System.out.println("err 😭 " + e.getCause());
        }
    }
}

히히 😊

profile
Hello velog! 

0개의 댓글