mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 23:50:45 +08:00
Migrate init_array, hash_map_operations, hash_map_traversal, queue_operations, deque_operations and insert_remove_node: zh and ru demo sources gain region markers and the snippets' comments, en, ja and ko are regenerated from reviewed translations, zh-hant via OpenCC. 70 blocks become region references; builtin_hash and five blocks without reviewed translations stay literal. The Java, Kotlin and Rust deque demos follow the book's order, and a few demo comments and typos are aligned with the snippets. Co-Authored-By: Claude Opus 5.5 <[email protected]>
50 lines
1.1 KiB
Dart
50 lines
1.1 KiB
Dart
/**
|
|
* File: queue.dart
|
|
* Created Time: 2023-03-28
|
|
* Author: liuyuxin ([email protected])
|
|
*/
|
|
|
|
import 'dart:collection';
|
|
|
|
void main() {
|
|
// [region]{init_queue}
|
|
/* 初始化队列 */
|
|
// 在 Dart 中,一般将双向队列类 Queue 看作队列使用
|
|
final Queue<int> queue = Queue();
|
|
// [endregion]{init_queue}
|
|
|
|
// [region]{push}
|
|
/* 元素入队 */
|
|
queue.add(1);
|
|
queue.add(3);
|
|
queue.add(2);
|
|
queue.add(5);
|
|
queue.add(4);
|
|
// [endregion]{push}
|
|
print("队列 queue = $queue");
|
|
|
|
// [region]{peek}
|
|
/* 访问队首元素 */
|
|
final int peek = queue.first;
|
|
// [endregion]{peek}
|
|
print("队首元素 peek = $peek");
|
|
|
|
// [region]{pop}
|
|
/* 元素出队 */
|
|
final int pop = queue.removeFirst();
|
|
// [endregion]{pop}
|
|
print("出队元素 pop = $pop ,出队后 queue = $queue");
|
|
|
|
// [region]{size}
|
|
/* 获取队列长度 */
|
|
final int size = queue.length;
|
|
// [endregion]{size}
|
|
print("队列长度 size = $size");
|
|
|
|
// [region]{is_empty}
|
|
/* 判断队列是否为空 */
|
|
final bool isEmpty = queue.isEmpty;
|
|
// [endregion]{is_empty}
|
|
print("队列是否为空 = $isEmpty");
|
|
}
|