This commit is contained in:
krahets
2024-03-18 03:11:07 +08:00
parent bc0054a577
commit 54c7448946
48 changed files with 577 additions and 408 deletions

View File

@ -512,7 +512,7 @@ By comparison, inserting an element into an array has a time complexity of $O(n)
/* 在链表的节点 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();
let n1 = n0.borrow_mut().next.take();
P.borrow_mut().next = n1;
n0.borrow_mut().next = Some(P);
}
@ -690,7 +690,9 @@ It's important to note that even though node `P` continues to point to `n1` afte
/* 删除链表的节点 n0 之后的首个节点 */
#[allow(non_snake_case)]
pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
if n0.borrow().next.is_none() {return};
if n0.borrow().next.is_none() {
return;
};
// n0 -> P -> n1
let P = n0.borrow_mut().next.take();
if let Some(node) = P {
@ -872,10 +874,13 @@ It's important to note that even though node `P` continues to point to `n1` afte
```rust title="linked_list.rs"
/* 访问链表中索引为 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_mut().next {
if index <= 0 {
return head;
};
if let Some(node) = &head.borrow().next {
return access(node.clone(), index - 1);
}
return head;
}
```
@ -1071,7 +1076,9 @@ Traverse the linked list to locate a node whose value matches `target`, and then
```rust title="linked_list.rs"
/* 在链表中查找值为 target 的首个节点 */
pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {
if head.borrow().val == target {return index};
if head.borrow().val == target {
return index;
};
if let Some(node) = &head.borrow_mut().next {
return find(node.clone(), target, index + 1);
}

View File

@ -1785,16 +1785,16 @@ To enhance our understanding of how lists work, we will attempt to implement a s
#[allow(dead_code)]
struct MyList {
arr: Vec<i32>, // 数组(存储列表元素)
capacity: usize, // 列表容量
size: usize, // 列表长度(当前元素数量)
extend_ratio: usize, // 每次列表扩容的倍数
capacity: usize, // 列表容量
size: usize, // 列表长度(当前元素数量)
extend_ratio: usize, // 每次列表扩容的倍数
}
#[allow(unused,unused_comparisons)]
#[allow(unused, unused_comparisons)]
impl MyList {
/* 构造方法 */
pub fn new(capacity: usize) -> Self {
let mut vec = Vec::new();
let mut vec = Vec::new();
vec.resize(capacity, 0);
Self {
arr: vec,
@ -1817,13 +1817,17 @@ To enhance our understanding of how lists work, we will attempt to implement a s
/* 访问元素 */
pub fn get(&self, index: usize) -> i32 {
// 索引如果越界,则抛出异常,下同
if index >= self.size {panic!("索引越界")};
if index >= self.size {
panic!("索引越界")
};
return self.arr[index];
}
/* 更新元素 */
pub fn set(&mut self, index: usize, num: i32) {
if index >= self.size {panic!("索引越界")};
if index >= self.size {
panic!("索引越界")
};
self.arr[index] = num;
}
@ -1840,7 +1844,9 @@ To enhance our understanding of how lists work, we will attempt to implement a s
/* 在中间插入元素 */
pub fn insert(&mut self, index: usize, num: i32) {
if index >= self.size() {panic!("索引越界")};
if index >= self.size() {
panic!("索引越界")
};
// 元素数量超出容量时,触发扩容机制
if self.size == self.capacity() {
self.extend_capacity();
@ -1856,7 +1862,9 @@ To enhance our understanding of how lists work, we will attempt to implement a s
/* 删除元素 */
pub fn remove(&mut self, index: usize) -> i32 {
if index >= self.size() {panic!("索引越界")};
if index >= self.size() {
panic!("索引越界")
};
let num = self.arr[index];
// 将将索引 index 之后的元素都向前移动一位
for j in (index..self.size - 1) {