mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 23:50:45 +08:00
Doc snippets for init_linked_list, init_binary_tree, stack_operations and
five list.md entries now reference marked regions of the demo programs
instead of copying code, so the book shows code that is compiled and run.
- Mark regions in the zh demo files and add the snippet comments that the
demos lacked, reusing texts that already have reviewed translations.
- Sync ru by hand, regenerate en/ja/ko from the reviewed dictionaries and
derive zh-hant with OpenCC.
- Convert 97 snippet blocks in six editions to [file]{..}-[region]{..}
references; init_list and the Rust stack block stay literal until their
comments have reviewed translations.
Co-authored-by: Claude Opus 5.5 <[email protected]>
48 lines
1.0 KiB
Dart
48 lines
1.0 KiB
Dart
/**
|
|
* File: stack.dart
|
|
* Created Time: 2023-03-27
|
|
* Author: liuyuxin ([email protected])
|
|
*/
|
|
|
|
void main() {
|
|
// [region]{init_stack}
|
|
/* 初始化栈 */
|
|
// Dart 没有内置的栈类,可以把 List 当作栈来使用
|
|
final List<int> stack = [];
|
|
// [endregion]{init_stack}
|
|
|
|
// [region]{push}
|
|
/* 元素入栈 */
|
|
stack.add(1);
|
|
stack.add(3);
|
|
stack.add(2);
|
|
stack.add(5);
|
|
stack.add(4);
|
|
// [endregion]{push}
|
|
print("栈 stack = $stack");
|
|
|
|
// [region]{peek}
|
|
/* 访问栈顶元素 */
|
|
final int peek = stack.last;
|
|
// [endregion]{peek}
|
|
print("栈顶元素 peek = $peek");
|
|
|
|
// [region]{pop}
|
|
/* 元素出栈 */
|
|
final int pop = stack.removeLast();
|
|
// [endregion]{pop}
|
|
print("出栈元素 pop = $pop ,出栈后 stack = $stack");
|
|
|
|
// [region]{size}
|
|
/* 获取栈的长度 */
|
|
final int size = stack.length;
|
|
// [endregion]{size}
|
|
print("栈的长度 size = $size");
|
|
|
|
// [region]{is_empty}
|
|
/* 判断是否为空 */
|
|
final bool isEmpty = stack.isEmpty;
|
|
// [endregion]{is_empty}
|
|
print("栈是否为空 = $isEmpty");
|
|
}
|