[백준] 18258 큐2 자바

이다혜·2023년 11월 19일
0

백준

목록 보기
2/29
post-thumbnail

📎 문제 출처


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

📌 문제 설명


정수를 저장하는 큐를 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.

명령은 총 여섯 가지이다.

push X: 정수 X를 큐에 넣는 연산이다.
pop: 큐에서 가장 앞에 있는 정수를 빼고, 그 수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
size: 큐에 들어있는 정수의 개수를 출력한다.
empty: 큐가 비어있으면 1, 아니면 0을 출력한다.
front: 큐의 가장 앞에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
back: 큐의 가장 뒤에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.

📌 Code1


처음에는 Queue인터페이스를 사용하려고 했는데 Queue 인터페이스에는 getLast() 메서드가 없어서 LinkedList를 사용했다.

package com.ll;

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        LinkedList<Integer> queue = new LinkedList<>();
        int N = Integer.parseInt(br.readLine());
        StringTokenizer st;
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            switch(st.nextToken()) {
                case "push" : queue.add(Integer.parseInt(st.nextToken())); break;
                case "pop" : sb.append(!queue.isEmpty() ? queue.poll() : -1).append("\n"); break;
                case "size" : sb.append(queue.size()).append("\n"); break;
                case "empty" : sb.append(queue.isEmpty() ? 1 : 0).append("\n"); break;
                case "front" : sb.append(!queue.isEmpty() ? queue.peek() : -1).append("\n"); break;
                case "back" : sb.append(!queue.isEmpty() ? queue.getLast() : -1).append("\n");
            }
        }
        System.out.println(sb.toString());
    }

}

📌 Code2


이번에는 배열로 큐를 구현했다.
배열의 가장 앞을 front, 가장 뒤를 rear로 하고 배열에 값을 추가할 때마다 rear를 뒤로 1 이동시키고 배열을 값을 앞에서 제거할 때마다 front를 1만큼 뒤로 이동했다.

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

public class Main {
    static BufferedReader br;
    static StringBuilder sb;
    static int[] queue = new int[2000000];
    static int front = 0;
    static int rear = 0;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        StringTokenizer st;
        sb = new StringBuilder();

        for(int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            switch(st.nextToken()) {
                case "push" : push(Integer.parseInt(st.nextToken()));break;
                case "pop" : pop();break;
                case "size" : size(); break;
                case "empty" : empty(); break;
                case "front" : front(); break;
                case "back" : back();
            }
        }
        System.out.println(sb.toString());
    }

    static void push(int n) {
        queue[rear++] = n;
    }
    static void pop() {
        if(rear == front) sb.append(-1);
        else sb.append(queue[front++]);
        sb.append("\n");
    }
    static void size() {
        sb.append(rear - front).append("\n");
    }
    static void empty() {
        if(front == rear) sb.append(1);
        else sb.append(0);
        sb.append("\n");
    }
    static void front() {
        if(front == rear) sb.append(-1);
        else sb.append(queue[front]);
        sb.append("\n");
    }
    static void back() {
        if(front == rear) sb.append(-1);
        else sb.append(queue[rear-1]);
        sb.append("\n");
    }

}

0개의 댓글