Files
hello-algo/en/codes/kotlin/chapter_stack_and_queue/queue.kt
Yudong Jin 14c314c08c Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
2026-10-31 03:35:51 +08:00

39 lines
854 B
Kotlin

/**
* File: queue.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_stack_and_queue
import java.util.*
/* Driver code */
fun main() {
/* Initialize the queue */
val queue = LinkedList<Int>()
/* Enqueue elements */
queue.offer(1)
queue.offer(3)
queue.offer(2)
queue.offer(5)
queue.offer(4)
println("Queue queue = $queue")
/* Access the front element */
val peek = queue.peek()
println("Front element: peek = $peek")
/* Dequeue an element */
val pop = queue.poll()
println("Dequeued element: pop = $pop; queue after removal = $queue")
/* Get the queue size */
val size = queue.size
println("Queue size = $size")
/* Check whether the queue is empty */
val isEmpty = queue.isEmpty()
println("Is the queue empty? $isEmpty")
}