mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-03 05:27:55 +08:00
Release Rust code to documents. (#656)
This commit is contained in:
@ -13,9 +13,9 @@ use list_node::ListNode;
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
#[allow(non_snake_case)]
|
||||
pub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {
|
||||
let n1 = n0.borrow_mut().next.take();
|
||||
P.borrow_mut().next = n1;
|
||||
n0.borrow_mut().next = Some(P);
|
||||
let n1 = n0.borrow_mut().next.take();
|
||||
P.borrow_mut().next = n1;
|
||||
n0.borrow_mut().next = Some(P);
|
||||
}
|
||||
|
||||
/* 删除链表的节点 n0 之后的首个节点 */
|
||||
@ -28,7 +28,7 @@ pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
|
||||
let n1 = node.borrow_mut().next.take();
|
||||
n0.borrow_mut().next = n1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 访问链表中索引为 index 的节点 */
|
||||
pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {
|
||||
@ -37,7 +37,7 @@ pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListN
|
||||
return access(node.clone(), index - 1);
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
||||
|
||||
/* 在链表中查找值为 target 的首个节点 */
|
||||
pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {
|
||||
@ -46,7 +46,7 @@ pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32)
|
||||
return find(node.clone(), target, index + 1);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
@ -74,7 +74,7 @@ fn main() {
|
||||
remove(&n0);
|
||||
print!("删除节点后的链表为 ");
|
||||
print_util::print_linked_list(&n0);
|
||||
|
||||
|
||||
/* 访问节点 */
|
||||
let node = access(n0.clone(), 3);
|
||||
println!("链表中索引 3 处的节点的值 = {}", node.borrow().val);
|
||||
|
||||
@ -4,72 +4,72 @@
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
include!("../include/include.rs");
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
// 初始化列表
|
||||
let mut list: Vec<i32> = vec![ 1, 3, 2, 5, 4 ];
|
||||
print!("列表 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 访问元素
|
||||
let num = list[1];
|
||||
println!("\n访问索引 1 处的元素,得到 num = {num}");
|
||||
|
||||
// 更新元素
|
||||
list[1] = 0;
|
||||
print!("将索引 1 处的元素更新为 0 ,得到 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 清空列表
|
||||
list.clear();
|
||||
print!("\n清空列表后 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 尾部添加元素
|
||||
list.push(1);
|
||||
list.push(3);
|
||||
list.push(2);
|
||||
list.push(5);
|
||||
list.push(4);
|
||||
print!("\n添加元素后 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 中间插入元素
|
||||
list.insert(3, 6);
|
||||
print!("\n在索引 3 处插入数字 6 ,得到 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 删除元素
|
||||
list.remove(3);
|
||||
print!("\n删除索引 3 处的元素,得到 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 通过索引遍历列表
|
||||
let mut _count = 0;
|
||||
for _ in 0..list.len() {
|
||||
_count += 1;
|
||||
}
|
||||
|
||||
// 直接遍历列表元素
|
||||
_count = 0;
|
||||
for _n in &list {
|
||||
_count += 1;
|
||||
}
|
||||
// 或者
|
||||
// list.iter().for_each(|_| _count += 1);
|
||||
// let _count = list.iter().fold(0, |_count, _| _count + 1);
|
||||
|
||||
// 拼接两个列表
|
||||
let mut list1 = vec![ 6, 8, 7, 10, 9 ];
|
||||
list.append(&mut list1); // append(移动) 之后 list1 为空!
|
||||
// list.extend(&list1); // extend(借用) list1 能继续使用
|
||||
print!("\n将列表 list1 拼接到 list 之后,得到 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 排序列表
|
||||
list.sort();
|
||||
print!("\n排序列表后 list = ");
|
||||
print_util::print_array(&list);
|
||||
}
|
||||
fn main() {
|
||||
// 初始化列表
|
||||
let mut list: Vec<i32> = vec![ 1, 3, 2, 5, 4 ];
|
||||
print!("列表 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 访问元素
|
||||
let num = list[1];
|
||||
println!("\n访问索引 1 处的元素,得到 num = {num}");
|
||||
|
||||
// 更新元素
|
||||
list[1] = 0;
|
||||
print!("将索引 1 处的元素更新为 0 ,得到 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 清空列表
|
||||
list.clear();
|
||||
print!("\n清空列表后 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 尾部添加元素
|
||||
list.push(1);
|
||||
list.push(3);
|
||||
list.push(2);
|
||||
list.push(5);
|
||||
list.push(4);
|
||||
print!("\n添加元素后 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 中间插入元素
|
||||
list.insert(3, 6);
|
||||
print!("\n在索引 3 处插入数字 6 ,得到 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 删除元素
|
||||
list.remove(3);
|
||||
print!("\n删除索引 3 处的元素,得到 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 通过索引遍历列表
|
||||
let mut _count = 0;
|
||||
for _ in 0..list.len() {
|
||||
_count += 1;
|
||||
}
|
||||
|
||||
// 直接遍历列表元素
|
||||
_count = 0;
|
||||
for _n in &list {
|
||||
_count += 1;
|
||||
}
|
||||
// 或者
|
||||
// list.iter().for_each(|_| _count += 1);
|
||||
// let _count = list.iter().fold(0, |_count, _| _count + 1);
|
||||
|
||||
// 拼接两个列表
|
||||
let mut list1 = vec![ 6, 8, 7, 10, 9 ];
|
||||
list.append(&mut list1); // append(移动) 之后 list1 为空!
|
||||
// list.extend(&list1); // extend(借用) list1 能继续使用
|
||||
print!("\n将列表 list1 拼接到 list 之后,得到 list = ");
|
||||
print_util::print_array(&list);
|
||||
|
||||
// 排序列表
|
||||
list.sort();
|
||||
print!("\n排序列表后 list = ");
|
||||
print_util::print_array(&list);
|
||||
}
|
||||
|
||||
@ -6,8 +6,8 @@
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
#[allow(dead_code)]
|
||||
/* 列表类简易实现 */
|
||||
#[allow(dead_code)]
|
||||
struct MyList {
|
||||
nums: Vec<i32>, // 数组(存储列表元素)
|
||||
capacity: usize, // 列表容量
|
||||
@ -154,4 +154,4 @@ fn main() {
|
||||
print!("\n扩容后的列表 list = ");
|
||||
print_util::print_array(&list.to_array());
|
||||
print!(" ,容量 = {} ,长度 = {}", list.capacity(), list.size());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user