mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-30 00:04:44 +08:00
Cite demo-source regions from doc snippets (pilot) (#2034)
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 <noreply@anthropic.com>
This commit is contained in:
@@ -65,18 +65,21 @@ pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T) -> i32 {
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* 初始化链表 */
|
||||
// [region]{init_linked_list}
|
||||
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
||||
// 初始化各个节点
|
||||
let n0 = ListNode::new(1);
|
||||
let n1 = ListNode::new(3);
|
||||
let n2 = ListNode::new(2);
|
||||
let n3 = ListNode::new(5);
|
||||
let n4 = ListNode::new(4);
|
||||
|
||||
// 构建节点之间的引用
|
||||
n0.borrow_mut().next = Some(n1.clone());
|
||||
n1.borrow_mut().next = Some(n2.clone());
|
||||
n2.borrow_mut().next = Some(n3.clone());
|
||||
n3.borrow_mut().next = Some(n4.clone());
|
||||
// [endregion]{init_linked_list}
|
||||
print!("初始化的链表为 ");
|
||||
print_util::print_linked_list(&n0);
|
||||
|
||||
|
||||
@@ -12,60 +12,79 @@ fn main() {
|
||||
print!("列表 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// [region]{access_element}
|
||||
// 访问元素
|
||||
let num = nums[1];
|
||||
let num = nums[1]; // 访问索引 1 处的元素
|
||||
// [endregion]{access_element}
|
||||
println!("\n访问索引 1 处的元素,得到 num = {num}");
|
||||
|
||||
// [region]{update_element}
|
||||
// 更新元素
|
||||
nums[1] = 0;
|
||||
nums[1] = 0; // 将索引 1 处的元素更新为 0
|
||||
// [endregion]{update_element}
|
||||
print!("将索引 1 处的元素更新为 0 ,得到 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// [region]{clear_list}
|
||||
// 清空列表
|
||||
nums.clear();
|
||||
// [endregion]{clear_list}
|
||||
print!("\n清空列表后 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// [region]{append_element}
|
||||
// 在尾部添加元素
|
||||
nums.push(1);
|
||||
nums.push(3);
|
||||
nums.push(2);
|
||||
nums.push(5);
|
||||
nums.push(4);
|
||||
// [endregion]{append_element}
|
||||
print!("\n添加元素后 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// [region]{insert_element}
|
||||
// 在中间插入元素
|
||||
nums.insert(3, 6);
|
||||
nums.insert(3, 6); // 在索引 3 处插入数字 6
|
||||
// [endregion]{insert_element}
|
||||
print!("\n在索引 3 处插入数字 6 ,得到 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// [region]{remove_element}
|
||||
// 删除元素
|
||||
nums.remove(3);
|
||||
nums.remove(3); // 删除索引 3 处的元素
|
||||
// [endregion]{remove_element}
|
||||
print!("\n删除索引 3 处的元素,得到 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// [region]{iterate_list}
|
||||
// 通过索引遍历列表
|
||||
let mut _count = 0;
|
||||
for i in 0..nums.len() {
|
||||
_count += nums[i];
|
||||
}
|
||||
|
||||
// 直接遍历列表元素
|
||||
_count = 0;
|
||||
for x in &nums {
|
||||
_count += x;
|
||||
}
|
||||
// [endregion]{iterate_list}
|
||||
|
||||
// [region]{concat_list}
|
||||
// 拼接两个列表
|
||||
let mut nums1 = vec![6, 8, 7, 10, 9];
|
||||
nums.append(&mut nums1); // append(移动) 之后 nums1 为空!
|
||||
// [endregion]{concat_list}
|
||||
|
||||
// nums.extend(&nums1); // extend(借用) nums1 能继续使用
|
||||
print!("\n将列表 nums1 拼接到 nums 之后,得到 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// [region]{sort_list}
|
||||
// 排序列表
|
||||
nums.sort();
|
||||
nums.sort(); // 排序后,列表元素从小到大排列
|
||||
// [endregion]{sort_list}
|
||||
print!("\n排序列表后 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use hello_algo_rust::include::{print_util, TreeNode};
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
// [region]{init_binary_tree}
|
||||
/* 初始化二叉树 */
|
||||
// 初始化节点
|
||||
let n1 = TreeNode::new(1);
|
||||
@@ -20,6 +21,7 @@ fn main() {
|
||||
n1.borrow_mut().right = Some(Rc::clone(&n3));
|
||||
n2.borrow_mut().left = Some(Rc::clone(&n4));
|
||||
n2.borrow_mut().right = Some(Rc::clone(&n5));
|
||||
// [endregion]{init_binary_tree}
|
||||
println!("\n初始化二叉树\n");
|
||||
print_util::print_tree(&n1);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user