Files
hello-algo/en/codes/kotlin/chapter_stack_and_queue/stack.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
853 B
Kotlin

/**
* File: stack.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 stack */
val stack = Stack<Int>()
/* Push elements onto the stack */
stack.push(1)
stack.push(3)
stack.push(2)
stack.push(5)
stack.push(4)
println("Stack stack = $stack")
/* Access the top element */
val peek = stack.peek()
println("Top element: peek = $peek")
/* Pop an element from the stack */
val pop = stack.pop()
println("Popped element: pop = $pop; stack after removal = $stack")
/* Get the stack size */
val size = stack.size
println("Stack size = $size")
/* Check whether it is empty */
val isEmpty = stack.isEmpty()
println("Is the stack empty? $isEmpty")
}