mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-03 10:53:35 +08:00
Add kotlin code for chapter_stack_and_queue and chapter_tree (#1197)
* Add kotlin code block for chapter_hashing * Add kotlin code block for chapter_heap. * Add kotlin code block for chapter_stack_and_queue and chapter_tree * fix indentation * Update binary_tree.md --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -327,7 +327,29 @@
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="deque.kt"
|
||||
|
||||
/* 初始化双向队列 */
|
||||
val deque = LinkedList<Int>()
|
||||
|
||||
/* 元素入队 */
|
||||
deque.offerLast(2) // 添加至队尾
|
||||
deque.offerLast(5)
|
||||
deque.offerLast(4)
|
||||
deque.offerFirst(3) // 添加至队首
|
||||
deque.offerFirst(1)
|
||||
|
||||
/* 访问元素 */
|
||||
val peekFirst = deque.peekFirst() // 队首元素
|
||||
val peekLast = deque.peekLast() // 队尾元素
|
||||
|
||||
/* 元素出队 */
|
||||
val popFirst = deque.pollFirst() // 队首元素出队
|
||||
val popLast = deque.pollLast() // 队尾元素出队
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
val size = deque.size
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
val isEmpty = deque.isEmpty()
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
Reference in New Issue
Block a user