본문 바로가기
알고리즘/백준

백준 10845번 - 큐 (Java 8)

by latissimus 2022. 3. 10.

https://www.acmicpc.net/problem/10845

 

10845번: 큐

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

아무 생각 없이 LinkedList 사용

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Main {
    static LinkedList<Integer> queue = new LinkedList();
    static StringTokenizer st;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int numOfCase = Integer.parseInt(br.readLine());
        for(int i=0; i<numOfCase; i++){
            st = new StringTokenizer(br.readLine());
            readCommand(st.nextToken());
        }
    }
    public static void readCommand(String input){
        if (input.equals("push")) {
            int X = Integer.parseInt(st.nextToken());
            push(X);
        }
        if (input.equals("pop")) {
            pop();
        }
        if (input.equals("size")) {
            size();
        }
        if (input.equals("empty")) {
            empty();
        }
        if (input.equals("front")) {
            front();
        }
        if (input.equals("back")) {
            back();
        }
    }
    public static void push(int X){
        queue.add(X);
    }
    public static void pop(){
        if(queue.size() == 0){
            System.out.println(-1);
        } else{
            System.out.println(queue.get(0));
            queue.remove(queue.get(0));
        }
    }
    public static void size(){
        System.out.println(queue.size());
    }
    public static void empty(){
        if(queue.size() == 0){
            System.out.println(1);
        } else{
            System.out.println(0);
        }
    }
    public static void front(){
        if(queue.size() == 0){
            System.out.println(-1);
        } else{
            System.out.println(queue.get(0));
        }
    }
    public static void back(){
        if(queue.size() == 0){
            System.out.println(-1);
        } else{
            System.out.println(queue.get(queue.size()-1));
        }
    }
}

 

큐 사용

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    static Queue<Integer> queue = new LinkedList<>();
    static StringTokenizer st;
    static int X;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int numOfCase = Integer.parseInt(br.readLine());
        for(int i=0; i<numOfCase; i++){
            st = new StringTokenizer(br.readLine());
            readCommand(st.nextToken());
        }
    }
    public static void readCommand(String input){
        if (input.equals("push")) {
            X = Integer.parseInt(st.nextToken());
            push(X);
        }
        if (input.equals("pop")) {
            pop();
        }
        if (input.equals("size")) {
            size();
        }
        if (input.equals("empty")) {
            empty();
        }
        if (input.equals("front")) {
            front();
        }
        if (input.equals("back")) {
            back();
        }
    }
    public static void push(int X){
        queue.offer(X);
    }
    public static void pop(){
        if(queue.size() == 0){
            System.out.println(-1);
        } else{
            System.out.println(queue.poll());
        }
    }
    public static void size(){
        System.out.println(queue.size());
    }
    public static void empty(){
        if(queue.size() == 0){
            System.out.println(1);
        } else{
            System.out.println(0);
        }
    }
    public static void front(){
        if(queue.size() == 0){
            System.out.println(-1);
        } else{
            System.out.println(queue.peek());
        }
    }
    public static void back(){
        if(queue.size() == 0){
            System.out.println(-1);
        } else{
            System.out.println(X);
        }
    }
}

 

언젠가 대량 리팩토링을 해야할 것 같다.

'알고리즘 > 백준' 카테고리의 다른 글

백준 1406번 - 에디터 (Java 8)  (0) 2022.03.11
백준 10866번 - 덱 (Java 8)  (0) 2022.03.11
백준 1158번 - 요세푸스 (Java 8)  (0) 2022.03.10
백준 1874번 - 스택 수열 (Java 8)  (0) 2022.03.09
백준 9012번 - 괄호 (Java 8)  (0) 2022.03.08

댓글