알고리즘공부(Algorithm Study)/문제풀이(ProblemSolving)

[자료구조/큐] 백준 18258 큐2

Chann._.y 2022. 12. 18.
728x90

 

package 큐덱;


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

/**
 * push
 * pop
 * size
 * empty
 * front
 * back
 * 명령 수 1 <= N <= 2,000,000
 * 주어지는 정수 1 <= M <= 100,000
 */
public class boj_18258_큐2 {
    private static int N, frontCursor, rearCursor, queue[];
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer stz;
        N = Integer.parseInt(br.readLine());
        queue = new int[N];
        frontCursor = 0;
        rearCursor = 0;
        String command = "";
        int value = 0;
        StringBuilder sb = new StringBuilder();

        // 명령어, 숫자 입력 받기
        for (int i = 0; i < N; i++) {
            stz = new StringTokenizer(br.readLine());
            command = stz.nextToken();
            if (command.equals("push")) {
                value = Integer.parseInt(stz.nextToken());
                push(value);
            } else if(command.equals("pop")) {
            	sb.append(pop()).append("\n");
            } else if(command.equals("size")) {
            	sb.append(size()).append("\n");
            } else if(command.equals("empty")) {
            	sb.append(empty()).append("\n");
            } else if(command.equals("front")) {
            	sb.append(front()).append("\n");
            } else {
            	sb.append(back()).append("\n");
            }
        }

        System.out.println(sb.toString());
    }

    private static int back() {
        return empty() == 1 ? -1 : queue[rearCursor - 1];
    }

    private static int front() {
        return empty() == 1 ? -1 : queue[frontCursor];
    }

    private static int empty() {
        return size() == 0 ? 1 : 0;
    }

    private static int size() {
        return rearCursor - frontCursor;
    }

    private static int pop() {
        return empty() == 1 ? -1 : queue[frontCursor++];
    }

    private static void push(int value) {
        queue[rearCursor++] = value;
    }
}
package 큐덱;


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

/**
 * push
 * pop
 * size
 * empty
 * front
 * back
 * 명령 수 1 <= N <= 2,000,000
 * 주어지는 정수 1 <= M <= 100,000
 */
public class boj_18258_큐2 {
    private static int N, frontCursor, rearCursor, queue[];
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer stz;
        N = Integer.parseInt(br.readLine());
        queue = new int[N];
        frontCursor = 0;
        rearCursor = 0;
        String command = "";
        int value = 0;
        StringBuilder sb = new StringBuilder();

        // 명령어, 숫자 입력 받기
        for (int i = 0; i < N; i++) {
            stz = new StringTokenizer(br.readLine());
            command = stz.nextToken();
            if (command.equals("push")) {
                value = Integer.parseInt(stz.nextToken());
                push(value);
            } else if(command.equals("pop")) {
            	sb.append(pop()).append("\n");
            } else if(command.equals("size")) {
            	sb.append(size()).append("\n");
            } else if(command.equals("empty")) {
            	sb.append(empty()).append("\n");
            } else if(command.equals("front")) {
            	sb.append(front()).append("\n");
            } else {
            	sb.append(back()).append("\n");
            }
        }

        System.out.println(sb.toString());
    }

    private static int back() {
        if (empty() == 1) {
            return -1;
        }
        return queue[rearCursor - 1];
    }

    private static int front() {
        if (empty() == 1) {
            return -1;
        }
        return queue[frontCursor];
    }

    private static int empty() {
        if(size() == 0) {
            return 1;
        }
        return 0;
    }

    private static int size() {
        return rearCursor - frontCursor;
    }

    private static int pop() {
        if(empty() ==  1) {
            return -1;
        }
        return queue[frontCursor++];
    }

    private static void push(int value) {
        queue[rearCursor++] = value;
    }
}

해당 문제는 자료구조 중 큐를 직접 구현하는 문제였다.

삼항 연산자 사용하지 않은 버전과 사용한 버전으로 풀어보았다.

 

 

728x90

댓글