Release Rust code to documents. (#656)

This commit is contained in:
Yudong Jin
2023-07-26 11:00:53 +08:00
committed by GitHub
parent 60162f6fa8
commit 027bdd6510
61 changed files with 1155 additions and 145 deletions
@@ -163,6 +163,12 @@
}
```
=== "Rust"
```rust title=""
```
!!! question "尾节点指向什么?"
我们将链表的最后一个节点称为「尾节点」,其指向的是“空”,在 Java, C++, Python 中分别记为 $\text{null}$ , $\text{nullptr}$ , $\text{None}$ 。在不引起歧义的前提下,本书都使用 $\text{None}$ 来表示空。
@@ -360,6 +366,12 @@
n3.next = n4;
```
=== "Rust"
```rust title="linked_list.rs"
```
## 链表优点
**链表中插入与删除节点的操作效率高**。例如,如果我们想在链表中间的两个节点 `A` , `B` 之间插入一个新节点 `P` ,我们只需要改变两个节点指针即可,时间复杂度为 $O(1)$ ;相比之下,数组的插入操作效率要低得多。
@@ -432,6 +444,12 @@
[class]{}-[func]{insert}
```
=== "Rust"
```rust title="linked_list.rs"
[class]{}-[func]{insert}
```
在链表中删除节点也非常方便,只需改变一个节点的指针即可。如下图所示,尽管在删除操作完成后,节点 `P` 仍然指向 `n1` ,但实际上 `P` 已经不再属于此链表,因为遍历此链表时无法访问到 `P` 。
![链表删除节点](linked_list.assets/linkedlist_remove_node.png)
@@ -502,6 +520,12 @@
[class]{}-[func]{remove}
```
=== "Rust"
```rust title="linked_list.rs"
[class]{}-[func]{remove}
```
## 链表缺点
**链表访问节点效率较低**。如上节所述,数组可以在 $O(1)$ 时间下访问任意元素。然而,链表无法直接访问任意节点,这是因为系统需要从头节点出发,逐个向后遍历直至找到目标节点。例如,若要访问链表索引为 `index`(即第 `index + 1` 个)的节点,则需要向后遍历 `index` 轮。
@@ -572,6 +596,12 @@
[class]{}-[func]{access}
```
=== "Rust"
```rust title="linked_list.rs"
[class]{}-[func]{access}
```
**链表的内存占用较大**。链表以节点为单位,每个节点除了保存值之外,还需额外保存指针(引用)。这意味着在相同数据量的情况下,链表比数组需要占用更多的内存空间。
## 链表常用操作
@@ -644,6 +674,12 @@
[class]{}-[func]{find}
```
=== "Rust"
```rust title="linked_list.rs"
[class]{}-[func]{find}
```
## 常见链表类型
**单向链表**。即上述介绍的普通链表。单向链表的节点包含值和指向下一节点的指针(引用)两项数据。我们将首个节点称为头节点,将最后一个节点成为尾节点,尾节点指向空 $\text{None}$ 。
@@ -823,6 +859,12 @@
}
```
=== "Rust"
```rust title=""
```
![常见链表种类](linked_list.assets/linkedlist_common_types.png)
## 链表典型应用