요깨비's LAB

[백준, 자료구조, Kotlin] P.10845 큐 본문

알고리즘(Kotlin)/자료구조

[백준, 자료구조, Kotlin] P.10845 큐

요깨비 2021. 6. 2. 18:49

 

import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))

    val N = br.readLine().toInt()
    val queue = LinkedList<Int>()
    val sb = StringBuffer()

    for (i in 0 until N) {
        val command = br.readLine().split(" ")

        when {
            command[0] == "push" -> {
                queue.add(command[1].toInt())
            }
            command[0] == "pop" -> {
                if (queue.isEmpty()) {
                    sb.append("-1\n")
                } else {
                    sb.append("${queue.poll()}\n")
                }
            }
            command[0] == "size" -> {
                sb.append("${queue.size}\n")
            }
            command[0] == "empty" -> {
                if (queue.isEmpty()) {
                    sb.append("1\n")
                } else {
                    sb.append("0\n")
                }
            }
            command[0] == "front" -> {
                if (queue.isEmpty()) {
                    sb.append("-1\n")
                } else {
                    sb.append("${queue.first}\n")
                }
            }
            command[0] == "back" -> {
                if (queue.isEmpty()) {
                    sb.append("-1\n")
                } else {
                    sb.append("${queue.last}\n")
                }
            }
        }
    }

    print(sb.toString())
}
Comments