题解 | #【模板】队列#

【模板】队列

https://www.nowcoder.com/practice/afe812c80ad946f4b292a26dd13ba549

import java.util.Scanner

fun main(args: Array<String>) {
    val read = Scanner(System.`in`)
    val size = read.nextLine().toInt()
    val queueAb7 = QueueAb7(size)
    while (read.hasNextLine()) {
        val lineArr = read.nextLine().split(" ")
        if (lineArr[0] == "push") {
            queueAb7.push(lineArr[1].toInt())
        } else if (lineArr[0] == "pop") {
            queueAb7.pop()
        } else {
            queueAb7.front()
        }
    }
}

class QueueAb7(size: Int) {
    private lateinit var tempArray: IntArray
    private var head = 0
    private var tail = 0

    init {
        if (size in 1..100000) {
            tempArray = IntArray(size)
        } else {
            println("error")
        }
    }

    fun push(value: Int) {
        if (tail <= tempArray.size) {
            tempArray[tail++] = value
        } else {
            println("error")
        }
    }

    fun pop() {
        if (tail in (head + 1)..tempArray.size) {
            println(tempArray[head++])
        } else {
            println("error")
        }
    }

    fun front() {
        if (tail in (head + 1)..tempArray.size) {
            println(tempArray[head])
        } else {
            println("error")
        }
    }
}

全部评论

相关推荐

Elastic90:公司不要求加班,但 又不允许你准点下班,经典又当又立
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务