Add C++ codes for the chapter

array and linked list.
This commit is contained in:
Yudong Jin
2022-11-27 19:07:35 +08:00
parent 19a4ccd86a
commit 731e98fc25
11 changed files with 705 additions and 44 deletions

View File

@@ -27,7 +27,9 @@ comments: true
=== "C++"
```cpp title="array.cpp"
/* 初始化数组 */
int* arr = new int[5];
int* nums = new int[5] { 1, 3, 2, 5, 4 };
```
=== "Python"
@@ -70,7 +72,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C++"
```cpp title="array.cpp"
/* 随机返回一个数组元素 */
int randomAccess(int* nums, int size) {
// 在区间 [0, size) 中随机抽取一个数字
int randomIndex = rand() % size;
// 获取并返回随机元素
int randomNum = nums[randomIndex];
return randomNum;
}
```
=== "Python"
@@ -108,7 +117,17 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C++"
```cpp title="array.cpp"
/* 扩展数组长度 */
int* extend(int* nums, int size, int enlarge) {
// 初始化一个扩展长度后的数组
int* res = new int[size + enlarge];
// 将原数组中的所有元素复制到新数组
for (int i = 0; i < size; i++) {
res[i] = nums[i];
}
// 返回扩展后的新数组
return res;
}
```
=== "Python"
@@ -160,7 +179,23 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C++"
```cpp title="array.cpp"
/* 在数组的索引 index 处插入元素 num */
void insert(int* nums, int size, int num, int index) {
// 把索引 index 以及之后的所有元素向后移动一位
for (int i = size - 1; i >= index; i--) {
nums[i] = nums[i - 1];
}
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
void remove(int* nums, int size, int index) {
// 把索引 index 之后的所有元素向前移动一位
for (int i = index; i < size - 1; i++) {
nums[i] = nums[i + 1];
}
}
```
=== "Python"
@@ -205,7 +240,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C++"
```cpp title="array.cpp"
/* 遍历数组 */
void traverse(int* nums, int size) {
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < size; i++) {
count++;
}
}
```
=== "Python"
@@ -240,7 +282,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C++"
```cpp title="array.cpp"
/* 在数组中查找指定元素 */
int find(int* nums, int size, int target) {
for (int i = 0; i < size; i++) {
if (nums[i] == target)
return i;
}
return -1;
}
```
=== "Python"

View File

@@ -30,7 +30,12 @@ comments: true
=== "C++"
```cpp title=""
/* 链表结点结构体 */
struct ListNode {
int val; // 结点值
ListNode *next; // 指向下一结点的指针(引用)
ListNode(int x) : val(x), next(nullptr) {} // 构造函数
};
```
=== "Python"
@@ -71,7 +76,18 @@ comments: true
=== "C++"
```cpp title=""
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
// 初始化各个结点
ListNode* n0 = new ListNode(1);
ListNode* n1 = new ListNode(3);
ListNode* n2 = new ListNode(2);
ListNode* n3 = new ListNode(5);
ListNode* n4 = new ListNode(4);
// 构建引用指向
n0->next = n1;
n1->next = n2;
n2->next = n3;
n3->next = n4;
```
=== "Python"
@@ -123,7 +139,22 @@ comments: true
=== "C++"
```cpp title=""
/* 在链表的结点 n0 之后插入结点 P */
void insert(ListNode* n0, ListNode* P) {
ListNode* n1 = n0->next;
n0->next = P;
P->next = n1;
}
/* 删除链表的结点 n0 之后的首个结点 */
void remove(ListNode* n0) {
if (n0->next == nullptr)
return;
// n0 -> P -> n1
ListNode* P = n0->next;
ListNode* n1 = P->next;
n0->next = n1;
}
```
=== "Python"
@@ -166,7 +197,15 @@ comments: true
=== "C++"
```cpp title=""
/* 访问链表中索引为 index 的结点 */
ListNode* access(ListNode* head, int index) {
for (int i = 0; i < index; i++) {
head = head->next;
if (head == nullptr)
return nullptr;
}
return head;
}
```
=== "Python"
@@ -206,7 +245,17 @@ comments: true
=== "C++"
```cpp title=""
/* 在链表中查找值为 target 的首个结点 */
int find(ListNode* head, int target) {
int index = 0;
while (head != nullptr) {
if (head->val == target)
return index;
head = head->next;
index++;
}
return -1;
}
```
=== "Python"
@@ -246,7 +295,13 @@ comments: true
=== "C++"
```cpp title=""
/* 链表结点结构体 */
struct ListNode {
int val; // 结点值
ListNode *next; // 指向后继结点的指针(引用)
ListNode *prev; // 指向前驱结点的指针(引用)
ListNode(int x) : val(x), next(nullptr) {} // 构造函数
};
```
=== "Python"

