From 11a2cbd3bd5c3292b9f642b21c8150b6908af66d Mon Sep 17 00:00:00 2001 From: flykhan Date: Wed, 21 Feb 2024 10:10:16 +0800 Subject: [PATCH] push --- chapter_array_and_linkedlist/array.cpp | 108 ++++++++++--------- chapter_array_and_linkedlist/linked_list.cpp | 36 +++---- 2 files changed, 78 insertions(+), 66 deletions(-) diff --git a/chapter_array_and_linkedlist/array.cpp b/chapter_array_and_linkedlist/array.cpp index dd87b92..c0780cb 100644 --- a/chapter_array_and_linkedlist/array.cpp +++ b/chapter_array_and_linkedlist/array.cpp @@ -6,106 +6,118 @@ #include "../utils/common.hpp" -/* 随机访问元素 */ -int randomAccess(int *nums, int size) { - // 在区间 [0, size) 中随机抽取一个数字 +/* Ԫ */ +int randomAccess(int *nums, int size) +{ // *nums Ϊ׵ַ + // [0, size) ȡһ int randomIndex = rand() % size; - // 获取并返回随机元素 - int randomNum = nums[randomIndex]; + // ȡԪ + int randomNum = nums[randomIndex]; // nums[randomIndex] ȼ *(nums + randomIndex) return randomNum; } -/* 扩展数组长度 */ -int *extend(int *nums, int size, int enlarge) { - // 初始化一个扩展长度后的数组 - int *res = new int[size + enlarge]; - // 将原数组中的所有元素复制到新数组 - for (int i = 0; i < size; i++) { +/* չ鳤 */ +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]; } - // 释放内存 - delete[] nums; - // 返回扩展后的新数组 + // ͷڴ + delete[] nums; // ͷԭڴռ + // չ return res; } -/* 在数组的索引 index 处插入元素 num */ -void insert(int *nums, int size, int num, int index) { - // 把索引 index 以及之后的所有元素向后移动一位 - for (int i = size - 1; i > index; i--) { +/* 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 处的元素 + // num index Ԫ nums[index] = num; } -/* 删除索引 index 处的元素 */ -void remove(int *nums, int size, int index) { - // 把索引 index 之后的所有元素向前移动一位 - for (int i = index; i < size - 1; i++) { +/* ɾ index Ԫ */ +void remove(int *nums, int size, int index) +{ + // index ֮Ԫǰƶһλ + for (int i = index; i < size - 1; i++) + { nums[i] = nums[i + 1]; } } -/* 遍历数组 */ -void traverse(int *nums, int size) { +/* */ +void traverse(int *nums, int size) +{ int count = 0; - // 通过索引遍历数组 - for (int i = 0; i < size; i++) { + // ͨ + for (int i = 0; i < size; i++) + { count += nums[i]; } } -/* 在数组中查找指定元素 */ -int find(int *nums, int size, int target) { - for (int i = 0; i < size; i++) { +/* вָԪ */ +int find(int *nums, int size, int target) +{ + for (int i = 0; i < size; i++) + { if (nums[i] == target) return i; } return -1; } -/* Driver Code */ -int main() { - /* 初始化数组 */ +/* Driver Code */ // +int main() +{ + /* ʼ */ int size = 5; int *arr = new int[size]; - cout << "数组 arr = "; + cout << " arr = "; // ûʼӡֵ printArray(arr, size); - int *nums = new int[size]{1, 3, 2, 5, 4}; - cout << "数组 nums = "; + int *nums = new int[size]{1, 3, 2, 5, 4}; // []{} һֳʼʽ(ʼб) + cout << " nums = "; printArray(nums, size); - /* 随机访问 */ + /* */ int randomNum = randomAccess(nums, size); - cout << "在 nums 中获取随机元素 " << randomNum << endl; + cout << " nums лȡԪ " << randomNum << endl; - /* 长度扩展 */ + /* չ */ int enlarge = 3; nums = extend(nums, size, enlarge); size += enlarge; - cout << "将数组长度扩展至 8 ,得到 nums = "; + cout << "鳤չ 8 õ nums = "; printArray(nums, size); - /* 插入元素 */ + /* Ԫ */ insert(nums, size, 6, 3); - cout << "在索引 3 处插入数字 6 ,得到 nums = "; + cout << " 3 6 õ nums = "; printArray(nums, size); - /* 删除元素 */ + /* ɾԪ */ remove(nums, size, 2); - cout << "删除索引 2 处的元素,得到 nums = "; + cout << "ɾ 2 Ԫأõ nums = "; printArray(nums, size); - /* 遍历数组 */ + /* */ traverse(nums, size); - /* 查找元素 */ + /* Ԫ */ int index = find(nums, size, 3); - cout << "在 nums 中查找元素 3 ,得到索引 = " << index << endl; + cout << " nums вԪ 3 õ = " << index << endl; - // 释放内存 + // ͷڴ delete[] arr; delete[] nums; diff --git a/chapter_array_and_linkedlist/linked_list.cpp b/chapter_array_and_linkedlist/linked_list.cpp index 092226e..3304ead 100644 --- a/chapter_array_and_linkedlist/linked_list.cpp +++ b/chapter_array_and_linkedlist/linked_list.cpp @@ -6,14 +6,14 @@ #include "../utils/common.hpp" -/* 在链表的节点 n0 之后插入节点 P */ +/* Ľڵ n0 ֮ڵ P */ void insert(ListNode *n0, ListNode *P) { ListNode *n1 = n0->next; P->next = n1; n0->next = P; } -/* 删除链表的节点 n0 之后的首个节点 */ +/* ɾĽڵ n0 ֮׸ڵ */ void remove(ListNode *n0) { if (n0->next == nullptr) return; @@ -21,11 +21,11 @@ void remove(ListNode *n0) { ListNode *P = n0->next; ListNode *n1 = P->next; n0->next = n1; - // 释放内存 + // ͷڴ delete P; } -/* 访问链表中索引为 index 的节点 */ +/* Ϊ index Ľڵ */ ListNode *access(ListNode *head, int index) { for (int i = 0; i < index; i++) { if (head == nullptr) @@ -35,7 +35,7 @@ ListNode *access(ListNode *head, int index) { return head; } -/* 在链表中查找值为 target 的首个节点 */ +/* вֵΪ target ׸ڵ */ int find(ListNode *head, int target) { int index = 0; while (head != nullptr) { @@ -49,40 +49,40 @@ int find(ListNode *head, int target) { /* Driver Code */ int main() { - /* 初始化链表 */ - // 初始化各个节点 + /* ʼ */ + // ʼڵ 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; - cout << "初始化的链表为" << endl; + cout << "ʼΪ" << endl; printLinkedList(n0); - /* 插入节点 */ + /* ڵ */ insert(n0, new ListNode(0)); - cout << "插入节点后的链表为" << endl; + cout << "ڵΪ" << endl; printLinkedList(n0); - /* 删除节点 */ + /* ɾڵ */ remove(n0); - cout << "删除节点后的链表为" << endl; + cout << "ɾڵΪ" << endl; printLinkedList(n0); - /* 访问节点 */ + /* ʽڵ */ ListNode *node = access(n0, 3); - cout << "链表中索引 3 处的节点的值 = " << node->val << endl; + cout << " 3 Ľڵֵ = " << node->val << endl; - /* 查找节点 */ + /* ҽڵ */ int index = find(n0, 2); - cout << "链表中值为 2 的节点的索引 = " << index << endl; + cout << "ֵΪ 2 Ľڵ = " << index << endl; - // 释放内存 + // ͷڴ freeMemoryLinkedList(n0); return 0;