Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rust linked list #1609

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions codes/rust/chapter_array_and_linkedlist/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ fn extend(nums: &[i32], enlarge: usize) -> Vec<i32> {
// 初始化一个扩展长度后的数组
let mut res: Vec<i32> = vec![0; nums.len() + enlarge];
// 将原数组中的所有元素复制到新
for i in 0..nums.len() {
res[i] = nums[i];
}
res[0..nums.len()].copy_from_slice(nums);

// 返回扩展后的新数组
res
}
Expand Down Expand Up @@ -54,7 +53,8 @@ fn traverse(nums: &[i32]) {
_count += nums[i];
}
// 直接遍历数组元素
for num in nums {
_count = 0;
for &num in nums {
_count += num;
}
}
Expand Down
48 changes: 29 additions & 19 deletions codes/rust/chapter_array_and_linkedlist/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ pub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {
/* 删除链表的节点 n0 之后的首个节点 */
#[allow(non_snake_case)]
pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
if n0.borrow().next.is_none() {
return;
};
// n0 -> P -> n1
let P = n0.borrow_mut().next.take();
if let Some(node) = P {
Expand All @@ -31,26 +28,39 @@ pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
}

/* 访问链表中索引为 index 的节点 */
pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {
if index <= 0 {
return head;
};
if let Some(node) = &head.borrow().next {
return access(node.clone(), index - 1);
pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Option<Rc<RefCell<ListNode<T>>>> {
fn dfs<T>(
head: Option<&Rc<RefCell<ListNode<T>>>>,
index: i32,
) -> Option<Rc<RefCell<ListNode<T>>>> {
if index <= 0 {
return head.cloned();
}

if let Some(node) = head {
dfs(node.borrow().next.as_ref(), index - 1)
} else {
None
}
}

return head;
dfs(Some(head).as_ref(), index)
}

/* 在链表中查找值为 target 的首个节点 */
pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {
if head.borrow().val == target {
return index;
};
if let Some(node) = &head.borrow_mut().next {
return find(node.clone(), target, index + 1);
pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T) -> i32 {
fn find<T: PartialEq>(head: Option<&Rc<RefCell<ListNode<T>>>>, target: T, idx: i32) -> i32 {
if let Some(node) = head {
if node.borrow().val == target {
return idx;
}
return find(node.borrow().next.as_ref(), target, idx + 1);
} else {
-1
}
}
return -1;

find(Some(head).as_ref(), target, 0)
}

/* Driver Code */
Expand Down Expand Up @@ -82,9 +92,9 @@ fn main() {

/* 访问节点 */
let node = access(n0.clone(), 3);
println!("链表中索引 3 处的节点的值 = {}", node.borrow().val);
println!("链表中索引 3 处的节点的值 = {}", node.unwrap().borrow().val);

/* 查找节点 */
let index = find(n0.clone(), 2, 0);
let index = find(n0.clone(), 2);
println!("链表中值为 2 的节点的索引 = {}", index);
}
8 changes: 7 additions & 1 deletion codes/rust/src/include/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ pub struct Vertex {
pub val: i32,
}

impl From<i32> for Vertex {
fn from(value: i32) -> Self {
Self { val: value }
}
}

/* 输入值列表 vals ,返回顶点列表 vets */
pub fn vals_to_vets(vals: Vec<i32>) -> Vec<Vertex> {
vals.into_iter().map(|val| Vertex { val }).collect()
vals.into_iter().map(|val| val.into()).collect()
}

/* 输入顶点列表 vets ,返回值列表 vals */
Expand Down
Loading