mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-09 05:41:47 +08:00
Add kotlin code for the chapter of stack and queue (#1162)
* modified array.kt. * feat(kotlin): add kotlin code for the chapter of stack and queue. * modified array.kt * modified comments.
This commit is contained in:
39
codes/kotlin/chapter_stack_and_queue/stack.kt
Normal file
39
codes/kotlin/chapter_stack_and_queue/stack.kt
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* 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() {
|
||||
/* 初始化栈 */
|
||||
val stack = Stack<Int>()
|
||||
|
||||
/* 元素入栈 */
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
println("栈 stack = $stack")
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
val peek = stack.peek()
|
||||
println("栈顶元素 peek = $peek")
|
||||
|
||||
/* 元素出栈 */
|
||||
val pop = stack.pop()
|
||||
println("出栈元素 pop = $pop,出栈后 stack = $stack")
|
||||
|
||||
/* 获取栈的长度 */
|
||||
val size = stack.size
|
||||
println("栈的长度 size = $size")
|
||||
|
||||
/* 判断是否为空 */
|
||||
val isEmpty = stack.isEmpty()
|
||||
println("栈是否为空 = $isEmpty")
|
||||
}
|
||||
Reference in New Issue
Block a user