mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 04:33:09 +08:00
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
39 lines
853 B
Kotlin
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")
|
|
} |