hello-algo/en/docs/chapter_array_and_linkedlist/list.md

1162 lines
28 KiB
Markdown
Raw Normal View History

2023-12-27 00:48:00 +08:00
---
comments: true
---
# 4.3   List
2024-05-02 01:46:14 +08:00
A <u>list</u> is an abstract data structure concept that represents an ordered collection of elements, supporting operations such as element access, modification, addition, deletion, and traversal, without requiring users to consider capacity limitations. Lists can be implemented based on linked lists or arrays.
2023-12-27 00:48:00 +08:00
2024-01-25 21:09:05 +08:00
- A linked list inherently serves as a list, supporting operations for adding, deleting, searching, and modifying elements, with the flexibility to dynamically adjust its size.
- Arrays also support these operations, but due to their immutable length, they can be considered as a list with a length limit.
2023-12-27 00:48:00 +08:00
2024-01-25 21:09:05 +08:00
When implementing lists using arrays, **the immutability of length reduces the practicality of the list**. This is because predicting the amount of data to be stored in advance is often challenging, making it difficult to choose an appropriate list length. If the length is too small, it may not meet the requirements; if too large, it may waste memory space.
2023-12-27 00:48:00 +08:00
2024-05-02 01:46:14 +08:00
To solve this problem, we can implement lists using a <u>dynamic array</u>. It inherits the advantages of arrays and can dynamically expand during program execution.
2023-12-27 00:48:00 +08:00
In fact, **many programming languages' standard libraries implement lists using dynamic arrays**, such as Python's `list`, Java's `ArrayList`, C++'s `vector`, and C#'s `List`. In the following discussion, we will consider "list" and "dynamic array" as synonymous concepts.
2024-04-06 03:02:20 +08:00
## 4.3.1 &nbsp; Common list operations
2023-12-27 00:48:00 +08:00
2024-04-06 03:02:20 +08:00
### 1. &nbsp; Initializing a list
2023-12-27 00:48:00 +08:00
2024-01-25 21:09:05 +08:00
We typically use two initialization methods: "without initial values" and "with initial values".
2023-12-27 00:48:00 +08:00
=== "Python"
```python title="list.py"
# Initialize list
# Without initial values
nums1: list[int] = []
# With initial values
nums: list[int] = [1, 3, 2, 5, 4]
```
=== "C++"
```cpp title="list.cpp"
/* Initialize list */
// Note, in C++ the vector is the equivalent of nums described here
// Without initial values
vector<int> nums1;
// With initial values
vector<int> nums = { 1, 3, 2, 5, 4 };
```
=== "Java"
```java title="list.java"
/* Initialize list */
// Without initial values
List<Integer> nums1 = new ArrayList<>();
// With initial values (note the element type should be the wrapper class Integer[] for int[])
Integer[] numbers = new Integer[] { 1, 3, 2, 5, 4 };
List<Integer> nums = new ArrayList<>(Arrays.asList(numbers));
```
=== "C#"
```csharp title="list.cs"
/* Initialize list */
// Without initial values
List<int> nums1 = [];
// With initial values
int[] numbers = [1, 3, 2, 5, 4];
List<int> nums = [.. numbers];
```
=== "Go"
```go title="list_test.go"
/* Initialize list */
// Without initial values
nums1 := []int{}
// With initial values
nums := []int{1, 3, 2, 5, 4}
```
=== "Swift"
```swift title="list.swift"
/* Initialize list */
// Without initial values
let nums1: [Int] = []
// With initial values
var nums = [1, 3, 2, 5, 4]
```
=== "JS"
```javascript title="list.js"
/* Initialize list */
// Without initial values
const nums1 = [];
// With initial values
const nums = [1, 3, 2, 5, 4];
```
=== "TS"
```typescript title="list.ts"
/* Initialize list */
// Without initial values
const nums1: number[] = [];
// With initial values
const nums: number[] = [1, 3, 2, 5, 4];
```
=== "Dart"
```dart title="list.dart"
/* Initialize list */
// Without initial values
List<int> nums1 = [];
// With initial values
List<int> nums = [1, 3, 2, 5, 4];
```
=== "Rust"
```rust title="list.rs"
/* Initialize list */
// Without initial values
let nums1: Vec<i32> = Vec::new();
// With initial values
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
```
=== "C"
```c title="list.c"
// C does not provide built-in dynamic arrays
```
2024-03-25 22:43:12 +08:00
=== "Kotlin"
```kotlin title="list.kt"
```
2023-12-27 00:48:00 +08:00
=== "Zig"
```zig title="list.zig"
// Initialize list
var nums = std.ArrayList(i32).init(std.heap.page_allocator);
defer nums.deinit();
try nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });
```
2024-04-06 03:02:20 +08:00
### 2. &nbsp; Accessing elements
2023-12-27 00:48:00 +08:00
2024-01-25 21:09:05 +08:00
Lists are essentially arrays, thus they can access and update elements in $O(1)$ time, which is very efficient.
2023-12-27 00:48:00 +08:00
=== "Python"
```python title="list.py"
# Access elements
num: int = nums[1] # Access the element at index 1
# Update elements
nums[1] = 0 # Update the element at index 1 to 0
```
=== "C++"
```cpp title="list.cpp"
/* Access elements */
int num = nums[1]; // Access the element at index 1
/* Update elements */
nums[1] = 0; // Update the element at index 1 to 0
```
=== "Java"
```java title="list.java"
/* Access elements */
int num = nums.get(1); // Access the element at index 1
/* Update elements */
nums.set(1, 0); // Update the element at index 1 to 0
```
=== "C#"
```csharp title="list.cs"
/* Access elements */
int num = nums[1]; // Access the element at index 1
/* Update elements */
nums[1] = 0; // Update the element at index 1 to 0
```
=== "Go"
```go title="list_test.go"
/* Access elements */
num := nums[1] // Access the element at index 1
/* Update elements */
nums[1] = 0 // Update the element at index 1 to 0
```
=== "Swift"
```swift title="list.swift"
/* Access elements */
let num = nums[1] // Access the element at index 1
/* Update elements */
nums[1] = 0 // Update the element at index 1 to 0
```
=== "JS"
```javascript title="list.js"
/* Access elements */
const num = nums[1]; // Access the element at index 1
/* Update elements */
nums[1] = 0; // Update the element at index 1 to 0
```
=== "TS"
```typescript title="list.ts"
/* Access elements */
const num: number = nums[1]; // Access the element at index 1
/* Update elements */
nums[1] = 0; // Update the element at index 1 to 0
```
=== "Dart"
```dart title="list.dart"
/* Access elements */
int num = nums[1]; // Access the element at index 1
/* Update elements */
nums[1] = 0; // Update the element at index 1 to 0
```
=== "Rust"
```rust title="list.rs"
/* Access elements */
let num: i32 = nums[1]; // Access the element at index 1
/* Update elements */
nums[1] = 0; // Update the element at index 1 to 0
```
=== "C"
```c title="list.c"
// C does not provide built-in dynamic arrays
```
2024-03-25 22:43:12 +08:00
=== "Kotlin"
```kotlin title="list.kt"
```
2023-12-27 00:48:00 +08:00
=== "Zig"
```zig title="list.zig"
// Access elements
var num = nums.items[1]; // Access the element at index 1
// Update elements
nums.items[1] = 0; // Update the element at index 1 to 0
```
2024-04-06 03:02:20 +08:00
### 3. &nbsp; Inserting and removing elements
2023-12-27 00:48:00 +08:00
2024-01-25 21:09:05 +08:00
Compared to arrays, lists offer more flexibility in adding and removing elements. While adding elements to the end of a list is an $O(1)$ operation, the efficiency of inserting and removing elements elsewhere in the list remains the same as in arrays, with a time complexity of $O(n)$.
2023-12-27 00:48:00 +08:00
=== "Python"
```python title="list.py"
# Clear list
nums.clear()
# Append elements at the end
nums.append(1)
nums.append(3)
nums.append(2)
nums.append(5)
nums.append(4)
# Insert element in the middle
nums.insert(3, 6) # Insert number 6 at index 3
# Remove elements
nums.pop(3) # Remove the element at index 3
```
=== "C++"
```cpp title="list.cpp"
/* Clear list */
nums.clear();
/* Append elements at the end */
nums.push_back(1);
nums.push_back(3);
nums.push_back(2);
nums.push_back(5);
nums.push_back(4);
/* Insert element in the middle */
nums.insert(nums.begin() + 3, 6); // Insert number 6 at index 3
/* Remove elements */
nums.erase(nums.begin() + 3); // Remove the element at index 3
```
=== "Java"
```java title="list.java"
/* Clear list */
nums.clear();
/* Append elements at the end */
nums.add(1);
nums.add(3);
nums.add(2);
nums.add(5);
nums.add(4);
/* Insert element in the middle */
nums.add(3, 6); // Insert number 6 at index 3
/* Remove elements */
nums.remove(3); // Remove the element at index 3
```
=== "C#"
```csharp title="list.cs"
/* Clear list */
nums.Clear();
/* Append elements at the end */
nums.Add(1);
nums.Add(3);
nums.Add(2);
nums.Add(5);
nums.Add(4);
/* Insert element in the middle */
nums.Insert(3, 6);
/* Remove elements */
nums.RemoveAt(3);
```
=== "Go"
```go title="list_test.go"
/* Clear list */
nums = nil
/* Append elements at the end */
nums = append(nums, 1)
nums = append(nums, 3)
nums = append(nums, 2)
nums = append(nums, 5)
nums = append(nums, 4)
/* Insert element in the middle */
nums = append(nums[:3], append([]int{6}, nums[3:]...)...) // Insert number 6 at index 3
/* Remove elements */
nums = append(nums[:3], nums[4:]...) // Remove the element at index 3
```
=== "Swift"
```swift title="list.swift"
/* Clear list */
nums.removeAll()
/* Append elements at the end */
nums.append(1)
nums.append(3)
nums.append(2)
nums.append(5)
nums.append(4)
/* Insert element in the middle */
nums.insert(6, at: 3) // Insert number 6 at index 3
/* Remove elements */
nums.remove(at: 3) // Remove the element at index 3
```
=== "JS"
```javascript title="list.js"
/* Clear list */
nums.length = 0;
/* Append elements at the end */
nums.push(1);
nums.push(3);
nums.push(2);
nums.push(5);
nums.push(4);
/* Insert element in the middle */
nums.splice(3, 0, 6);
/* Remove elements */
nums.splice(3, 1);
```
=== "TS"
```typescript title="list.ts"
/* Clear list */
nums.length = 0;
/* Append elements at the end */
nums.push(1);
nums.push(3);
nums.push(2);
nums.push(5);
nums.push(4);
/* Insert element in the middle */
nums.splice(3, 0, 6);
/* Remove elements */
nums.splice(3, 1);
```
=== "Dart"
```dart title="list.dart"
/* Clear list */
nums.clear();
/* Append elements at the end */
nums.add(1);
nums.add(3);
nums.add(2);
nums.add(5);
nums.add(4);
/* Insert element in the middle */
nums.insert(3, 6); // Insert number 6 at index 3
/* Remove elements */
nums.removeAt(3); // Remove the element at index 3
```
=== "Rust"
```rust title="list.rs"
/* Clear list */
nums.clear();
/* Append elements at the end */
nums.push(1);
nums.push(3);
nums.push(2);
nums.push(5);
nums.push(4);
/* Insert element in the middle */
nums.insert(3, 6); // Insert number 6 at index 3
/* Remove elements */
nums.remove(3); // Remove the element at index 3
```
=== "C"
```c title="list.c"
// C does not provide built-in dynamic arrays
```
2024-03-25 22:43:12 +08:00
=== "Kotlin"
```kotlin title="list.kt"
```
2023-12-27 00:48:00 +08:00
=== "Zig"
```zig title="list.zig"
// Clear list
nums.clearRetainingCapacity();
// Append elements at the end
try nums.append(1);
try nums.append(3);
try nums.append(2);
try nums.append(5);
try nums.append(4);
// Insert element in the middle
try nums.insert(3, 6); // Insert number 6 at index 3
// Remove elements
_ = nums.orderedRemove(3); // Remove the element at index 3
```
2024-04-06 03:02:20 +08:00
### 4. &nbsp; Iterating the list
2023-12-27 00:48:00 +08:00
2024-01-25 21:09:05 +08:00
Similar to arrays, lists can be iterated either by using indices or by directly iterating through each element.
2023-12-27 00:48:00 +08:00
=== "Python"
```python title="list.py"
# Iterate through the list by index
count = 0
for i in range(len(nums)):
count += nums[i]
# Iterate directly through list elements
for num in nums:
count += num
```
=== "C++"
```cpp title="list.cpp"
/* Iterate through the list by index */
int count = 0;
for (int i = 0; i < nums.size(); i++) {
count += nums[i];
}
/* Iterate directly through list elements */
count = 0;
for (int num : nums) {
count += num;
}
```
=== "Java"
```java title="list.java"
/* Iterate through the list by index */
int count = 0;
for (int i = 0; i < nums.size(); i++) {
count += nums.get(i);
}
/* Iterate directly through list elements */
for (int num : nums) {
count += num;
}
```
=== "C#"
```csharp title="list.cs"
/* Iterate through the list by index */
int count = 0;
for (int i = 0; i < nums.Count; i++) {
count += nums[i];
}
/* Iterate directly through list elements */
count = 0;
foreach (int num in nums) {
count += num;
}
```
=== "Go"
```go title="list_test.go"
/* Iterate through the list by index */
count := 0
for i := 0; i < len(nums); i++ {
count += nums[i]
}
/* Iterate directly through list elements */
count = 0
for _, num := range nums {
count += num
}
```
=== "Swift"
```swift title="list.swift"
/* Iterate through the list by index */
var count = 0
for i in nums.indices {
count += nums[i]
}
/* Iterate directly through list elements */
count = 0
for num in nums {
count += num
}
```
=== "JS"
```javascript title="list.js"
/* Iterate through the list by index */
let count = 0;
for (let i = 0; i < nums.length; i++) {
count += nums[i];
}
/* Iterate directly through list elements */
count = 0;
for (const num of nums) {
count += num;
}
```
=== "TS"
```typescript title="list.ts"
/* Iterate through the list by index */
let count = 0;
for (let i = 0; i < nums.length; i++) {
count += nums[i];
}
/* Iterate directly through list elements */
count = 0;
for (const num of nums) {
count += num;
}
```
=== "Dart"
```dart title="list.dart"
/* Iterate through the list by index */
int count = 0;
for (var i = 0; i < nums.length; i++) {
count += nums[i];
}
/* Iterate directly through list elements */
count = 0;
for (var num in nums) {
count += num;
}
```
=== "Rust"
```rust title="list.rs"
// Iterate through the list by index
let mut _count = 0;
for i in 0..nums.len() {
_count += nums[i];
}
// Iterate directly through list elements
_count = 0;
for num in &nums {
_count += num;
}
```
=== "C"
```c title="list.c"
// C does not provide built-in dynamic arrays
```
2024-03-25 22:43:12 +08:00
=== "Kotlin"
```kotlin title="list.kt"
```
2023-12-27 00:48:00 +08:00
=== "Zig"
```zig title="list.zig"
// Iterate through the list by index
var count: i32 = 0;
var i: i32 = 0;
while (i < nums.items.len) : (i += 1) {
count += nums[i];
}
// Iterate directly through list elements
count = 0;
for (nums.items) |num| {
count += num;
}
```
2024-04-06 03:02:20 +08:00
### 5. &nbsp; Concatenating lists
2023-12-27 00:48:00 +08:00
Given a new list `nums1`, we can append it to the end of the original list.
=== "Python"
```python title="list.py"
# Concatenate two lists
nums1: list[int] = [6, 8, 7, 10, 9]
nums += nums1 # Concatenate nums1 to the end of nums
```
=== "C++"
```cpp title="list.cpp"
/* Concatenate two lists */
vector<int> nums1 = { 6, 8, 7, 10, 9 };
// Concatenate nums1 to the end of nums
nums.insert(nums.end(), nums1.begin(), nums1.end());
```
=== "Java"
```java title="list.java"
/* Concatenate two lists */
List<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));
nums.addAll(nums1); // Concatenate nums1 to the end of nums
```
=== "C#"
```csharp title="list.cs"
/* Concatenate two lists */
List<int> nums1 = [6, 8, 7, 10, 9];
nums.AddRange(nums1); // Concatenate nums1 to the end of nums
```
=== "Go"
```go title="list_test.go"
/* Concatenate two lists */
nums1 := []int{6, 8, 7, 10, 9}
nums = append(nums, nums1...) // Concatenate nums1 to the end of nums
```
=== "Swift"
```swift title="list.swift"
/* Concatenate two lists */
let nums1 = [6, 8, 7, 10, 9]
nums.append(contentsOf: nums1) // Concatenate nums1 to the end of nums
```
=== "JS"
```javascript title="list.js"
/* Concatenate two lists */
const nums1 = [6, 8, 7, 10, 9];
nums.push(...nums1); // Concatenate nums1 to the end of nums
```
=== "TS"
```typescript title="list.ts"
/* Concatenate two lists */
const nums1: number[] = [6, 8, 7, 10, 9];
nums.push(...nums1); // Concatenate nums1 to the end of nums
```
=== "Dart"
```dart title="list.dart"
/* Concatenate two lists */
List<int> nums1 = [6, 8, 7, 10, 9];
nums.addAll(nums1); // Concatenate nums1 to the end of nums
```
=== "Rust"
```rust title="list.rs"
/* Concatenate two lists */
let nums1: Vec<i32> = vec![6, 8, 7, 10, 9];
nums.extend(nums1);
```
=== "C"
```c title="list.c"
// C does not provide built-in dynamic arrays
```
2024-03-25 22:43:12 +08:00
=== "Kotlin"
```kotlin title="list.kt"
```
2023-12-27 00:48:00 +08:00
=== "Zig"
```zig title="list.zig"
// Concatenate two lists
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); // Concatenate nums1 to the end of nums
```
2024-04-06 03:02:20 +08:00
### 6. &nbsp; Sorting the list
2023-12-27 00:48:00 +08:00
2024-01-25 21:09:05 +08:00
Once the list is sorted, we can employ algorithms commonly used in array-related algorithm problems, such as "binary search" and "two-pointer" algorithms.
2023-12-27 00:48:00 +08:00
=== "Python"
```python title="list.py"
# Sort the list
nums.sort() # After sorting, the list elements are in ascending order
```
=== "C++"
```cpp title="list.cpp"
/* Sort the list */
sort(nums.begin(), nums.end()); // After sorting, the list elements are in ascending order
```
=== "Java"
```java title="list.java"
/* Sort the list */
Collections.sort(nums); // After sorting, the list elements are in ascending order
```
=== "C#"
```csharp title="list.cs"
/* Sort the list */
nums.Sort(); // After sorting, the list elements are in ascending order
```
=== "Go"
```go title="list_test.go"
/* Sort the list */
sort.Ints(nums) // After sorting, the list elements are in ascending order
```
=== "Swift"
```swift title="list.swift"
/* Sort the list */
nums.sort() // After sorting, the list elements are in ascending order
```
=== "JS"
```javascript title="list.js"
/* Sort the list */
nums.sort((a, b) => a - b); // After sorting, the list elements are in ascending order
```
=== "TS"
```typescript title="list.ts"
/* Sort the list */
nums.sort((a, b) => a - b); // After sorting, the list elements are in ascending order
```
=== "Dart"
```dart title="list.dart"
/* Sort the list */
nums.sort(); // After sorting, the list elements are in ascending order
```
=== "Rust"
```rust title="list.rs"
/* Sort the list */
nums.sort(); // After sorting, the list elements are in ascending order
```
=== "C"
```c title="list.c"
// C does not provide built-in dynamic arrays
```
2024-03-25 22:43:12 +08:00
=== "Kotlin"
```kotlin title="list.kt"
```
2023-12-27 00:48:00 +08:00
=== "Zig"
```zig title="list.zig"
// Sort the list
std.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));
```
2024-04-06 03:02:20 +08:00
## 4.3.2 &nbsp; List implementation
2023-12-27 00:48:00 +08:00
2024-01-25 21:09:05 +08:00
Many programming languages come with built-in lists, including Java, C++, Python, etc. Their implementations tend to be intricate, featuring carefully considered settings for various parameters, like initial capacity and expansion factors. Readers who are curious can delve into the source code for further learning.
2023-12-27 00:48:00 +08:00
2024-01-25 21:09:05 +08:00
To enhance our understanding of how lists work, we will attempt to implement a simplified version of a list, focusing on three crucial design aspects:
2023-12-27 00:48:00 +08:00
2024-04-06 03:02:20 +08:00
- **Initial capacity**: Choose a reasonable initial capacity for the array. In this example, we choose 10 as the initial capacity.
- **Size recording**: Declare a variable `size` to record the current number of elements in the list, updating in real-time with element insertion and deletion. With this variable, we can locate the end of the list and determine whether expansion is needed.
- **Expansion mechanism**: If the list reaches full capacity upon an element insertion, an expansion process is required. This involves creating a larger array based on the expansion factor, and then transferring all elements from the current array to the new one. In this example, we stipulate that the array size should double with each expansion.
2023-12-27 00:48:00 +08:00
=== "Python"
```python title="my_list.py"
class MyList:
2024-05-06 05:27:10 +08:00
"""List class"""
2023-12-27 00:48:00 +08:00
def __init__(self):
2024-05-06 05:27:10 +08:00
"""Constructor"""
self._capacity: int = 10 # List capacity
self._arr: list[int] = [0] * self._capacity # Array (stores list elements)
self._size: int = 0 # List length (current number of elements)
self._extend_ratio: int = 2 # Multiple for each list expansion
2023-12-27 00:48:00 +08:00
def size(self) -> int:
2024-05-06 05:27:10 +08:00
"""Get list length (current number of elements)"""
2023-12-27 00:48:00 +08:00
return self._size
def capacity(self) -> int:
2024-05-06 05:27:10 +08:00
"""Get list capacity"""
2023-12-27 00:48:00 +08:00
return self._capacity
def get(self, index: int) -> int:
2024-05-06 05:27:10 +08:00
"""Access element"""
# If the index is out of bounds, throw an exception, as below
2023-12-27 00:48:00 +08:00
if index < 0 or index >= self._size:
2024-05-06 05:27:10 +08:00
raise IndexError("Index out of bounds")
2023-12-27 00:48:00 +08:00
return self._arr[index]
def set(self, num: int, index: int):
2024-05-06 05:27:10 +08:00
"""Update element"""
2023-12-27 00:48:00 +08:00
if index < 0 or index >= self._size:
2024-05-06 05:27:10 +08:00
raise IndexError("Index out of bounds")
2023-12-27 00:48:00 +08:00
self._arr[index] = num
def add(self, num: int):
2024-05-06 05:27:10 +08:00
"""Add element at the end"""
# When the number of elements exceeds capacity, trigger the expansion mechanism
2023-12-27 00:48:00 +08:00
if self.size() == self.capacity():
self.extend_capacity()
self._arr[self._size] = num
self._size += 1
def insert(self, num: int, index: int):
2024-05-06 05:27:10 +08:00
"""Insert element in the middle"""
2023-12-27 00:48:00 +08:00
if index < 0 or index >= self._size:
2024-05-06 05:27:10 +08:00
raise IndexError("Index out of bounds")
# When the number of elements exceeds capacity, trigger the expansion mechanism
2023-12-27 00:48:00 +08:00
if self._size == self.capacity():
self.extend_capacity()
2024-05-06 05:27:10 +08:00
# Move all elements after `index` one position backward
2023-12-27 00:48:00 +08:00
for j in range(self._size - 1, index - 1, -1):
self._arr[j + 1] = self._arr[j]
self._arr[index] = num
2024-05-06 05:27:10 +08:00
# Update the number of elements
2023-12-27 00:48:00 +08:00
self._size += 1
def remove(self, index: int) -> int:
2024-05-06 05:27:10 +08:00
"""Remove element"""
2023-12-27 00:48:00 +08:00
if index < 0 or index >= self._size:
2024-05-06 05:27:10 +08:00
raise IndexError("Index out of bounds")
2023-12-27 00:48:00 +08:00
num = self._arr[index]
2024-05-06 05:27:10 +08:00
# Move all elements after `index` one position forward
2023-12-27 00:48:00 +08:00
for j in range(index, self._size - 1):
self._arr[j] = self._arr[j + 1]
2024-05-06 05:27:10 +08:00
# Update the number of elements
2023-12-27 00:48:00 +08:00
self._size -= 1
2024-05-06 05:27:10 +08:00
# Return the removed element
2023-12-27 00:48:00 +08:00
return num
def extend_capacity(self):
2024-05-06 05:27:10 +08:00
"""Extend list"""
# Create a new array of _extend_ratio times the length of the original array and copy the original array to the new array
2023-12-27 00:48:00 +08:00
self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)
2024-05-06 05:27:10 +08:00
# Update list capacity
2023-12-27 00:48:00 +08:00
self._capacity = len(self._arr)
def to_array(self) -> list[int]:
2024-05-06 05:27:10 +08:00
"""Return a list of valid lengths"""
2023-12-27 00:48:00 +08:00
return self._arr[: self._size]
```
=== "C++"
```cpp title="my_list.cpp"
2024-05-06 05:27:10 +08:00
[class]{MyList}-[func]{}
2023-12-27 00:48:00 +08:00
```
=== "Java"
```java title="my_list.java"
2024-05-06 05:27:10 +08:00
/* List class */
2023-12-27 00:48:00 +08:00
class MyList {
2024-05-06 05:27:10 +08:00
private int[] arr; // Array (stores list elements)
private int capacity = 10; // List capacity
private int size = 0; // List length (current number of elements)
private int extendRatio = 2; // Multiple for each list expansion
2023-12-27 00:48:00 +08:00
2024-05-06 05:27:10 +08:00
/* Constructor */
2023-12-27 00:48:00 +08:00
public MyList() {
arr = new int[capacity];
}
2024-05-06 05:27:10 +08:00
/* Get list length (current number of elements) */
2023-12-27 00:48:00 +08:00
public int size() {
return size;
}
2024-05-06 05:27:10 +08:00
/* Get list capacity */
2023-12-27 00:48:00 +08:00
public int capacity() {
return capacity;
}
2024-05-06 05:27:10 +08:00
/* Access element */
2023-12-27 00:48:00 +08:00
public int get(int index) {
2024-05-06 05:27:10 +08:00
// If the index is out of bounds, throw an exception, as below
2023-12-27 00:48:00 +08:00
if (index < 0 || index >= size)
2024-05-06 05:27:10 +08:00
throw new IndexOutOfBoundsException("Index out of bounds");
2023-12-27 00:48:00 +08:00
return arr[index];
}
2024-05-06 05:27:10 +08:00
/* Update element */
2023-12-27 00:48:00 +08:00
public void set(int index, int num) {
if (index < 0 || index >= size)
2024-05-06 05:27:10 +08:00
throw new IndexOutOfBoundsException("Index out of bounds");
2023-12-27 00:48:00 +08:00
arr[index] = num;
}
2024-05-06 05:27:10 +08:00
/* Add element at the end */
2023-12-27 00:48:00 +08:00
public void add(int num) {
2024-05-06 05:27:10 +08:00
// When the number of elements exceeds capacity, trigger the expansion mechanism
2023-12-27 00:48:00 +08:00
if (size == capacity())
extendCapacity();
arr[size] = num;
2024-05-06 05:27:10 +08:00
// Update the number of elements
2023-12-27 00:48:00 +08:00
size++;
}
2024-05-06 05:27:10 +08:00
/* Insert element in the middle */
2023-12-27 00:48:00 +08:00
public void insert(int index, int num) {
if (index < 0 || index >= size)
2024-05-06 05:27:10 +08:00
throw new IndexOutOfBoundsException("Index out of bounds");
// When the number of elements exceeds capacity, trigger the expansion mechanism
2023-12-27 00:48:00 +08:00
if (size == capacity())
extendCapacity();
2024-05-06 05:27:10 +08:00
// Move all elements after `index` one position backward
2023-12-27 00:48:00 +08:00
for (int j = size - 1; j >= index; j--) {
arr[j + 1] = arr[j];
}
arr[index] = num;
2024-05-06 05:27:10 +08:00
// Update the number of elements
2023-12-27 00:48:00 +08:00
size++;
}
2024-05-06 05:27:10 +08:00
/* Remove element */
2023-12-27 00:48:00 +08:00
public int remove(int index) {
if (index < 0 || index >= size)
2024-05-06 05:27:10 +08:00
throw new IndexOutOfBoundsException("Index out of bounds");
2023-12-27 00:48:00 +08:00
int num = arr[index];
2024-05-06 05:27:10 +08:00
// Move all elements after `index` one position forward
2023-12-27 00:48:00 +08:00
for (int j = index; j < size - 1; j++) {
arr[j] = arr[j + 1];
}
2024-05-06 05:27:10 +08:00
// Update the number of elements
2023-12-27 00:48:00 +08:00
size--;
2024-05-06 05:27:10 +08:00
// Return the removed element
2023-12-27 00:48:00 +08:00
return num;
}
2024-05-06 05:27:10 +08:00
/* Extend list */
2023-12-27 00:48:00 +08:00
public void extendCapacity() {
2024-05-06 05:27:10 +08:00
// Create a new array with a length multiple of the original array by extendRatio, and copy the original array to the new array
2023-12-27 00:48:00 +08:00
arr = Arrays.copyOf(arr, capacity() * extendRatio);
2024-05-06 05:27:10 +08:00
// Update list capacity
2023-12-27 00:48:00 +08:00
capacity = arr.length;
}
2024-05-06 05:27:10 +08:00
/* Convert the list to an array */
2023-12-27 00:48:00 +08:00
public int[] toArray() {
int size = size();
2024-05-06 05:27:10 +08:00
// Only convert elements within valid length range
2023-12-27 00:48:00 +08:00
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = get(i);
}
return arr;
}
}
```
=== "C#"
```csharp title="my_list.cs"
2024-05-06 05:27:10 +08:00
[class]{MyList}-[func]{}
2023-12-27 00:48:00 +08:00
```
=== "Go"
```go title="my_list.go"
2024-05-06 05:27:10 +08:00
[class]{myList}-[func]{}
2023-12-27 00:48:00 +08:00
```
=== "Swift"
```swift title="my_list.swift"
2024-05-06 05:27:10 +08:00
[class]{MyList}-[func]{}
2023-12-27 00:48:00 +08:00
```
=== "JS"
```javascript title="my_list.js"
2024-05-06 05:27:10 +08:00
[class]{MyList}-[func]{}
2023-12-27 00:48:00 +08:00
```
=== "TS"
```typescript title="my_list.ts"
2024-05-06 05:27:10 +08:00
[class]{MyList}-[func]{}
2023-12-27 00:48:00 +08:00
```
=== "Dart"
```dart title="my_list.dart"
2024-05-06 05:27:10 +08:00
[class]{MyList}-[func]{}
2023-12-27 00:48:00 +08:00
```
=== "Rust"
```rust title="my_list.rs"
2024-05-06 05:27:10 +08:00
[class]{MyList}-[func]{}
2023-12-27 00:48:00 +08:00
```
=== "C"
```c title="my_list.c"
2024-05-06 05:27:10 +08:00
[class]{MyList}-[func]{}
2023-12-27 00:48:00 +08:00
```
2024-03-25 22:43:12 +08:00
=== "Kotlin"
```kotlin title="my_list.kt"
2024-05-06 05:27:10 +08:00
[class]{MyList}-[func]{}
2024-03-25 22:43:12 +08:00
```
2024-03-31 03:53:04 +08:00
=== "Ruby"
```ruby title="my_list.rb"
2024-05-06 05:27:10 +08:00
[class]{MyList}-[func]{}
2024-03-31 03:53:04 +08:00
```
2023-12-27 00:48:00 +08:00
=== "Zig"
```zig title="my_list.zig"
2024-05-06 05:27:10 +08:00
[class]{MyList}-[func]{}
2023-12-27 00:48:00 +08:00
```