2023-10-06 13:31:21 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# 4.3 列表
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
「列表 list」是一个抽象的数据结构概念,它表示元素的有序集合,支持元素访问、修改、添加、删除和遍历等操作,无须使用者考虑容量限制的问题。列表可以基于链表或数组实现。
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-14 02:53:34 +08:00
|
|
|
|
- 链表天然可以看作一个列表,其支持元素增删查改操作,并且可以灵活动态扩容。
|
|
|
|
|
- 数组也支持元素增删查改,但由于其长度不可变,因此只能看作一个具有长度限制的列表。
|
2023-10-17 00:34:03 +08:00
|
|
|
|
|
2023-12-14 02:53:34 +08:00
|
|
|
|
当使用数组实现列表时,**长度不可变的性质会导致列表的实用性降低**。这是因为我们通常无法事先确定需要存储多少数据,从而难以选择合适的列表长度。若长度过小,则很可能无法满足使用需求;若长度过大,则会造成内存空间浪费。
|
2023-10-17 00:34:03 +08:00
|
|
|
|
|
|
|
|
|
为解决此问题,我们可以使用「动态数组 dynamic array」来实现列表。它继承了数组的各项优点,并且可以在程序运行过程中进行动态扩容。
|
|
|
|
|
|
2023-12-14 02:53:34 +08:00
|
|
|
|
实际上,**许多编程语言中的标准库提供的列表是基于动态数组实现的**,例如 Python 中的 `list` 、Java 中的 `ArrayList` 、C++ 中的 `vector` 和 C# 中的 `List` 等。在接下来的讨论中,我们将把“列表”和“动态数组”视为等同的概念。
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
## 4.3.1 列表常用操作
|
|
|
|
|
|
|
|
|
|
### 1. 初始化列表
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
我们通常使用“无初始值”和“有初始值”这两种初始化方法:
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="list.py"
|
|
|
|
|
# 初始化列表
|
|
|
|
|
# 无初始值
|
2023-10-27 20:41:59 +08:00
|
|
|
|
nums1: list[int] = []
|
2023-10-06 13:31:21 +08:00
|
|
|
|
# 有初始值
|
2023-10-27 20:41:59 +08:00
|
|
|
|
nums: list[int] = [1, 3, 2, 5, 4]
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="list.cpp"
|
|
|
|
|
/* 初始化列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
// 需注意,C++ 中 vector 即是本文描述的 nums
|
2023-10-06 13:31:21 +08:00
|
|
|
|
// 无初始值
|
2023-10-09 18:19:22 +08:00
|
|
|
|
vector<int> nums1;
|
2023-10-06 13:31:21 +08:00
|
|
|
|
// 有初始值
|
2023-10-09 18:19:22 +08:00
|
|
|
|
vector<int> nums = { 1, 3, 2, 5, 4 };
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="list.java"
|
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
// 无初始值
|
2023-10-09 18:19:22 +08:00
|
|
|
|
List<Integer> nums1 = new ArrayList<>();
|
2023-10-06 13:31:21 +08:00
|
|
|
|
// 有初始值(注意数组的元素类型需为 int[] 的包装类 Integer[])
|
|
|
|
|
Integer[] numbers = new Integer[] { 1, 3, 2, 5, 4 };
|
2023-10-09 18:19:22 +08:00
|
|
|
|
List<Integer> nums = new ArrayList<>(Arrays.asList(numbers));
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="list.cs"
|
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
// 无初始值
|
2023-11-27 02:32:06 +08:00
|
|
|
|
List<int> nums1 = [];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
// 有初始值
|
2023-11-27 02:32:06 +08:00
|
|
|
|
int[] numbers = [1, 3, 2, 5, 4];
|
|
|
|
|
List<int> nums = [.. numbers];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="list_test.go"
|
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
// 无初始值
|
2023-11-26 02:06:39 +08:00
|
|
|
|
nums1 := []int{}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
// 有初始值
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums := []int{1, 3, 2, 5, 4}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="list.swift"
|
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
// 无初始值
|
2023-10-09 18:19:22 +08:00
|
|
|
|
let nums1: [Int] = []
|
2023-10-06 13:31:21 +08:00
|
|
|
|
// 有初始值
|
2023-10-09 18:19:22 +08:00
|
|
|
|
var nums = [1, 3, 2, 5, 4]
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JS"
|
|
|
|
|
|
|
|
|
|
```javascript title="list.js"
|
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
// 无初始值
|
2023-10-09 18:19:22 +08:00
|
|
|
|
const nums1 = [];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
// 有初始值
|
2023-10-09 18:19:22 +08:00
|
|
|
|
const nums = [1, 3, 2, 5, 4];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TS"
|
|
|
|
|
|
|
|
|
|
```typescript title="list.ts"
|
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
// 无初始值
|
2023-10-09 18:19:22 +08:00
|
|
|
|
const nums1: number[] = [];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
// 有初始值
|
2023-10-09 18:19:22 +08:00
|
|
|
|
const nums: number[] = [1, 3, 2, 5, 4];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="list.dart"
|
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
// 无初始值
|
2023-10-09 18:19:22 +08:00
|
|
|
|
List<int> nums1 = [];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
// 有初始值
|
2023-10-09 18:19:22 +08:00
|
|
|
|
List<int> nums = [1, 3, 2, 5, 4];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="list.rs"
|
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
// 无初始值
|
2023-10-09 18:19:22 +08:00
|
|
|
|
let nums1: Vec<i32> = Vec::new();
|
2023-10-06 13:31:21 +08:00
|
|
|
|
// 有初始值
|
2023-10-10 13:20:01 +08:00
|
|
|
|
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="list.c"
|
|
|
|
|
// C 未提供内置动态数组
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="list.kt"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="list.zig"
|
|
|
|
|
// 初始化列表
|
2023-10-09 18:19:22 +08:00
|
|
|
|
var nums = std.ArrayList(i32).init(std.heap.page_allocator);
|
|
|
|
|
defer nums.deinit();
|
|
|
|
|
try nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 03:26:23 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:00:24 +08:00
|
|
|
|
<div style="height: 315px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20%23%20%E6%97%A0%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums1%20%3D%20%5B%5D%0A%20%20%20%20%23%20%E6%9C%89%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=4&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
|
|
|
|
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20%23%20%E6%97%A0%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums1%20%3D%20%5B%5D%0A%20%20%20%20%23%20%E6%9C%89%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=4&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
|
2024-01-07 03:26:23 +08:00
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
### 2. 访问元素
|
|
|
|
|
|
|
|
|
|
列表本质上是数组,因此可以在 $O(1)$ 时间内访问和更新元素,效率很高。
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="list.py"
|
|
|
|
|
# 访问元素
|
2023-10-10 13:20:01 +08:00
|
|
|
|
num: int = nums[1] # 访问索引 1 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
# 更新元素
|
2023-10-10 13:20:01 +08:00
|
|
|
|
nums[1] = 0 # 将索引 1 处的元素更新为 0
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="list.cpp"
|
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int num = nums[1]; // 访问索引 1 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums[1] = 0; // 将索引 1 处的元素更新为 0
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="list.java"
|
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int num = nums.get(1); // 访问索引 1 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.set(1, 0); // 将索引 1 处的元素更新为 0
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="list.cs"
|
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int num = nums[1]; // 访问索引 1 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums[1] = 0; // 将索引 1 处的元素更新为 0
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="list_test.go"
|
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
num := nums[1] // 访问索引 1 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums[1] = 0 // 将索引 1 处的元素更新为 0
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="list.swift"
|
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
let num = nums[1] // 访问索引 1 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums[1] = 0 // 将索引 1 处的元素更新为 0
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JS"
|
|
|
|
|
|
|
|
|
|
```javascript title="list.js"
|
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
const num = nums[1]; // 访问索引 1 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums[1] = 0; // 将索引 1 处的元素更新为 0
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TS"
|
|
|
|
|
|
|
|
|
|
```typescript title="list.ts"
|
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
const num: number = nums[1]; // 访问索引 1 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums[1] = 0; // 将索引 1 处的元素更新为 0
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="list.dart"
|
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int num = nums[1]; // 访问索引 1 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums[1] = 0; // 将索引 1 处的元素更新为 0
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="list.rs"
|
|
|
|
|
/* 访问元素 */
|
2023-10-17 23:44:50 +08:00
|
|
|
|
let num: i32 = nums[1]; // 访问索引 1 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
/* 更新元素 */
|
2023-10-17 23:44:50 +08:00
|
|
|
|
nums[1] = 0; // 将索引 1 处的元素更新为 0
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="list.c"
|
|
|
|
|
// C 未提供内置动态数组
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="list.kt"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="list.zig"
|
|
|
|
|
// 访问元素
|
2023-10-09 18:19:22 +08:00
|
|
|
|
var num = nums.items[1]; // 访问索引 1 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
// 更新元素
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.items[1] = 0; // 将索引 1 处的元素更新为 0
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 03:26:23 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:00:24 +08:00
|
|
|
|
<div style="height: 369px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%0A%20%20%20%20num%20%3D%20nums%5B1%5D%20%20%23%20%E8%AE%BF%E9%97%AE%E7%B4%A2%E5%BC%95%201%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%0A%0A%20%20%20%20%23%20%E6%9B%B4%E6%96%B0%E5%85%83%E7%B4%A0%0A%20%20%20%20nums%5B1%5D%20%3D%200%20%20%20%20%23%20%E5%B0%86%E7%B4%A2%E5%BC%95%201%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%E6%9B%B4%E6%96%B0%E4%B8%BA%200&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
|
|
|
|
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%0A%20%20%20%20num%20%3D%20nums%5B1%5D%20%20%23%20%E8%AE%BF%E9%97%AE%E7%B4%A2%E5%BC%95%201%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%0A%0A%20%20%20%20%23%20%E6%9B%B4%E6%96%B0%E5%85%83%E7%B4%A0%0A%20%20%20%20nums%5B1%5D%20%3D%200%20%20%20%20%23%20%E5%B0%86%E7%B4%A2%E5%BC%95%201%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%E6%9B%B4%E6%96%B0%E4%B8%BA%200&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
|
2024-01-07 03:26:23 +08:00
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
### 3. 插入与删除元素
|
|
|
|
|
|
|
|
|
|
相较于数组,列表可以自由地添加与删除元素。在列表尾部添加元素的时间复杂度为 $O(1)$ ,但插入和删除元素的效率仍与数组相同,时间复杂度为 $O(n)$ 。
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="list.py"
|
|
|
|
|
# 清空列表
|
2023-10-10 13:20:01 +08:00
|
|
|
|
nums.clear()
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
# 在尾部添加元素
|
2023-10-10 13:20:01 +08:00
|
|
|
|
nums.append(1)
|
|
|
|
|
nums.append(3)
|
|
|
|
|
nums.append(2)
|
|
|
|
|
nums.append(5)
|
|
|
|
|
nums.append(4)
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
# 在中间插入元素
|
2023-10-10 13:20:01 +08:00
|
|
|
|
nums.insert(3, 6) # 在索引 3 处插入数字 6
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
# 删除元素
|
2023-10-10 13:20:01 +08:00
|
|
|
|
nums.pop(3) # 删除索引 3 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="list.cpp"
|
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.clear();
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.push_back(1);
|
|
|
|
|
nums.push_back(3);
|
|
|
|
|
nums.push_back(2);
|
|
|
|
|
nums.push_back(5);
|
|
|
|
|
nums.push_back(4);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.insert(nums.begin() + 3, 6); // 在索引 3 处插入数字 6
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.erase(nums.begin() + 3); // 删除索引 3 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="list.java"
|
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.clear();
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.add(1);
|
|
|
|
|
nums.add(3);
|
|
|
|
|
nums.add(2);
|
|
|
|
|
nums.add(5);
|
|
|
|
|
nums.add(4);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.add(3, 6); // 在索引 3 处插入数字 6
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.remove(3); // 删除索引 3 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="list.cs"
|
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.Clear();
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.Add(1);
|
|
|
|
|
nums.Add(3);
|
|
|
|
|
nums.Add(2);
|
|
|
|
|
nums.Add(5);
|
|
|
|
|
nums.Add(4);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.Insert(3, 6);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.RemoveAt(3);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="list_test.go"
|
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums = nil
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums = append(nums, 1)
|
|
|
|
|
nums = append(nums, 3)
|
|
|
|
|
nums = append(nums, 2)
|
|
|
|
|
nums = append(nums, 5)
|
|
|
|
|
nums = append(nums, 4)
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums = append(nums[:3], append([]int{6}, nums[3:]...)...) // 在索引 3 处插入数字 6
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums = append(nums[:3], nums[4:]...) // 删除索引 3 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="list.swift"
|
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.removeAll()
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.append(1)
|
|
|
|
|
nums.append(3)
|
|
|
|
|
nums.append(2)
|
|
|
|
|
nums.append(5)
|
|
|
|
|
nums.append(4)
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.insert(6, at: 3) // 在索引 3 处插入数字 6
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.remove(at: 3) // 删除索引 3 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JS"
|
|
|
|
|
|
|
|
|
|
```javascript title="list.js"
|
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.length = 0;
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.push(1);
|
|
|
|
|
nums.push(3);
|
|
|
|
|
nums.push(2);
|
|
|
|
|
nums.push(5);
|
|
|
|
|
nums.push(4);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.splice(3, 0, 6);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.splice(3, 1);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TS"
|
|
|
|
|
|
|
|
|
|
```typescript title="list.ts"
|
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.length = 0;
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.push(1);
|
|
|
|
|
nums.push(3);
|
|
|
|
|
nums.push(2);
|
|
|
|
|
nums.push(5);
|
|
|
|
|
nums.push(4);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.splice(3, 0, 6);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.splice(3, 1);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="list.dart"
|
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.clear();
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.add(1);
|
|
|
|
|
nums.add(3);
|
|
|
|
|
nums.add(2);
|
|
|
|
|
nums.add(5);
|
|
|
|
|
nums.add(4);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.insert(3, 6); // 在索引 3 处插入数字 6
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.removeAt(3); // 删除索引 3 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="list.rs"
|
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.clear();
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.push(1);
|
|
|
|
|
nums.push(3);
|
|
|
|
|
nums.push(2);
|
|
|
|
|
nums.push(5);
|
|
|
|
|
nums.push(4);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.insert(3, 6); // 在索引 3 处插入数字 6
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.remove(3); // 删除索引 3 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="list.c"
|
|
|
|
|
// C 未提供内置动态数组
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="list.kt"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="list.zig"
|
|
|
|
|
// 清空列表
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.clearRetainingCapacity();
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 在尾部添加元素
|
2023-10-09 18:19:22 +08:00
|
|
|
|
try nums.append(1);
|
|
|
|
|
try nums.append(3);
|
|
|
|
|
try nums.append(2);
|
|
|
|
|
try nums.append(5);
|
|
|
|
|
try nums.append(4);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 在中间插入元素
|
2023-10-09 18:19:22 +08:00
|
|
|
|
try nums.insert(3, 6); // 在索引 3 处插入数字 6
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
// 删除元素
|
2023-10-09 18:19:22 +08:00
|
|
|
|
_ = nums.orderedRemove(3); // 删除索引 3 处的元素
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 03:26:23 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:00:24 +08:00
|
|
|
|
<div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E6%9C%89%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%B8%85%E7%A9%BA%E5%88%97%E8%A1%A8%0A%20%20%20%20nums.clear%28%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%9C%A8%E5%B0%BE%E9%83%A8%E6%B7%BB%E5%8A%A0%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.append%281%29%0A%20%20%20%20nums.append%283%29%0A%20%20%20%20nums.append%282%29%0A%20%20%20%20nums.append%285%29%0A%20%20%20%20nums.append%284%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%9C%A8%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.insert%283,%206%29%20%20%23%20%E5%9C%A8%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E6%8F%92%E5%85%A5%E6%95%B0%E5%AD%97%206%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.pop%283%29%20%20%20%20%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
|
|
|
|
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E6%9C%89%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%B8%85%E7%A9%BA%E5%88%97%E8%A1%A8%0A%20%20%20%20nums.clear%28%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%9C%A8%E5%B0%BE%E9%83%A8%E6%B7%BB%E5%8A%A0%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.append%281%29%0A%20%20%20%20nums.append%283%29%0A%20%20%20%20nums.append%282%29%0A%20%20%20%20nums.append%285%29%0A%20%20%20%20nums.append%284%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%9C%A8%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.insert%283,%206%29%20%20%23%20%E5%9C%A8%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E6%8F%92%E5%85%A5%E6%95%B0%E5%AD%97%206%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.pop%283%29%20%20%20%20%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
|
2024-01-07 03:26:23 +08:00
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
### 4. 遍历列表
|
|
|
|
|
|
|
|
|
|
与数组一样,列表可以根据索引遍历,也可以直接遍历各元素。
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="list.py"
|
|
|
|
|
# 通过索引遍历列表
|
|
|
|
|
count = 0
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for i in range(len(nums)):
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += nums[i]
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
# 直接遍历列表元素
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for num in nums:
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += num
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="list.cpp"
|
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
int count = 0;
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for (int i = 0; i < nums.size(); i++) {
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += nums[i];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0;
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for (int num : nums) {
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += num;
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="list.java"
|
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
int count = 0;
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for (int i = 0; i < nums.size(); i++) {
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += nums.get(i);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 直接遍历列表元素 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for (int num : nums) {
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += num;
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="list.cs"
|
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
int count = 0;
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for (int i = 0; i < nums.Count; i++) {
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += nums[i];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0;
|
2023-10-09 18:19:22 +08:00
|
|
|
|
foreach (int num in nums) {
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += num;
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="list_test.go"
|
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
count := 0
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for i := 0; i < len(nums); i++ {
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += nums[i]
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0
|
2023-11-11 23:03:42 +08:00
|
|
|
|
for _, num := range nums {
|
|
|
|
|
count += num
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="list.swift"
|
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
var count = 0
|
2023-11-11 23:03:42 +08:00
|
|
|
|
for i in nums.indices {
|
|
|
|
|
count += nums[i]
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0
|
2023-11-11 23:03:42 +08:00
|
|
|
|
for num in nums {
|
|
|
|
|
count += num
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JS"
|
|
|
|
|
|
|
|
|
|
```javascript title="list.js"
|
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
let count = 0;
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += nums[i];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0;
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for (const num of nums) {
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += num;
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TS"
|
|
|
|
|
|
|
|
|
|
```typescript title="list.ts"
|
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
let count = 0;
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += nums[i];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0;
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for (const num of nums) {
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += num;
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="list.dart"
|
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
int count = 0;
|
2023-11-11 23:03:42 +08:00
|
|
|
|
for (var i = 0; i < nums.length; i++) {
|
|
|
|
|
count += nums[i];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
2023-11-11 23:03:42 +08:00
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0;
|
2023-11-11 23:03:42 +08:00
|
|
|
|
for (var num in nums) {
|
|
|
|
|
count += num;
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="list.rs"
|
2023-11-11 23:03:42 +08:00
|
|
|
|
// 通过索引遍历列表
|
|
|
|
|
let mut _count = 0;
|
|
|
|
|
for i in 0..nums.len() {
|
|
|
|
|
_count += nums[i];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-11 23:03:42 +08:00
|
|
|
|
// 直接遍历列表元素
|
|
|
|
|
_count = 0;
|
|
|
|
|
for num in &nums {
|
|
|
|
|
_count += num;
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="list.c"
|
|
|
|
|
// C 未提供内置动态数组
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="list.kt"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="list.zig"
|
|
|
|
|
// 通过索引遍历列表
|
|
|
|
|
var count: i32 = 0;
|
|
|
|
|
var i: i32 = 0;
|
2023-10-09 18:19:22 +08:00
|
|
|
|
while (i < nums.items.len) : (i += 1) {
|
2023-11-11 23:03:42 +08:00
|
|
|
|
count += nums[i];
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 直接遍历列表元素
|
|
|
|
|
count = 0;
|
2023-11-11 23:03:42 +08:00
|
|
|
|
for (nums.items) |num| {
|
|
|
|
|
count += num;
|
2023-10-06 13:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 03:26:23 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:00:24 +08:00
|
|
|
|
<div style="height: 423px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E9%80%9A%E8%BF%87%E7%B4%A2%E5%BC%95%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20nums%5Bi%5D%0A%0A%20%20%20%20%23%20%E7%9B%B4%E6%8E%A5%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8%E5%85%83%E7%B4%A0%0A%20%20%20%20for%20num%20in%20nums%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20num&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
|
|
|
|
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E9%80%9A%E8%BF%87%E7%B4%A2%E5%BC%95%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20nums%5Bi%5D%0A%0A%20%20%20%20%23%20%E7%9B%B4%E6%8E%A5%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8%E5%85%83%E7%B4%A0%0A%20%20%20%20for%20num%20in%20nums%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20num&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
|
2024-01-07 03:26:23 +08:00
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
### 5. 拼接列表
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
给定一个新列表 `nums1` ,我们可以将其拼接到原列表的尾部。
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="list.py"
|
|
|
|
|
# 拼接两个列表
|
2023-10-27 20:41:59 +08:00
|
|
|
|
nums1: list[int] = [6, 8, 7, 10, 9]
|
2023-10-10 13:20:01 +08:00
|
|
|
|
nums += nums1 # 将列表 nums1 拼接到 nums 之后
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="list.cpp"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
vector<int> nums1 = { 6, 8, 7, 10, 9 };
|
|
|
|
|
// 将列表 nums1 拼接到 nums 之后
|
|
|
|
|
nums.insert(nums.end(), nums1.begin(), nums1.end());
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="list.java"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
List<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));
|
|
|
|
|
nums.addAll(nums1); // 将列表 nums1 拼接到 nums 之后
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="list.cs"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-11-27 02:32:06 +08:00
|
|
|
|
List<int> nums1 = [6, 8, 7, 10, 9];
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.AddRange(nums1); // 将列表 nums1 拼接到 nums 之后
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="list_test.go"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums1 := []int{6, 8, 7, 10, 9}
|
|
|
|
|
nums = append(nums, nums1...) // 将列表 nums1 拼接到 nums 之后
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="list.swift"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
let nums1 = [6, 8, 7, 10, 9]
|
|
|
|
|
nums.append(contentsOf: nums1) // 将列表 nums1 拼接到 nums 之后
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JS"
|
|
|
|
|
|
|
|
|
|
```javascript title="list.js"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
const nums1 = [6, 8, 7, 10, 9];
|
|
|
|
|
nums.push(...nums1); // 将列表 nums1 拼接到 nums 之后
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TS"
|
|
|
|
|
|
|
|
|
|
```typescript title="list.ts"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
const nums1: number[] = [6, 8, 7, 10, 9];
|
|
|
|
|
nums.push(...nums1); // 将列表 nums1 拼接到 nums 之后
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="list.dart"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
List<int> nums1 = [6, 8, 7, 10, 9];
|
|
|
|
|
nums.addAll(nums1); // 将列表 nums1 拼接到 nums 之后
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="list.rs"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
let nums1: Vec<i32> = vec![6, 8, 7, 10, 9];
|
|
|
|
|
nums.extend(nums1);
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="list.c"
|
|
|
|
|
// C 未提供内置动态数组
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="list.kt"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="list.zig"
|
|
|
|
|
// 拼接两个列表
|
2023-10-09 18:19:22 +08:00
|
|
|
|
var nums1 = std.ArrayList(i32).init(std.heap.page_allocator);
|
|
|
|
|
defer nums1.deinit();
|
|
|
|
|
try nums1.appendSlice(&[_]i32{ 6, 8, 7, 10, 9 });
|
|
|
|
|
try nums.insertSlice(nums.items.len, nums1.items); // 将列表 nums1 拼接到 nums 之后
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 03:26:23 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:00:24 +08:00
|
|
|
|
<div style="height: 333px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%8B%BC%E6%8E%A5%E4%B8%A4%E4%B8%AA%E5%88%97%E8%A1%A8%0A%20%20%20%20nums1%20%3D%20%5B6,%208,%207,%2010,%209%5D%0A%20%20%20%20nums%20%2B%3D%20nums1%20%20%23%20%E5%B0%86%E5%88%97%E8%A1%A8%20nums1%20%E6%8B%BC%E6%8E%A5%E5%88%B0%20nums%20%E4%B9%8B%E5%90%8E&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
|
|
|
|
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%8B%BC%E6%8E%A5%E4%B8%A4%E4%B8%AA%E5%88%97%E8%A1%A8%0A%20%20%20%20nums1%20%3D%20%5B6,%208,%207,%2010,%209%5D%0A%20%20%20%20nums%20%2B%3D%20nums1%20%20%23%20%E5%B0%86%E5%88%97%E8%A1%A8%20nums1%20%E6%8B%BC%E6%8E%A5%E5%88%B0%20nums%20%E4%B9%8B%E5%90%8E&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
|
2024-01-07 03:26:23 +08:00
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
### 6. 排序列表
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
完成列表排序后,我们便可以使用在数组类算法题中经常考查的“二分查找”和“双指针”算法。
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="list.py"
|
|
|
|
|
# 排序列表
|
2023-10-10 13:20:01 +08:00
|
|
|
|
nums.sort() # 排序后,列表元素从小到大排列
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="list.cpp"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
sort(nums.begin(), nums.end()); // 排序后,列表元素从小到大排列
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="list.java"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
Collections.sort(nums); // 排序后,列表元素从小到大排列
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="list.cs"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.Sort(); // 排序后,列表元素从小到大排列
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="list_test.go"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
sort.Ints(nums) // 排序后,列表元素从小到大排列
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="list.swift"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.sort() // 排序后,列表元素从小到大排列
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JS"
|
|
|
|
|
|
|
|
|
|
```javascript title="list.js"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.sort((a, b) => a - b); // 排序后,列表元素从小到大排列
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TS"
|
|
|
|
|
|
|
|
|
|
```typescript title="list.ts"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.sort((a, b) => a - b); // 排序后,列表元素从小到大排列
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="list.dart"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.sort(); // 排序后,列表元素从小到大排列
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="list.rs"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums.sort(); // 排序后,列表元素从小到大排列
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="list.c"
|
|
|
|
|
// C 未提供内置动态数组
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="list.kt"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="list.zig"
|
|
|
|
|
// 排序列表
|
2023-10-09 18:19:22 +08:00
|
|
|
|
std.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 03:26:23 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:00:24 +08:00
|
|
|
|
<div style="height: 315px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%8E%92%E5%BA%8F%E5%88%97%E8%A1%A8%0A%20%20%20%20nums.sort%28%29%20%20%23%20%E6%8E%92%E5%BA%8F%E5%90%8E%EF%BC%8C%E5%88%97%E8%A1%A8%E5%85%83%E7%B4%A0%E4%BB%8E%E5%B0%8F%E5%88%B0%E5%A4%A7%E6%8E%92%E5%88%97&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
|
|
|
|
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%8E%92%E5%BA%8F%E5%88%97%E8%A1%A8%0A%20%20%20%20nums.sort%28%29%20%20%23%20%E6%8E%92%E5%BA%8F%E5%90%8E%EF%BC%8C%E5%88%97%E8%A1%A8%E5%85%83%E7%B4%A0%E4%BB%8E%E5%B0%8F%E5%88%B0%E5%A4%A7%E6%8E%92%E5%88%97&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
|
2024-01-07 03:26:23 +08:00
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
## 4.3.2 列表实现
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
许多编程语言内置了列表,例如 Java、C++、Python 等。它们的实现比较复杂,各个参数的设定也非常考究,例如初始容量、扩容倍数等。感兴趣的读者可以查阅源码进行学习。
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
为了加深对列表工作原理的理解,我们尝试实现一个简易版列表,包括以下三个重点设计。
|
|
|
|
|
|
|
|
|
|
- **初始容量**:选取一个合理的数组初始容量。在本示例中,我们选择 10 作为初始容量。
|
|
|
|
|
- **数量记录**:声明一个变量 `size` ,用于记录列表当前元素数量,并随着元素插入和删除实时更新。根据此变量,我们可以定位列表尾部,以及判断是否需要扩容。
|
2023-12-02 06:24:05 +08:00
|
|
|
|
- **扩容机制**:若插入元素时列表容量已满,则需要进行扩容。先根据扩容倍数创建一个更大的数组,再将当前数组的所有元素依次移动至新数组。在本示例中,我们规定每次将数组扩容至之前的 2 倍。
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="my_list.py"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
class MyList:
|
2023-12-02 06:24:05 +08:00
|
|
|
|
"""列表类"""
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
"""构造方法"""
|
2023-10-14 22:14:46 +08:00
|
|
|
|
self._capacity: int = 10 # 列表容量
|
|
|
|
|
self._arr: list[int] = [0] * self._capacity # 数组(存储列表元素)
|
2023-12-02 06:24:05 +08:00
|
|
|
|
self._size: int = 0 # 列表长度(当前元素数量)
|
2023-10-14 22:14:46 +08:00
|
|
|
|
self._extend_ratio: int = 2 # 每次列表扩容的倍数
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
def size(self) -> int:
|
2023-12-02 06:24:05 +08:00
|
|
|
|
"""获取列表长度(当前元素数量)"""
|
2023-10-14 22:14:46 +08:00
|
|
|
|
return self._size
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
def capacity(self) -> int:
|
|
|
|
|
"""获取列表容量"""
|
2023-10-14 22:14:46 +08:00
|
|
|
|
return self._capacity
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
def get(self, index: int) -> int:
|
|
|
|
|
"""访问元素"""
|
2023-12-28 17:18:37 +08:00
|
|
|
|
# 索引如果越界,则抛出异常,下同
|
2023-10-14 22:14:46 +08:00
|
|
|
|
if index < 0 or index >= self._size:
|
2023-10-06 14:10:18 +08:00
|
|
|
|
raise IndexError("索引越界")
|
2023-10-14 22:14:46 +08:00
|
|
|
|
return self._arr[index]
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
def set(self, num: int, index: int):
|
|
|
|
|
"""更新元素"""
|
2023-10-14 22:14:46 +08:00
|
|
|
|
if index < 0 or index >= self._size:
|
2023-10-06 14:10:18 +08:00
|
|
|
|
raise IndexError("索引越界")
|
2023-10-14 22:14:46 +08:00
|
|
|
|
self._arr[index] = num
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
def add(self, num: int):
|
2023-12-02 06:24:05 +08:00
|
|
|
|
"""在尾部添加元素"""
|
2023-10-06 14:10:18 +08:00
|
|
|
|
# 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if self.size() == self.capacity():
|
|
|
|
|
self.extend_capacity()
|
2023-10-14 22:14:46 +08:00
|
|
|
|
self._arr[self._size] = num
|
|
|
|
|
self._size += 1
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
def insert(self, num: int, index: int):
|
2023-12-02 06:24:05 +08:00
|
|
|
|
"""在中间插入元素"""
|
2023-10-14 22:14:46 +08:00
|
|
|
|
if index < 0 or index >= self._size:
|
2023-10-06 14:10:18 +08:00
|
|
|
|
raise IndexError("索引越界")
|
|
|
|
|
# 元素数量超出容量时,触发扩容机制
|
2023-10-14 22:14:46 +08:00
|
|
|
|
if self._size == self.capacity():
|
2023-10-06 14:10:18 +08:00
|
|
|
|
self.extend_capacity()
|
|
|
|
|
# 将索引 index 以及之后的元素都向后移动一位
|
2023-10-14 22:14:46 +08:00
|
|
|
|
for j in range(self._size - 1, index - 1, -1):
|
|
|
|
|
self._arr[j + 1] = self._arr[j]
|
|
|
|
|
self._arr[index] = num
|
2023-10-06 14:10:18 +08:00
|
|
|
|
# 更新元素数量
|
2023-10-14 22:14:46 +08:00
|
|
|
|
self._size += 1
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
def remove(self, index: int) -> int:
|
|
|
|
|
"""删除元素"""
|
2023-10-14 22:14:46 +08:00
|
|
|
|
if index < 0 or index >= self._size:
|
2023-10-06 14:10:18 +08:00
|
|
|
|
raise IndexError("索引越界")
|
2023-10-14 22:14:46 +08:00
|
|
|
|
num = self._arr[index]
|
2024-01-07 03:59:56 +08:00
|
|
|
|
# 将索引 index 之后的元素都向前移动一位
|
2023-10-14 22:14:46 +08:00
|
|
|
|
for j in range(index, self._size - 1):
|
|
|
|
|
self._arr[j] = self._arr[j + 1]
|
2023-10-06 14:10:18 +08:00
|
|
|
|
# 更新元素数量
|
2023-10-14 22:14:46 +08:00
|
|
|
|
self._size -= 1
|
2023-12-28 17:18:37 +08:00
|
|
|
|
# 返回被删除的元素
|
2023-10-06 14:10:18 +08:00
|
|
|
|
return num
|
|
|
|
|
|
|
|
|
|
def extend_capacity(self):
|
|
|
|
|
"""列表扩容"""
|
2023-12-28 17:18:37 +08:00
|
|
|
|
# 新建一个长度为原数组 _extend_ratio 倍的新数组,并将原数组复制到新数组
|
2023-10-14 22:14:46 +08:00
|
|
|
|
self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
# 更新列表容量
|
2023-10-14 22:14:46 +08:00
|
|
|
|
self._capacity = len(self._arr)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
def to_array(self) -> list[int]:
|
|
|
|
|
"""返回有效长度的列表"""
|
2023-10-14 22:14:46 +08:00
|
|
|
|
return self._arr[: self._size]
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="my_list.cpp"
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 列表类 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
class MyList {
|
|
|
|
|
private:
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int *arr; // 数组(存储列表元素)
|
|
|
|
|
int arrCapacity = 10; // 列表容量
|
2023-12-02 06:24:05 +08:00
|
|
|
|
int arrSize = 0; // 列表长度(当前元素数量)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
int extendRatio = 2; // 每次列表扩容的倍数
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/* 构造方法 */
|
|
|
|
|
MyList() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr = new int[arrCapacity];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 析构方法 */
|
|
|
|
|
~MyList() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
delete[] arr;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 获取列表长度(当前元素数量)*/
|
2023-10-06 14:10:18 +08:00
|
|
|
|
int size() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arrSize;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表容量 */
|
|
|
|
|
int capacity() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arrCapacity;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
|
int get(int index) {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 索引如果越界,则抛出异常,下同
|
2023-10-06 14:10:18 +08:00
|
|
|
|
if (index < 0 || index >= size())
|
|
|
|
|
throw out_of_range("索引越界");
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arr[index];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
|
|
|
|
void set(int index, int num) {
|
|
|
|
|
if (index < 0 || index >= size())
|
|
|
|
|
throw out_of_range("索引越界");
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
void add(int num) {
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if (size() == capacity())
|
|
|
|
|
extendCapacity();
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[size()] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arrSize++;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
void insert(int index, int num) {
|
|
|
|
|
if (index < 0 || index >= size())
|
|
|
|
|
throw out_of_range("索引越界");
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if (size() == capacity())
|
|
|
|
|
extendCapacity();
|
|
|
|
|
// 将索引 index 以及之后的元素都向后移动一位
|
|
|
|
|
for (int j = size() - 1; j >= index; j--) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[j + 1] = arr[j];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arrSize++;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
|
|
|
|
int remove(int index) {
|
|
|
|
|
if (index < 0 || index >= size())
|
|
|
|
|
throw out_of_range("索引越界");
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int num = arr[index];
|
2024-01-07 03:59:56 +08:00
|
|
|
|
// 将索引 index 之后的元素都向前移动一位
|
2023-10-06 14:10:18 +08:00
|
|
|
|
for (int j = index; j < size() - 1; j++) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[j] = arr[j + 1];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
// 更新元素数量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arrSize--;
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 返回被删除的元素
|
2023-10-06 14:10:18 +08:00
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 列表扩容 */
|
|
|
|
|
void extendCapacity() {
|
|
|
|
|
// 新建一个长度为原数组 extendRatio 倍的新数组
|
|
|
|
|
int newCapacity = capacity() * extendRatio;
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int *tmp = arr;
|
|
|
|
|
arr = new int[newCapacity];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
|
|
|
|
for (int i = 0; i < size(); i++) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[i] = tmp[i];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
// 释放内存
|
|
|
|
|
delete[] tmp;
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arrCapacity = newCapacity;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将列表转换为 Vector 用于打印 */
|
|
|
|
|
vector<int> toVector() {
|
|
|
|
|
// 仅转换有效长度范围内的列表元素
|
|
|
|
|
vector<int> vec(size());
|
|
|
|
|
for (int i = 0; i < size(); i++) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
vec[i] = arr[i];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
return vec;
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="my_list.java"
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 列表类 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
class MyList {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
private int[] arr; // 数组(存储列表元素)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
private int capacity = 10; // 列表容量
|
2023-12-02 06:24:05 +08:00
|
|
|
|
private int size = 0; // 列表长度(当前元素数量)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
private int extendRatio = 2; // 每次列表扩容的倍数
|
|
|
|
|
|
|
|
|
|
/* 构造方法 */
|
|
|
|
|
public MyList() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr = new int[capacity];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 获取列表长度(当前元素数量) */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
public int size() {
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表容量 */
|
|
|
|
|
public int capacity() {
|
|
|
|
|
return capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
|
public int get(int index) {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 索引如果越界,则抛出异常,下同
|
2023-10-06 14:10:18 +08:00
|
|
|
|
if (index < 0 || index >= size)
|
|
|
|
|
throw new IndexOutOfBoundsException("索引越界");
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arr[index];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
|
|
|
|
public void set(int index, int num) {
|
|
|
|
|
if (index < 0 || index >= size)
|
|
|
|
|
throw new IndexOutOfBoundsException("索引越界");
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
public void add(int num) {
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if (size == capacity())
|
|
|
|
|
extendCapacity();
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[size] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
|
|
|
|
size++;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
public void insert(int index, int num) {
|
|
|
|
|
if (index < 0 || index >= size)
|
|
|
|
|
throw new IndexOutOfBoundsException("索引越界");
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if (size == capacity())
|
|
|
|
|
extendCapacity();
|
|
|
|
|
// 将索引 index 以及之后的元素都向后移动一位
|
|
|
|
|
for (int j = size - 1; j >= index; j--) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[j + 1] = arr[j];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
|
|
|
|
size++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
|
|
|
|
public int remove(int index) {
|
|
|
|
|
if (index < 0 || index >= size)
|
|
|
|
|
throw new IndexOutOfBoundsException("索引越界");
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int num = arr[index];
|
2024-01-07 03:59:56 +08:00
|
|
|
|
// 将将索引 index 之后的元素都向前移动一位
|
2023-10-06 14:10:18 +08:00
|
|
|
|
for (int j = index; j < size - 1; j++) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[j] = arr[j + 1];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
// 更新元素数量
|
|
|
|
|
size--;
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 返回被删除的元素
|
2023-10-06 14:10:18 +08:00
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 列表扩容 */
|
|
|
|
|
public void extendCapacity() {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr = Arrays.copyOf(arr, capacity() * extendRatio);
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新列表容量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
capacity = arr.length;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将列表转换为数组 */
|
|
|
|
|
public int[] toArray() {
|
|
|
|
|
int size = size();
|
|
|
|
|
// 仅转换有效长度范围内的列表元素
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int[] arr = new int[size];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
for (int i = 0; i < size; i++) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[i] = get(i);
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arr;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="my_list.cs"
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 列表类 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
class MyList {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
private int[] arr; // 数组(存储列表元素)
|
|
|
|
|
private int arrCapacity = 10; // 列表容量
|
2023-12-02 06:24:05 +08:00
|
|
|
|
private int arrSize = 0; // 列表长度(当前元素数量)
|
2023-10-08 01:43:28 +08:00
|
|
|
|
private readonly int extendRatio = 2; // 每次列表扩容的倍数
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
/* 构造方法 */
|
|
|
|
|
public MyList() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr = new int[arrCapacity];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 获取列表长度(当前元素数量)*/
|
2023-10-08 01:43:28 +08:00
|
|
|
|
public int Size() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arrSize;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表容量 */
|
2023-10-08 01:43:28 +08:00
|
|
|
|
public int Capacity() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arrCapacity;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
2023-10-08 01:43:28 +08:00
|
|
|
|
public int Get(int index) {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 索引如果越界,则抛出异常,下同
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if (index < 0 || index >= arrSize)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
throw new IndexOutOfRangeException("索引越界");
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arr[index];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-10-08 01:43:28 +08:00
|
|
|
|
public void Set(int index, int num) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if (index < 0 || index >= arrSize)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
throw new IndexOutOfRangeException("索引越界");
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-08 01:43:28 +08:00
|
|
|
|
public void Add(int num) {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if (arrSize == arrCapacity)
|
2023-10-08 01:43:28 +08:00
|
|
|
|
ExtendCapacity();
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[arrSize] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arrSize++;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-08 01:43:28 +08:00
|
|
|
|
public void Insert(int index, int num) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if (index < 0 || index >= arrSize)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
throw new IndexOutOfRangeException("索引越界");
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if (arrSize == arrCapacity)
|
2023-10-08 01:43:28 +08:00
|
|
|
|
ExtendCapacity();
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 将索引 index 以及之后的元素都向后移动一位
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for (int j = arrSize - 1; j >= index; j--) {
|
|
|
|
|
arr[j + 1] = arr[j];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arrSize++;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-08 01:43:28 +08:00
|
|
|
|
public int Remove(int index) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if (index < 0 || index >= arrSize)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
throw new IndexOutOfRangeException("索引越界");
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int num = arr[index];
|
2024-01-07 03:59:56 +08:00
|
|
|
|
// 将将索引 index 之后的元素都向前移动一位
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for (int j = index; j < arrSize - 1; j++) {
|
|
|
|
|
arr[j] = arr[j + 1];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
// 更新元素数量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arrSize--;
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 返回被删除的元素
|
2023-10-06 14:10:18 +08:00
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 列表扩容 */
|
2023-10-08 01:43:28 +08:00
|
|
|
|
public void ExtendCapacity() {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 新建一个长度为 arrCapacity * extendRatio 的数组,并将原数组复制到新数组
|
2023-10-09 18:19:22 +08:00
|
|
|
|
Array.Resize(ref arr, arrCapacity * extendRatio);
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新列表容量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arrCapacity = arr.Length;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将列表转换为数组 */
|
2023-10-08 01:43:28 +08:00
|
|
|
|
public int[] ToArray() {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 仅转换有效长度范围内的列表元素
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int[] arr = new int[arrSize];
|
|
|
|
|
for (int i = 0; i < arrSize; i++) {
|
|
|
|
|
arr[i] = Get(i);
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arr;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="my_list.go"
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 列表类 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
type myList struct {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arrCapacity int
|
|
|
|
|
arr []int
|
|
|
|
|
arrSize int
|
|
|
|
|
extendRatio int
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 构造函数 */
|
|
|
|
|
func newMyList() *myList {
|
|
|
|
|
return &myList{
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arrCapacity: 10, // 列表容量
|
|
|
|
|
arr: make([]int, 10), // 数组(存储列表元素)
|
2023-12-02 06:24:05 +08:00
|
|
|
|
arrSize: 0, // 列表长度(当前元素数量)
|
2023-10-09 18:19:22 +08:00
|
|
|
|
extendRatio: 2, // 每次列表扩容的倍数
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 获取列表长度(当前元素数量) */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
func (l *myList) size() int {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return l.arrSize
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表容量 */
|
|
|
|
|
func (l *myList) capacity() int {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return l.arrCapacity
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
|
func (l *myList) get(index int) int {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 索引如果越界,则抛出异常,下同
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if index < 0 || index >= l.arrSize {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
panic("索引越界")
|
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return l.arr[index]
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
|
|
|
|
func (l *myList) set(num, index int) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if index < 0 || index >= l.arrSize {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
panic("索引越界")
|
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
l.arr[index] = num
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
func (l *myList) add(num int) {
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if l.arrSize == l.arrCapacity {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
l.extendCapacity()
|
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
l.arr[l.arrSize] = num
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
l.arrSize++
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
func (l *myList) insert(num, index int) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if index < 0 || index >= l.arrSize {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
panic("索引越界")
|
|
|
|
|
}
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if l.arrSize == l.arrCapacity {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
l.extendCapacity()
|
|
|
|
|
}
|
|
|
|
|
// 将索引 index 以及之后的元素都向后移动一位
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for j := l.arrSize - 1; j >= index; j-- {
|
|
|
|
|
l.arr[j+1] = l.arr[j]
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
l.arr[index] = num
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
l.arrSize++
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
|
|
|
|
func (l *myList) remove(index int) int {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if index < 0 || index >= l.arrSize {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
panic("索引越界")
|
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
num := l.arr[index]
|
2024-01-07 03:59:56 +08:00
|
|
|
|
// 将索引 index 之后的元素都向前移动一位
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for j := index; j < l.arrSize-1; j++ {
|
|
|
|
|
l.arr[j] = l.arr[j+1]
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
// 更新元素数量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
l.arrSize--
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 返回被删除的元素
|
2023-10-06 14:10:18 +08:00
|
|
|
|
return num
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 列表扩容 */
|
|
|
|
|
func (l *myList) extendCapacity() {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组
|
2023-10-09 18:19:22 +08:00
|
|
|
|
l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新列表容量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
l.arrCapacity = len(l.arr)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 返回有效长度的列表 */
|
|
|
|
|
func (l *myList) toArray() []int {
|
|
|
|
|
// 仅转换有效长度范围内的列表元素
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return l.arr[:l.arrSize]
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="my_list.swift"
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 列表类 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
class MyList {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
private var arr: [Int] // 数组(存储列表元素)
|
2024-03-21 04:22:07 +08:00
|
|
|
|
private var _capacity: Int // 列表容量
|
|
|
|
|
private var _size: Int // 列表长度(当前元素数量)
|
|
|
|
|
private let extendRatio: Int // 每次列表扩容的倍数
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
/* 构造方法 */
|
|
|
|
|
init() {
|
2024-03-21 04:22:07 +08:00
|
|
|
|
_capacity = 10
|
|
|
|
|
_size = 0
|
|
|
|
|
extendRatio = 2
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr = Array(repeating: 0, count: _capacity)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 获取列表长度(当前元素数量)*/
|
2023-10-06 14:10:18 +08:00
|
|
|
|
func size() -> Int {
|
|
|
|
|
_size
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表容量 */
|
|
|
|
|
func capacity() -> Int {
|
|
|
|
|
_capacity
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
|
func get(index: Int) -> Int {
|
|
|
|
|
// 索引如果越界则抛出错误,下同
|
2024-03-21 04:22:07 +08:00
|
|
|
|
if index < 0 || index >= size() {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
fatalError("索引越界")
|
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arr[index]
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
|
|
|
|
func set(index: Int, num: Int) {
|
2024-03-21 04:22:07 +08:00
|
|
|
|
if index < 0 || index >= size() {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
fatalError("索引越界")
|
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[index] = num
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
func add(num: Int) {
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
2024-03-21 04:22:07 +08:00
|
|
|
|
if size() == capacity() {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
extendCapacity()
|
|
|
|
|
}
|
2024-03-21 04:22:07 +08:00
|
|
|
|
arr[size()] = num
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
|
|
|
|
_size += 1
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
func insert(index: Int, num: Int) {
|
2024-03-21 04:22:07 +08:00
|
|
|
|
if index < 0 || index >= size() {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
fatalError("索引越界")
|
|
|
|
|
}
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
2024-03-21 04:22:07 +08:00
|
|
|
|
if size() == capacity() {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
extendCapacity()
|
|
|
|
|
}
|
|
|
|
|
// 将索引 index 以及之后的元素都向后移动一位
|
2024-03-21 04:22:07 +08:00
|
|
|
|
for j in (index ..< size()).reversed() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[j + 1] = arr[j]
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[index] = num
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
|
|
|
|
_size += 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
|
|
|
|
@discardableResult
|
|
|
|
|
func remove(index: Int) -> Int {
|
2024-03-21 04:22:07 +08:00
|
|
|
|
if index < 0 || index >= size() {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
fatalError("索引越界")
|
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
let num = arr[index]
|
2024-01-07 03:59:56 +08:00
|
|
|
|
// 将将索引 index 之后的元素都向前移动一位
|
2024-03-21 04:22:07 +08:00
|
|
|
|
for j in index ..< (size() - 1) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[j] = arr[j + 1]
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
// 更新元素数量
|
|
|
|
|
_size -= 1
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 返回被删除的元素
|
2023-10-06 14:10:18 +08:00
|
|
|
|
return num
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 列表扩容 */
|
|
|
|
|
func extendCapacity() {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组
|
2024-03-21 04:22:07 +08:00
|
|
|
|
arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新列表容量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
_capacity = arr.count
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将列表转换为数组 */
|
|
|
|
|
func toArray() -> [Int] {
|
2024-03-21 04:22:07 +08:00
|
|
|
|
Array(arr.prefix(size()))
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JS"
|
|
|
|
|
|
|
|
|
|
```javascript title="my_list.js"
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 列表类 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
class MyList {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
#arr = new Array(); // 数组(存储列表元素)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
#capacity = 10; // 列表容量
|
2023-12-02 06:24:05 +08:00
|
|
|
|
#size = 0; // 列表长度(当前元素数量)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
#extendRatio = 2; // 每次列表扩容的倍数
|
|
|
|
|
|
|
|
|
|
/* 构造方法 */
|
|
|
|
|
constructor() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.#arr = new Array(this.#capacity);
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 获取列表长度(当前元素数量)*/
|
2023-10-06 14:10:18 +08:00
|
|
|
|
size() {
|
|
|
|
|
return this.#size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表容量 */
|
|
|
|
|
capacity() {
|
|
|
|
|
return this.#capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
|
get(index) {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 索引如果越界,则抛出异常,下同
|
2023-10-06 14:10:18 +08:00
|
|
|
|
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return this.#arr[index];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
|
|
|
|
set(index, num) {
|
|
|
|
|
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.#arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
add(num) {
|
|
|
|
|
// 如果长度等于容量,则需要扩容
|
|
|
|
|
if (this.#size === this.#capacity) {
|
|
|
|
|
this.extendCapacity();
|
|
|
|
|
}
|
|
|
|
|
// 将新元素添加到列表尾部
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.#arr[this.#size] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
this.#size++;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
insert(index, num) {
|
|
|
|
|
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if (this.#size === this.#capacity) {
|
|
|
|
|
this.extendCapacity();
|
|
|
|
|
}
|
|
|
|
|
// 将索引 index 以及之后的元素都向后移动一位
|
|
|
|
|
for (let j = this.#size - 1; j >= index; j--) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.#arr[j + 1] = this.#arr[j];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
// 更新元素数量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.#arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
this.#size++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
|
|
|
|
remove(index) {
|
|
|
|
|
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
2023-10-09 18:19:22 +08:00
|
|
|
|
let num = this.#arr[index];
|
2024-01-07 03:59:56 +08:00
|
|
|
|
// 将将索引 index 之后的元素都向前移动一位
|
2023-10-06 14:10:18 +08:00
|
|
|
|
for (let j = index; j < this.#size - 1; j++) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.#arr[j] = this.#arr[j + 1];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
// 更新元素数量
|
|
|
|
|
this.#size--;
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 返回被删除的元素
|
2023-10-06 14:10:18 +08:00
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 列表扩容 */
|
|
|
|
|
extendCapacity() {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.#arr = this.#arr.concat(
|
2023-10-06 14:10:18 +08:00
|
|
|
|
new Array(this.capacity() * (this.#extendRatio - 1))
|
|
|
|
|
);
|
|
|
|
|
// 更新列表容量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.#capacity = this.#arr.length;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将列表转换为数组 */
|
|
|
|
|
toArray() {
|
|
|
|
|
let size = this.size();
|
|
|
|
|
// 仅转换有效长度范围内的列表元素
|
2023-10-09 18:19:22 +08:00
|
|
|
|
const arr = new Array(size);
|
2023-10-06 14:10:18 +08:00
|
|
|
|
for (let i = 0; i < size; i++) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[i] = this.get(i);
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arr;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TS"
|
|
|
|
|
|
|
|
|
|
```typescript title="my_list.ts"
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 列表类 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
class MyList {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
private arr: Array<number>; // 数组(存储列表元素)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
private _capacity: number = 10; // 列表容量
|
2023-12-02 06:24:05 +08:00
|
|
|
|
private _size: number = 0; // 列表长度(当前元素数量)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
private extendRatio: number = 2; // 每次列表扩容的倍数
|
|
|
|
|
|
|
|
|
|
/* 构造方法 */
|
|
|
|
|
constructor() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.arr = new Array(this._capacity);
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 获取列表长度(当前元素数量)*/
|
2023-10-06 14:10:18 +08:00
|
|
|
|
public size(): number {
|
|
|
|
|
return this._size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表容量 */
|
|
|
|
|
public capacity(): number {
|
|
|
|
|
return this._capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
|
public get(index: number): number {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 索引如果越界,则抛出异常,下同
|
2023-10-06 14:10:18 +08:00
|
|
|
|
if (index < 0 || index >= this._size) throw new Error('索引越界');
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return this.arr[index];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
|
|
|
|
public set(index: number, num: number): void {
|
|
|
|
|
if (index < 0 || index >= this._size) throw new Error('索引越界');
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
public add(num: number): void {
|
|
|
|
|
// 如果长度等于容量,则需要扩容
|
|
|
|
|
if (this._size === this._capacity) this.extendCapacity();
|
|
|
|
|
// 将新元素添加到列表尾部
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.arr[this._size] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
this._size++;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
public insert(index: number, num: number): void {
|
|
|
|
|
if (index < 0 || index >= this._size) throw new Error('索引越界');
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if (this._size === this._capacity) {
|
|
|
|
|
this.extendCapacity();
|
|
|
|
|
}
|
|
|
|
|
// 将索引 index 以及之后的元素都向后移动一位
|
|
|
|
|
for (let j = this._size - 1; j >= index; j--) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.arr[j + 1] = this.arr[j];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
// 更新元素数量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
this._size++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
|
|
|
|
public remove(index: number): number {
|
|
|
|
|
if (index < 0 || index >= this._size) throw new Error('索引越界');
|
2023-10-09 18:19:22 +08:00
|
|
|
|
let num = this.arr[index];
|
2024-01-07 03:59:56 +08:00
|
|
|
|
// 将将索引 index 之后的元素都向前移动一位
|
2023-10-06 14:10:18 +08:00
|
|
|
|
for (let j = index; j < this._size - 1; j++) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.arr[j] = this.arr[j + 1];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
// 更新元素数量
|
|
|
|
|
this._size--;
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 返回被删除的元素
|
2023-10-06 14:10:18 +08:00
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 列表扩容 */
|
|
|
|
|
public extendCapacity(): void {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 新建一个长度为 size 的数组,并将原数组复制到新数组
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this.arr = this.arr.concat(
|
2023-10-06 14:10:18 +08:00
|
|
|
|
new Array(this.capacity() * (this.extendRatio - 1))
|
|
|
|
|
);
|
|
|
|
|
// 更新列表容量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
this._capacity = this.arr.length;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将列表转换为数组 */
|
|
|
|
|
public toArray(): number[] {
|
|
|
|
|
let size = this.size();
|
|
|
|
|
// 仅转换有效长度范围内的列表元素
|
2023-10-09 18:19:22 +08:00
|
|
|
|
const arr = new Array(size);
|
2023-10-06 14:10:18 +08:00
|
|
|
|
for (let i = 0; i < size; i++) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr[i] = this.get(i);
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arr;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="my_list.dart"
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 列表类 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
class MyList {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
late List<int> _arr; // 数组(存储列表元素)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
int _capacity = 10; // 列表容量
|
2023-12-02 06:24:05 +08:00
|
|
|
|
int _size = 0; // 列表长度(当前元素数量)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
int _extendRatio = 2; // 每次列表扩容的倍数
|
|
|
|
|
|
|
|
|
|
/* 构造方法 */
|
|
|
|
|
MyList() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
_arr = List.filled(_capacity, 0);
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 获取列表长度(当前元素数量)*/
|
2023-10-06 14:10:18 +08:00
|
|
|
|
int size() => _size;
|
|
|
|
|
|
|
|
|
|
/* 获取列表容量 */
|
|
|
|
|
int capacity() => _capacity;
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
|
int get(int index) {
|
|
|
|
|
if (index >= _size) throw RangeError('索引越界');
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return _arr[index];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-11-17 00:35:19 +08:00
|
|
|
|
void set(int index, int _num) {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
if (index >= _size) throw RangeError('索引越界');
|
2023-11-17 00:35:19 +08:00
|
|
|
|
_arr[index] = _num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-11-17 00:35:19 +08:00
|
|
|
|
void add(int _num) {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if (_size == _capacity) extendCapacity();
|
2023-11-17 00:35:19 +08:00
|
|
|
|
_arr[_size] = _num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
|
|
|
|
_size++;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-11-17 00:35:19 +08:00
|
|
|
|
void insert(int index, int _num) {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
if (index >= _size) throw RangeError('索引越界');
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if (_size == _capacity) extendCapacity();
|
|
|
|
|
// 将索引 index 以及之后的元素都向后移动一位
|
|
|
|
|
for (var j = _size - 1; j >= index; j--) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
_arr[j + 1] = _arr[j];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-11-17 00:35:19 +08:00
|
|
|
|
_arr[index] = _num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
|
|
|
|
_size++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
|
|
|
|
int remove(int index) {
|
|
|
|
|
if (index >= _size) throw RangeError('索引越界');
|
2023-11-17 00:35:19 +08:00
|
|
|
|
int _num = _arr[index];
|
2024-01-07 03:59:56 +08:00
|
|
|
|
// 将将索引 index 之后的元素都向前移动一位
|
2023-10-06 14:10:18 +08:00
|
|
|
|
for (var j = index; j < _size - 1; j++) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
_arr[j] = _arr[j + 1];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
// 更新元素数量
|
|
|
|
|
_size--;
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 返回被删除的元素
|
2023-11-17 00:35:19 +08:00
|
|
|
|
return _num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 列表扩容 */
|
|
|
|
|
void extendCapacity() {
|
|
|
|
|
// 新建一个长度为原数组 _extendRatio 倍的新数组
|
|
|
|
|
final _newNums = List.filled(_capacity * _extendRatio, 0);
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 将原数组复制到新数组
|
2023-10-09 18:19:22 +08:00
|
|
|
|
List.copyRange(_newNums, 0, _arr);
|
|
|
|
|
// 更新 _arr 的引用
|
|
|
|
|
_arr = _newNums;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新列表容量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
_capacity = _arr.length;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将列表转换为数组 */
|
|
|
|
|
List<int> toArray() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
List<int> arr = [];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
for (var i = 0; i < _size; i++) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr.add(get(i));
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arr;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="my_list.rs"
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 列表类 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
struct MyList {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr: Vec<i32>, // 数组(存储列表元素)
|
2024-03-18 03:11:07 +08:00
|
|
|
|
capacity: usize, // 列表容量
|
|
|
|
|
size: usize, // 列表长度(当前元素数量)
|
|
|
|
|
extend_ratio: usize, // 每次列表扩容的倍数
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-18 03:11:07 +08:00
|
|
|
|
#[allow(unused, unused_comparisons)]
|
2023-10-06 14:10:18 +08:00
|
|
|
|
impl MyList {
|
|
|
|
|
/* 构造方法 */
|
|
|
|
|
pub fn new(capacity: usize) -> Self {
|
2024-03-18 03:11:07 +08:00
|
|
|
|
let mut vec = Vec::new();
|
2023-10-06 14:10:18 +08:00
|
|
|
|
vec.resize(capacity, 0);
|
|
|
|
|
Self {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr: vec,
|
2023-10-06 14:10:18 +08:00
|
|
|
|
capacity,
|
|
|
|
|
size: 0,
|
|
|
|
|
extend_ratio: 2,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 获取列表长度(当前元素数量)*/
|
2023-10-06 14:10:18 +08:00
|
|
|
|
pub fn size(&self) -> usize {
|
|
|
|
|
return self.size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表容量 */
|
|
|
|
|
pub fn capacity(&self) -> usize {
|
|
|
|
|
return self.capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
|
pub fn get(&self, index: usize) -> i32 {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 索引如果越界,则抛出异常,下同
|
2024-03-18 03:11:07 +08:00
|
|
|
|
if index >= self.size {
|
|
|
|
|
panic!("索引越界")
|
|
|
|
|
};
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return self.arr[index];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
|
|
|
|
pub fn set(&mut self, index: usize, num: i32) {
|
2024-03-18 03:11:07 +08:00
|
|
|
|
if index >= self.size {
|
|
|
|
|
panic!("索引越界")
|
|
|
|
|
};
|
2023-10-09 18:19:22 +08:00
|
|
|
|
self.arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
pub fn add(&mut self, num: i32) {
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if self.size == self.capacity() {
|
|
|
|
|
self.extend_capacity();
|
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
self.arr[self.size] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
|
|
|
|
self.size += 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-06 14:10:18 +08:00
|
|
|
|
pub fn insert(&mut self, index: usize, num: i32) {
|
2024-03-18 03:11:07 +08:00
|
|
|
|
if index >= self.size() {
|
|
|
|
|
panic!("索引越界")
|
|
|
|
|
};
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if self.size == self.capacity() {
|
|
|
|
|
self.extend_capacity();
|
|
|
|
|
}
|
|
|
|
|
// 将索引 index 以及之后的元素都向后移动一位
|
|
|
|
|
for j in (index..self.size).rev() {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
self.arr[j + 1] = self.arr[j];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
self.arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
|
|
|
|
self.size += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
|
|
|
|
pub fn remove(&mut self, index: usize) -> i32 {
|
2024-03-18 03:11:07 +08:00
|
|
|
|
if index >= self.size() {
|
|
|
|
|
panic!("索引越界")
|
|
|
|
|
};
|
2023-10-09 18:19:22 +08:00
|
|
|
|
let num = self.arr[index];
|
2024-01-07 03:59:56 +08:00
|
|
|
|
// 将将索引 index 之后的元素都向前移动一位
|
2023-10-06 14:10:18 +08:00
|
|
|
|
for j in (index..self.size - 1) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
self.arr[j] = self.arr[j + 1];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
// 更新元素数量
|
|
|
|
|
self.size -= 1;
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 返回被删除的元素
|
2023-10-06 14:10:18 +08:00
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 列表扩容 */
|
|
|
|
|
pub fn extend_capacity(&mut self) {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 新建一个长度为原数组 extend_ratio 倍的新数组,并将原数组复制到新数组
|
2023-10-06 14:10:18 +08:00
|
|
|
|
let new_capacity = self.capacity * self.extend_ratio;
|
2023-10-09 18:19:22 +08:00
|
|
|
|
self.arr.resize(new_capacity, 0);
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新列表容量
|
|
|
|
|
self.capacity = new_capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将列表转换为数组 */
|
|
|
|
|
pub fn to_array(&mut self) -> Vec<i32> {
|
|
|
|
|
// 仅转换有效长度范围内的列表元素
|
2023-10-09 18:19:22 +08:00
|
|
|
|
let mut arr = Vec::new();
|
2023-10-06 14:10:18 +08:00
|
|
|
|
for i in 0..self.size {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr.push(self.get(i));
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="my_list.c"
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 列表类 */
|
2023-10-18 02:16:55 +08:00
|
|
|
|
typedef struct {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int *arr; // 数组(存储列表元素)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
int capacity; // 列表容量
|
|
|
|
|
int size; // 列表大小
|
|
|
|
|
int extendRatio; // 列表每次扩容的倍数
|
2023-10-18 02:16:55 +08:00
|
|
|
|
} MyList;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
/* 构造函数 */
|
2023-10-18 02:16:55 +08:00
|
|
|
|
MyList *newMyList() {
|
|
|
|
|
MyList *nums = malloc(sizeof(MyList));
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums->capacity = 10;
|
|
|
|
|
nums->arr = malloc(sizeof(int) * nums->capacity);
|
|
|
|
|
nums->size = 0;
|
|
|
|
|
nums->extendRatio = 2;
|
|
|
|
|
return nums;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 析构函数 */
|
2023-10-18 02:16:55 +08:00
|
|
|
|
void delMyList(MyList *nums) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
free(nums->arr);
|
|
|
|
|
free(nums);
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表长度 */
|
2023-10-18 02:16:55 +08:00
|
|
|
|
int size(MyList *nums) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return nums->size;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表容量 */
|
2023-10-18 02:16:55 +08:00
|
|
|
|
int capacity(MyList *nums) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return nums->capacity;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
2023-10-18 02:16:55 +08:00
|
|
|
|
int get(MyList *nums, int index) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
assert(index >= 0 && index < nums->size);
|
|
|
|
|
return nums->arr[index];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-10-18 02:16:55 +08:00
|
|
|
|
void set(MyList *nums, int index, int num) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
assert(index >= 0 && index < nums->size);
|
|
|
|
|
nums->arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-18 02:16:55 +08:00
|
|
|
|
void add(MyList *nums, int num) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if (size(nums) == capacity(nums)) {
|
|
|
|
|
extendCapacity(nums); // 扩容
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums->arr[size(nums)] = num;
|
|
|
|
|
nums->size++;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-18 02:16:55 +08:00
|
|
|
|
void insert(MyList *nums, int index, int num) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
assert(index >= 0 && index < size(nums));
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
2023-10-09 18:19:22 +08:00
|
|
|
|
if (size(nums) == capacity(nums)) {
|
|
|
|
|
extendCapacity(nums); // 扩容
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for (int i = size(nums); i > index; --i) {
|
|
|
|
|
nums->arr[i] = nums->arr[i - 1];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums->arr[index] = num;
|
|
|
|
|
nums->size++;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
|
|
|
|
// 注意:stdio.h 占用了 remove 关键词
|
2023-11-01 05:12:56 +08:00
|
|
|
|
int removeItem(MyList *nums, int index) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
assert(index >= 0 && index < size(nums));
|
|
|
|
|
int num = nums->arr[index];
|
|
|
|
|
for (int i = index; i < size(nums) - 1; i++) {
|
|
|
|
|
nums->arr[i] = nums->arr[i + 1];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums->size--;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 列表扩容 */
|
2023-10-18 02:16:55 +08:00
|
|
|
|
void extendCapacity(MyList *nums) {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 先分配空间
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int newCapacity = capacity(nums) * nums->extendRatio;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
int *extend = (int *)malloc(sizeof(int) * newCapacity);
|
2023-10-09 18:19:22 +08:00
|
|
|
|
int *temp = nums->arr;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
// 拷贝旧数据到新数据
|
2023-10-09 18:19:22 +08:00
|
|
|
|
for (int i = 0; i < size(nums); i++)
|
|
|
|
|
extend[i] = nums->arr[i];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
|
|
|
|
|
// 释放旧数据
|
|
|
|
|
free(temp);
|
|
|
|
|
|
|
|
|
|
// 更新新数据
|
2023-10-09 18:19:22 +08:00
|
|
|
|
nums->arr = extend;
|
|
|
|
|
nums->capacity = newCapacity;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将列表转换为 Array 用于打印 */
|
2023-10-18 02:16:55 +08:00
|
|
|
|
int *toArray(MyList *nums) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return nums->arr;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="my_list.kt"
|
|
|
|
|
/* 列表类 */
|
|
|
|
|
class MyList {
|
|
|
|
|
private var arr: IntArray = intArrayOf() // 数组(存储列表元素)
|
|
|
|
|
private var capacity = 10 // 列表容量
|
|
|
|
|
private var size = 0 // 列表长度(当前元素数量)
|
|
|
|
|
private var extendRatio = 2 // 每次列表扩容的倍数
|
|
|
|
|
|
|
|
|
|
/* 构造函数 */
|
|
|
|
|
init {
|
|
|
|
|
arr = IntArray(capacity)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表长度(当前元素数量) */
|
|
|
|
|
fun size(): Int {
|
|
|
|
|
return size
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表容量 */
|
|
|
|
|
fun capacity(): Int {
|
|
|
|
|
return capacity
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
|
fun get(index: Int): Int {
|
|
|
|
|
// 索引如果越界,则抛出异常,下同
|
|
|
|
|
if (index < 0 || index >= size)
|
|
|
|
|
throw IndexOutOfBoundsException()
|
|
|
|
|
return arr[index]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
|
|
|
|
fun set(index: Int, num: Int) {
|
|
|
|
|
if (index < 0 || index >= size)
|
|
|
|
|
throw IndexOutOfBoundsException("索引越界")
|
|
|
|
|
arr[index] = num
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 在尾部添加元素 */
|
|
|
|
|
fun add(num: Int) {
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if (size == capacity())
|
|
|
|
|
extendCapacity()
|
|
|
|
|
arr[size] = num
|
|
|
|
|
// 更新元素数量
|
|
|
|
|
size++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 在中间插入元素 */
|
|
|
|
|
fun insert(index: Int, num: Int) {
|
|
|
|
|
if (index < 0 || index >= size)
|
|
|
|
|
throw IndexOutOfBoundsException("索引越界")
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if (size == capacity())
|
|
|
|
|
extendCapacity()
|
|
|
|
|
// 将索引 index 以及之后的元素都向后移动一位
|
|
|
|
|
for (j in size - 1 downTo index)
|
|
|
|
|
arr[j + 1] = arr[j]
|
|
|
|
|
arr[index] = num
|
|
|
|
|
// 更新元素数量
|
|
|
|
|
size++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
|
|
|
|
fun remove(index: Int): Int {
|
|
|
|
|
if (index < 0 || index >= size)
|
|
|
|
|
throw IndexOutOfBoundsException("索引越界")
|
|
|
|
|
val num: Int = arr[index]
|
|
|
|
|
// 将将索引 index 之后的元素都向前移动一位
|
|
|
|
|
for (j in index..<size - 1)
|
|
|
|
|
arr[j] = arr[j + 1]
|
|
|
|
|
// 更新元素数量
|
|
|
|
|
size--
|
|
|
|
|
// 返回被删除的元素
|
|
|
|
|
return num
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 列表扩容 */
|
|
|
|
|
fun extendCapacity() {
|
|
|
|
|
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组
|
|
|
|
|
arr = arr.copyOf(capacity() * extendRatio)
|
|
|
|
|
// 更新列表容量
|
|
|
|
|
capacity = arr.size
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将列表转换为数组 */
|
|
|
|
|
fun toArray(): IntArray {
|
|
|
|
|
val size = size()
|
|
|
|
|
// 仅转换有效长度范围内的列表元素
|
|
|
|
|
val arr = IntArray(size)
|
|
|
|
|
for (i in 0..<size) {
|
|
|
|
|
arr[i] = get(i)
|
|
|
|
|
}
|
|
|
|
|
return arr
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-10-06 13:31:21 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="my_list.zig"
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 列表类
|
2023-10-06 14:10:18 +08:00
|
|
|
|
fn MyList(comptime T: type) type {
|
|
|
|
|
return struct {
|
|
|
|
|
const Self = @This();
|
|
|
|
|
|
2023-10-09 18:19:22 +08:00
|
|
|
|
arr: []T = undefined, // 数组(存储列表元素)
|
|
|
|
|
arrCapacity: usize = 10, // 列表容量
|
2023-12-02 06:24:05 +08:00
|
|
|
|
numSize: usize = 0, // 列表长度(当前元素数量)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
extendRatio: usize = 2, // 每次列表扩容的倍数
|
|
|
|
|
mem_arena: ?std.heap.ArenaAllocator = null,
|
|
|
|
|
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
|
|
|
|
|
|
|
|
|
// 构造函数(分配内存+初始化列表)
|
|
|
|
|
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
|
|
|
|
if (self.mem_arena == null) {
|
|
|
|
|
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
|
self.mem_allocator = self.mem_arena.?.allocator();
|
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
self.arr = try self.mem_allocator.alloc(T, self.arrCapacity);
|
|
|
|
|
@memset(self.arr, @as(T, 0));
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 析构函数(释放内存)
|
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
|
|
|
if (self.mem_arena == null) return;
|
|
|
|
|
self.mem_arena.?.deinit();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 获取列表长度(当前元素数量)
|
2023-10-06 14:10:18 +08:00
|
|
|
|
pub fn size(self: *Self) usize {
|
|
|
|
|
return self.numSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取列表容量
|
|
|
|
|
pub fn capacity(self: *Self) usize {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return self.arrCapacity;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 访问元素
|
|
|
|
|
pub fn get(self: *Self, index: usize) T {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 索引如果越界,则抛出异常,下同
|
2023-10-06 14:10:18 +08:00
|
|
|
|
if (index < 0 or index >= self.size()) @panic("索引越界");
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return self.arr[index];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新元素
|
|
|
|
|
pub fn set(self: *Self, index: usize, num: T) void {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 索引如果越界,则抛出异常,下同
|
2023-10-06 14:10:18 +08:00
|
|
|
|
if (index < 0 or index >= self.size()) @panic("索引越界");
|
2023-10-09 18:19:22 +08:00
|
|
|
|
self.arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 在尾部添加元素
|
2023-10-06 14:10:18 +08:00
|
|
|
|
pub fn add(self: *Self, num: T) !void {
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if (self.size() == self.capacity()) try self.extendCapacity();
|
2023-10-09 18:19:22 +08:00
|
|
|
|
self.arr[self.size()] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
|
|
|
|
self.numSize += 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 在中间插入元素
|
2023-10-06 14:10:18 +08:00
|
|
|
|
pub fn insert(self: *Self, index: usize, num: T) !void {
|
|
|
|
|
if (index < 0 or index >= self.size()) @panic("索引越界");
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if (self.size() == self.capacity()) try self.extendCapacity();
|
|
|
|
|
// 将索引 index 以及之后的元素都向后移动一位
|
|
|
|
|
var j = self.size() - 1;
|
|
|
|
|
while (j >= index) : (j -= 1) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
self.arr[j + 1] = self.arr[j];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
self.arr[index] = num;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新元素数量
|
|
|
|
|
self.numSize += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除元素
|
|
|
|
|
pub fn remove(self: *Self, index: usize) T {
|
|
|
|
|
if (index < 0 or index >= self.size()) @panic("索引越界");
|
2023-10-09 18:19:22 +08:00
|
|
|
|
var num = self.arr[index];
|
2024-01-07 03:59:56 +08:00
|
|
|
|
// 将索引 index 之后的元素都向前移动一位
|
2023-10-06 14:10:18 +08:00
|
|
|
|
var j = index;
|
|
|
|
|
while (j < self.size() - 1) : (j += 1) {
|
2023-10-09 18:19:22 +08:00
|
|
|
|
self.arr[j] = self.arr[j + 1];
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
// 更新元素数量
|
|
|
|
|
self.numSize -= 1;
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 返回被删除的元素
|
2023-10-06 14:10:18 +08:00
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 列表扩容
|
|
|
|
|
pub fn extendCapacity(self: *Self) !void {
|
2023-12-28 17:18:37 +08:00
|
|
|
|
// 新建一个长度为 size * extendRatio 的数组,并将原数组复制到新数组
|
2023-10-06 14:10:18 +08:00
|
|
|
|
var newCapacity = self.capacity() * self.extendRatio;
|
|
|
|
|
var extend = try self.mem_allocator.alloc(T, newCapacity);
|
|
|
|
|
@memset(extend, @as(T, 0));
|
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
2023-10-09 18:19:22 +08:00
|
|
|
|
std.mem.copy(T, extend, self.arr);
|
|
|
|
|
self.arr = extend;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 更新列表容量
|
2023-10-09 18:19:22 +08:00
|
|
|
|
self.arrCapacity = newCapacity;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将列表转换为数组
|
|
|
|
|
pub fn toArray(self: *Self) ![]T {
|
|
|
|
|
// 仅转换有效长度范围内的列表元素
|
2023-10-09 18:19:22 +08:00
|
|
|
|
var arr = try self.mem_allocator.alloc(T, self.size());
|
|
|
|
|
@memset(arr, @as(T, 0));
|
|
|
|
|
for (arr, 0..) |*num, i| {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
num.* = self.get(i);
|
|
|
|
|
}
|
2023-10-09 18:19:22 +08:00
|
|
|
|
return arr;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
2024-01-07 03:59:56 +08:00
|
|
|
|
|
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:00:24 +08:00
|
|
|
|
<div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=class%20MyList%3A%0A%20%20%20%20%22%22%22%E5%88%97%E8%A1%A8%E7%B1%BB%22%22%22%0A%0A%20%20%20%20def%20__init__%28self%29%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95%22%22%22%0A%20%20%20%20%20%20%20%20self._capacity%3A%20int%20%3D%2010%0A%20%20%20%20%20%20%20%20self._arr%3A%20list%5Bint%5D%20%3D%20%5B0%5D%20*%20self._capacity%0A%20%20%20%20%20%20%20%20self._size%3A%20int%20%3D%200%0A%20%20%20%20%20%20%20%20self._extend_ratio%3A%20int%20%3D%202%0A%0A%20%20%20%20def%20size%28self%29%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E8%8E%B7%E5%8F%96%E5%88%97%E8%A1%A8%E9%95%BF%E5%BA%A6%EF%BC%88%E5%BD%93%E5%89%8D%E5%85%83%E7%B4%A0%E6%95%B0%E9%87%8F%EF%BC%89%22%22%22%0A%20%20%20%20%20%20%20%20return%20self._size%0A%0A%20%20%20%20def%20capacity%28self%29%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E8%8E%B7%E5%8F%96%E5%88%97%E8%A1%A8%E5%AE%B9%E9%87%8F%22%22%22%0A%20%20%20%20%20%20%20%20return%20self._capacity%0A%0A%20%20%20%20def%20get%28self,%20index%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%20%20%20%20if%20index%20%3C%200%20or%20index%20%3E%3D%20self._size%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20IndexError%28%22%E7%B4%A2%E5%BC%95%E8%B6%8A%E7%95%8C%22%29%0A%20%20%20%20%20%20%20%20return%20self._arr%5Bindex%5D%0A%0A%20%20%20%20def%20set%28self,%20num%3A%20int,%20index%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E6%9B%B4%E6%96%B0%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%20%20%20%20if%20index%20%3C%200%20or%20index%20%3E%3D%20self._size%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20IndexError%28%22%E7%B4%A2%E5%BC%95%E8%B6%8A%E7%95%8C%22%29%0A%20%20%20%20%20%20%20%20self._arr%5Bindex%5D%20%3D%20num%0A%0A%20%20%20%20def%20add%28self,%20num%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E5%9C%A8%E5%B0%BE%E9%83%A8%E6%B7%BB%E5%8A%A0%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%20%20%20%20if%20self.size%28%29%20%3D%3D%20self.capacity%28%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.extend_capacity%28%29%0A%20%20%20%20%20%20%20%20self._arr%5Bself._size%5D%20%3D%20num%0A%20%20%20%20%20%20%20%20self._size%20%2B%3D%201%0A%0A%20%20%20%20def%20insert%28self,%20num%3A%20int,%20index%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E5%9C%A8%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%20%20%20%20if%20index%20%3C%200%20or%20index%20%3E%3D%20self._size%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20IndexError%28%22%E7%B4%A2%E5%BC%95%E8%B6%8A%E7%95%8C%22%29%0A%20%20%20%20%20%20%20%20%20%20%20%20self.extend_capacity%28%29%0A%20%20%20%20%20%20%20%20%23%20%E5%B0%86%E7%B4%A2%E5%BC%95%20index%20%E4%BB%A5%E5%8F%8A%E4%B9%8B%E5%90%8E%E7%9A%84%E5%85%83%E7%B4%A0%E9%83%BD%E5%90%91%E5%90%8E%E7%A7%BB%E5%8A%A8%E4%B8%80%E4%BD%8D%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28self._size%20-%201,%20index%20-%201,%20-1%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._arr%5Bj%20%2B%201%5D%20%3D%20self._arr%5Bj%5D%0A%20%20%20%20%20%20%20%20self._arr%5Bindex%5D%20%3D%20num%0A%20%20%20%20%20%20%20%20self._size%20%2B%3D%201%0A%0A%20%20%20%20def%20remove%28self,%20index%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%20%20%20%20if%20index%20%3C%200%20or%20index%20%3E%3D%20self._size%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20IndexError%28%22%E7%B4%A2%E5%BC%95%E8%B6%8A%E7%95%8C%22%29%0A%20%20%20%20%20%20%20%20num%20%3D%20self._arr%5Bindex%5D%0A%20%20%20%20%20%20%20%20%23%20%E7%B4%A2%E5%BC%95%20i%20%E4%B9%8B%E5%90%8E%E7%9A%84%E5%85%83%E7%B4%A0%E9%83%BD%E5%90%91%E5%89%8D%E7%A7%BB%E5%8A%A8%E4%B8%80%E4%BD%8D%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28index,%20self._size%20-%201%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._arr%5Bj%5D%20%3D%20self._arr%5Bj%20%2B%201%5D%0A%20%20%20%20%20%20%20%20self._size%20-%3D%201%0A%20%20%20%20%20%20%20%20return%20num%0A%0A%20%20%20%20de
|
|
|
|
|
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=class%20MyList%3A%0A%20%20%20%20%22%22%22%E5%88%97%E8%A1%A8%E7%B1%BB%22%22%22%0A%0A%20%20%20%20def%20__init__%28self%29%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95%22%22%22%0A%20%20%20%20%20%20%20%20self._capacity%3A%20int%20%3D%2010%0A%20%20%20%20%20%20%20%20self._arr%3A%20list%5Bint%5D%20%3D%20%5B0%5D%20*%20self._capacity%0A%20%20%20%20%20%20%20%20self._size%3A%20int%20%3D%200%0A%20%20%20%20%20%20%20%20self._extend_ratio%3A%20int%20%3D%202%0A%0A%20%20%20%20def%20size%28self%29%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E8%8E%B7%E5%8F%96%E5%88%97%E8%A1%A8%E9%95%BF%E5%BA%A6%EF%BC%88%E5%BD%93%E5%89%8D%E5%85%83%E7%B4%A0%E6%95%B0%E9%87%8F%EF%BC%89%22%22%22%0A%20%20%20%20%20%20%20%20return%20self._size%0A%0A%20%20%20%20def%20capacity%28self%29%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E8%8E%B7%E5%8F%96%E5%88%97%E8%A1%A8%E5%AE%B9%E9%87%8F%22%22%22%0A%20%20%20%20%20%20%20%20return%20self._capacity%0A%0A%20%20%20%20def%20get%28self,%20index%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%20%20%20%20if%20index%20%3C%200%20or%20index%20%3E%3D%20self._size%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20IndexError%28%22%E7%B4%A2%E5%BC%95%E8%B6%8A%E7%95%8C%22%29%0A%20%20%20%20%20%20%20%20return%20self._arr%5Bindex%5D%0A%0A%20%20%20%20def%20set%28self,%20num%3A%20int,%20index%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E6%9B%B4%E6%96%B0%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%20%20%20%20if%20index%20%3C%200%20or%20index%20%3E%3D%20self._size%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20IndexError%28%22%E7%B4%A2%E5%BC%95%E8%B6%8A%E7%95%8C%22%29%0A%20%20%20%20%20%20%20%20self._arr%5Bindex%5D%20%3D%20num%0A%0A%20%20%20%20def%20add%28self,%20num%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E5%9C%A8%E5%B0%BE%E9%83%A8%E6%B7%BB%E5%8A%A0%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%20%20%20%20if%20self.size%28%29%20%3D%3D%20self.capacity%28%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.extend_capacity%28%29%0A%20%20%20%20%20%20%20%20self._arr%5Bself._size%5D%20%3D%20num%0A%20%20%20%20%20%20%20%20self._size%20%2B%3D%201%0A%0A%20%20%20%20def%20insert%28self,%20num%3A%20int,%20index%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E5%9C%A8%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%20%20%20%20if%20index%20%3C%200%20or%20index%20%3E%3D%20self._size%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20IndexError%28%22%E7%B4%A2%E5%BC%95%E8%B6%8A%E7%95%8C%22%29%0A%20%20%20%20%20%20%20%20%20%20%20%20self.extend_capacity%28%29%0A%20%20%20%20%20%20%20%20%23%20%E5%B0%86%E7%B4%A2%E5%BC%95%20index%20%E4%BB%A5%E5%8F%8A%E4%B9%8B%E5%90%8E%E7%9A%84%E5%85%83%E7%B4%A0%E9%83%BD%E5%90%91%E5%90%8E%E7%A7%BB%E5%8A%A8%E4%B8%80%E4%BD%8D%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28self._size%20-%201,%20index%20-%201,%20-1%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._arr%5Bj%20%2B%201%5D%20%3D%20self._arr%5Bj%5D%0A%20%20%20%20%20%20%20%20self._arr%5Bindex%5D%20%3D%20num%0A%20%20%20%20%20%20%20%20self._size%20%2B%3D%201%0A%0A%20%20%20%20def%20remove%28self,%20index%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20%22%22%22%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%20%20%20%20if%20index%20%3C%200%20or%20index%20%3E%3D%20self._size%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20IndexError%28%22%E7%B4%A2%E5%BC%95%E8%B6%8A%E7%95%8C%22%29%0A%20%20%20%20%20%20%20%20num%20%3D%20self._arr%5Bindex%5D%0A%20%20%20%20%20%20%20%20%23%20%E7%B4%A2%E5%BC%95%20i%20%E4%B9%8B%E5%90%8E%E7%9A%84%E5%85%83%E7%B4%A0%E9%83%BD%E5%90%91%E5%89%8D%E7%A7%BB%E5%8A%A8%E4%B8%80%E4%BD%8D%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28index,%20self._size%20-%201%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._arr%5Bj%5D%20%3D%20self._arr%5Bj%20%2B%201%5D%0A%20%20%20%20%20%20%20%20self._size%20-%3D%201%0A%20%20%20%20%20%20%20%20return%20num%0A%0A%20%20%20%20def%20extend_capacity%28self%29%3A%0A%20%20%
|