View File

@@ -24,7 +24,8 @@ comments: true
=== "C++"
```cpp title="list.cpp"
/* 初始化列表 */
vector<int> list = { 1, 3, 2, 5, 4 };
```
=== "Python"
@@ -49,7 +50,11 @@ comments: true
=== "C++"
```cpp title="list.cpp"
/* 访问元素 */
int num = list[1]; // 访问索引 1 处的元素
/* 更新元素 */
list[1] = 0; // 将索引 1 处的元素更新为 0
```
=== "Python"
@@ -87,7 +92,21 @@ comments: true
=== "C++"
```cpp title="list.cpp"
/* 清空列表 */
list.clear();
/* 尾部添加元素 */
list.push_back(1);
list.push_back(3);
list.push_back(2);
list.push_back(5);
list.push_back(4);
/* 中间插入元素 */
list.insert(list.begin() + 3, 6); // 在索引 3 处插入数字 6
/* 删除元素 */
list.erase(list.begin() + 3); // 删除索引 3 处的元素
```
=== "Python"
@@ -131,7 +150,17 @@ comments: true
=== "C++"
```cpp title="list.cpp"
/* 通过索引遍历列表 */
int count = 0;
for (int i = 0; i < list.size(); i++) {
count++;
}
/* 直接遍历列表元素 */
count = 0;
for (int n : list) {
count++;
}
```
=== "Python"
@@ -161,7 +190,10 @@ comments: true
=== "C++"
```cpp title="list.cpp"
/* 拼接两个列表 */
vector<int> list1 = { 6, 8, 7, 10, 9 };
// 将列表 list1 拼接到 list 之后
list.insert(list.end(), list1.begin(), list1.end());
```
=== "Python"
@@ -184,7 +216,8 @@ comments: true
=== "C++"
```cpp title="list.cpp"
/* 排序列表 */
sort(list.begin(), list.end()); // 排序后,列表元素从小到大排列
```
=== "Python"
@@ -209,14 +242,14 @@ comments: true
```java title="my_list.java"
/* 列表类简易实现 */
class MyList {
int[] nums; // 数组(存储列表元素)
int initialCapacity = 10; // 列表初始容量
int size = 0; // 列表长度(即当前元素数量)
int extendRatio = 2; // 每次列表扩容的倍数
private int[] nums; // 数组(存储列表元素)
private int capacity = 10; // 列表容量
private int size = 0; // 列表长度(即当前元素数量)
private int extendRatio = 2; // 每次列表扩容的倍数
/* 构造函数 */
public MyList() {
nums = new int[initialCapacity];
nums = new int[capacity];
}
/* 获取列表长度(即当前元素数量)*/
@@ -226,7 +259,7 @@ comments: true
/* 获取列表容量 */
public int capacity() {
return nums.length;
return capacity;
}
/* 访问元素 */
@@ -247,7 +280,7 @@ comments: true
/* 尾部添加元素 */
public void add(int num) {
// 元素数量超出容量时,触发扩容机制
if (size == nums.length)
if (size == capacity())
extendCapacity();
nums[size] = num;
// 更新元素数量
@@ -259,7 +292,7 @@ comments: true
if (index >= size)
throw new IndexOutOfBoundsException("索引越界");
// 元素数量超出容量时,触发扩容机制
if (size == nums.length)
if (size == capacity())
extendCapacity();
// 索引 i 以及之后的元素都向后移动一位
for (int j = size - 1; j >= index; j--) {
@@ -285,7 +318,9 @@ comments: true
/* 列表扩容 */
public void extendCapacity() {
// 新建一个长度为 size 的数组,并将原数组拷贝到新数组
nums = Arrays.copyOf(nums, nums.length * extendRatio);
nums = Arrays.copyOf(nums, capacity() * extendRatio);
// 更新列表容量
capacity = nums.length;
}
}
```
@@ -293,7 +328,108 @@ comments: true
=== "C++"
```cpp title="my_list.cpp"
/* 列表类简易实现 */
class MyList {
private:
int* nums; // 数组(存储列表元素)
int numsCapacity = 10; // 列表容量
int numsSize = 0; // 列表长度(即当前元素数量)
int extendRatio = 2; // 每次列表扩容的倍数
public:
/* 构造函数 */
MyList() {
nums = new int[numsCapacity];
}
/* 获取列表长度(即当前元素数量)*/
int size() {
return numsSize;
}
/* 获取列表容量 */
int capacity() {
return numsCapacity;
}
/* 访问元素 */
int get(int index) {
// 索引如果越界则抛出异常,下同
if (index >= size())
throw std::out_of_range ("索引越界");
return nums[index];
}
/* 更新元素 */
void set(int index, int num) {
if (index >= size())
throw std::out_of_range ("索引越界");
nums[index] = num;
}
/* 尾部添加元素 */
void add(int num) {
// 元素数量超出容量时,触发扩容机制
if (size() == capacity())
extendCapacity();
nums[size()] = num;
// 更新元素数量
numsSize++;
}
/* 中间插入元素 */
void insert(int index, int num) {
if (index >= size())
throw std::out_of_range ("索引越界");
// 元素数量超出容量时,触发扩容机制
if (size() == capacity())
extendCapacity();
// 索引 i 以及之后的元素都向后移动一位
for (int j = size() - 1; j >= index; j--) {
nums[j + 1] = nums[j];
}
nums[index] = num;
// 更新元素数量
numsSize++;
}
/* 删除元素 */
void remove(int index) {
if (index >= size())
throw std::out_of_range ("索引越界");
// 索引 i 之后的元素都向前移动一位
for (int j = index; j < size() - 1; j++) {
nums[j] = nums[j + 1];
}
// 更新元素数量
numsSize--;
}
/* 列表扩容 */
void extendCapacity() {
// 新建一个长度为 size * extendRatio 的数组,并将原数组拷贝到新数组
int newCapacity = capacity() * extendRatio;
int* extend = new int[newCapacity];
// 将原数组中的所有元素复制到新数组
for (int i = 0; i < size(); i++) {
extend[i] = nums[i];
}
int* temp = nums;
nums = extend;
delete[] temp;
numsCapacity = newCapacity;
}
/* 将列表转换为 Vector 用于打印 */
vector<int> toVector() {
// 仅转换有效长度范围内的列表元素
vector<int> vec(size());
for (int i = 0; i < size(); i++) {
vec[i] = nums[i];
}
return vec;
}
};
```
=== "Python"
@@ -303,10 +439,10 @@ comments: true
class MyList:
""" 构造函数 """
def __init__(self):
self._initial_capacity = 10 # 列表初始容量
self._nums = [0] * self._initial_capacity # 数组(存储列表元素)
self._size = 0 # 列表长度(即当前元素数量)
self._extend_ratio = 2 # 每次列表扩容的倍数
self._capacity = 10 # 列表容量
self._nums = [0] * self._capacity # 数组(存储列表元素)
self._size = 0 # 列表长度(即当前元素数量)
self._extend_ratio = 2 # 每次列表扩容的倍数
""" 获取列表长度(即当前元素数量) """
def size(self):
@@ -314,7 +450,7 @@ comments: true
""" 获取列表容量 """
def capacity(self):
return len(self._nums)
return self._capacity
""" 访问元素 """
def get(self, index):
@@ -355,4 +491,6 @@ comments: true
def extend_capacity(self):
# 新建一个长度为 self._size 的数组,并将原数组拷贝到新数组
self._nums = self._nums + [0] * self.capacity() * (self._extend_ratio - 1)
# 更新列表容量
self._capacity = len(self._nums)
```