From 5cc58c7b916cc97a61ddd4a8d6ca4d060474f0e1 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Thu, 12 Jan 2023 07:08:56 +0800 Subject: [PATCH 1/7] =?UTF-8?q?fix(docs/cpp):=20fix=20=20error:=20comparis?= =?UTF-8?q?on=20of=20integer=20expressions=20of=20different=20signedness:?= =?UTF-8?q?=20=E2=80=98size=5Ft=E2=80=99=20{aka=20=E2=80=98long=20unsigned?= =?UTF-8?q?=20int=E2=80=99}=20and=20=E2=80=98int=E2=80=99=20[-Werror=3Dsig?= =?UTF-8?q?n-compare]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/cpp/chapter_array_and_linkedlist/list.cpp | 4 ++-- .../worst_best_time_complexity.cpp | 2 +- codes/cpp/chapter_searching/hashing_search.cpp | 2 +- codes/cpp/chapter_searching/linear_search.cpp | 2 +- codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/codes/cpp/chapter_array_and_linkedlist/list.cpp b/codes/cpp/chapter_array_and_linkedlist/list.cpp index 44bbf88c8..aadf83d40 100644 --- a/codes/cpp/chapter_array_and_linkedlist/list.cpp +++ b/codes/cpp/chapter_array_and_linkedlist/list.cpp @@ -48,8 +48,8 @@ int main() { PrintUtil::printVector(list); /* 通过索引遍历列表 */ - int count = 0; - for (int i = 0; i < list.size(); i++) { + size_t count = 0; + for (size_t i = 0; i < list.size(); i++) { count++; } diff --git a/codes/cpp/chapter_computational_complexity/worst_best_time_complexity.cpp b/codes/cpp/chapter_computational_complexity/worst_best_time_complexity.cpp index c2916cb40..c3f35adaf 100644 --- a/codes/cpp/chapter_computational_complexity/worst_best_time_complexity.cpp +++ b/codes/cpp/chapter_computational_complexity/worst_best_time_complexity.cpp @@ -22,7 +22,7 @@ vector randomNumbers(int n) { /* 查找数组 nums 中数字 1 所在索引 */ int findOne(vector& nums) { - for (int i = 0; i < nums.size(); i++) { + for (size_t i = 0; i < nums.size(); i++) { if (nums[i] == 1) return i; } diff --git a/codes/cpp/chapter_searching/hashing_search.cpp b/codes/cpp/chapter_searching/hashing_search.cpp index ebc2fb01c..57c9f5d16 100644 --- a/codes/cpp/chapter_searching/hashing_search.cpp +++ b/codes/cpp/chapter_searching/hashing_search.cpp @@ -33,7 +33,7 @@ int main() { vector nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 }; // 初始化哈希表 unordered_map map; - for (int i = 0; i < nums.size(); i++) { + for (size_t i = 0; i < nums.size(); i++) { map[nums[i]] = i; // key: 元素,value: 索引 } int index = hashingSearch(map, target); diff --git a/codes/cpp/chapter_searching/linear_search.cpp b/codes/cpp/chapter_searching/linear_search.cpp index 7b5092762..45a08ded7 100644 --- a/codes/cpp/chapter_searching/linear_search.cpp +++ b/codes/cpp/chapter_searching/linear_search.cpp @@ -9,7 +9,7 @@ /* 线性查找(数组) */ int linearSearch(vector& nums, int target) { // 遍历数组 - for (int i = 0; i < nums.size(); i++) { + for (size_t i = 0; i < nums.size(); i++) { // 找到目标元素,返回其索引 if (nums[i] == target) return i; diff --git a/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp b/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp index ae22740d6..5694e702e 100644 --- a/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp +++ b/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp @@ -68,7 +68,7 @@ public: vector toVector() { ListNode* node = front; vector res(size()); - for (int i = 0; i < res.size(); i++) { + for (size_t i = 0; i < res.size(); i++) { res[i] = node->val; node = node->next; } From 5271276f4e0e7e8efd5bdd14e761e973d89d33d5 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Thu, 12 Jan 2023 11:02:39 +0800 Subject: [PATCH 2/7] feat(codes/c): add linked_list.c --- .../CMakeLists.txt | 3 +- .../linked_list.c | 89 +++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 codes/c/chapter_array_and_linkedlist/linked_list.c diff --git a/codes/c/chapter_array_and_linkedlist/CMakeLists.txt b/codes/c/chapter_array_and_linkedlist/CMakeLists.txt index 24658b301..dfa3322fb 100644 --- a/codes/c/chapter_array_and_linkedlist/CMakeLists.txt +++ b/codes/c/chapter_array_and_linkedlist/CMakeLists.txt @@ -1 +1,2 @@ -add_executable(array array.c) \ No newline at end of file +add_executable(array array.c) +add_executable(linked_list linked_list.c) \ No newline at end of file diff --git a/codes/c/chapter_array_and_linkedlist/linked_list.c b/codes/c/chapter_array_and_linkedlist/linked_list.c new file mode 100644 index 000000000..ed5764254 --- /dev/null +++ b/codes/c/chapter_array_and_linkedlist/linked_list.c @@ -0,0 +1,89 @@ +/** + * File: linked_list.c + * Created Time: 2022-01-12 + * Author: Zero (glj0@outlook.com) + */ + + #include "../include/include.h" + + /* 在链表的结点 n0 之后插入结点 P */ +void insertNode(ListNode* n0, ListNode* P) { + ListNode *n1; + n1 = n0 -> next; + n0 -> next = P; + P -> next = n1; +} + +/* 删除链表的结点 n0 之后的首个结点 */ +void removeNode(ListNode* n0) { + // n0 -> P -> n1 + ListNode *n1, *P; + P = n0 -> next; + n1 = P -> next; + n0 -> next = n1; + + free(P); +} + +/* 访问链表中索引为 index 的结点 */ +ListNode* accessNode(ListNode* head, int index) { + ListNode* n; + while(head && head->next && index) { + head = head->next; + index --; + } + n = head; +} + +/* 在链表中查找值为 target 的首个结点 */ +int findNode(ListNode* head, int target) { + ListNode* n; + int idx=0; + while(head) { + if(head->val == target) { + return idx; + } + head = head -> next; + idx ++; + } + return -1; +} + + +/* Driver Code */ +int main() { + /* 初始化链表 */ + // 初始化各个结点 + ListNode* n0 = newListNode(1); + ListNode* n1 = newListNode(2); + ListNode* n2 = newListNode(3); + ListNode* n3 = newListNode(4); + ListNode* n4 = newListNode(5); + // 构建引用指向 + n0->next = n1; + n1->next = n2; + n2->next = n3; + n3->next = n4; + printf("初始化的链表为\r\n"); + printLinkedList(n0); + + /* 插入结点 */ + insertNode(n0, newListNode(0)); + printf("插入结点后的链表为\r\n"); + printLinkedList(n0); + + /* 删除结点 */ + removeNode(n0); + printf("删除结点后的链表为\r\n"); + printLinkedList(n0); + + /* 访问结点 */ + ListNode* node = accessNode(n0, 3); + printf("链表中索引 3 处的结点的值 = %d\r\n", node->val); + + /* 查找结点 */ + int index = findNode(n0, 5); + printf("链表中值为 5 的结点的索引 = %d\r\n", index); + + return 0; +} From be2d109c5be6ca48d50f233ac4200858f98a881f Mon Sep 17 00:00:00 2001 From: Gonglja Date: Thu, 12 Jan 2023 15:16:57 +0800 Subject: [PATCH 3/7] style(codes/c): update comment format --- .../linked_list.c | 53 +++++++++---------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/codes/c/chapter_array_and_linkedlist/linked_list.c b/codes/c/chapter_array_and_linkedlist/linked_list.c index ed5764254..5ce7cf4c0 100644 --- a/codes/c/chapter_array_and_linkedlist/linked_list.c +++ b/codes/c/chapter_array_and_linkedlist/linked_list.c @@ -4,47 +4,44 @@ * Author: Zero (glj0@outlook.com) */ - #include "../include/include.h" +#include "../include/include.h" /* 在链表的结点 n0 之后插入结点 P */ void insertNode(ListNode* n0, ListNode* P) { - ListNode *n1; - n1 = n0 -> next; - n0 -> next = P; - P -> next = n1; + ListNode *n1 = n0->next; + n0->next = P; + P->next = n1; } /* 删除链表的结点 n0 之后的首个结点 */ void removeNode(ListNode* n0) { + if (!n0->next) + return; // n0 -> P -> n1 - ListNode *n1, *P; - P = n0 -> next; - n1 = P -> next; - n0 -> next = n1; - + ListNode *P = n0->next; + ListNode *n1 = P->next; + n0->next = n1; + // 释放内存 free(P); } /* 访问链表中索引为 index 的结点 */ ListNode* accessNode(ListNode* head, int index) { - ListNode* n; - while(head && head->next && index) { + while (head && head->next && index) { head = head->next; - index --; + index--; } - n = head; + return head; } /* 在链表中查找值为 target 的首个结点 */ int findNode(ListNode* head, int target) { - ListNode* n; - int idx=0; - while(head) { - if(head->val == target) { - return idx; - } - head = head -> next; - idx ++; + int index = 0; + while (head) { + if (head->val == target) + return index; + head = head->next; + index++; } return -1; } @@ -55,10 +52,10 @@ int main() { /* 初始化链表 */ // 初始化各个结点 ListNode* n0 = newListNode(1); - ListNode* n1 = newListNode(2); - ListNode* n2 = newListNode(3); - ListNode* n3 = newListNode(4); - ListNode* n4 = newListNode(5); + ListNode* n1 = newListNode(3); + ListNode* n2 = newListNode(2); + ListNode* n3 = newListNode(5); + ListNode* n4 = newListNode(4); // 构建引用指向 n0->next = n1; n1->next = n2; @@ -82,8 +79,8 @@ int main() { printf("链表中索引 3 处的结点的值 = %d\r\n", node->val); /* 查找结点 */ - int index = findNode(n0, 5); - printf("链表中值为 5 的结点的索引 = %d\r\n", index); + int index = findNode(n0, 2); + printf("链表中值为 2 的结点的索引 = %d\r\n", index); return 0; } From 78d7d07bd9cfc4308b50cc756b0743c9e0d0367c Mon Sep 17 00:00:00 2001 From: Gonglja Date: Fri, 13 Jan 2023 06:01:21 +0800 Subject: [PATCH 4/7] style(codes/c): update comment format --- codes/c/chapter_array_and_linkedlist/linked_list.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/codes/c/chapter_array_and_linkedlist/linked_list.c b/codes/c/chapter_array_and_linkedlist/linked_list.c index 5ce7cf4c0..8228c48c9 100644 --- a/codes/c/chapter_array_and_linkedlist/linked_list.c +++ b/codes/c/chapter_array_and_linkedlist/linked_list.c @@ -7,13 +7,15 @@ #include "../include/include.h" /* 在链表的结点 n0 之后插入结点 P */ -void insertNode(ListNode* n0, ListNode* P) { +void insert(ListNode* n0, ListNode* P) { ListNode *n1 = n0->next; n0->next = P; P->next = n1; } /* 删除链表的结点 n0 之后的首个结点 */ +/* Keep Sample, 受 c 语言特性限制,removeNode 无法更改为 remove, + 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888 */ void removeNode(ListNode* n0) { if (!n0->next) return; @@ -26,7 +28,7 @@ void removeNode(ListNode* n0) { } /* 访问链表中索引为 index 的结点 */ -ListNode* accessNode(ListNode* head, int index) { +ListNode* access(ListNode* head, int index) { while (head && head->next && index) { head = head->next; index--; @@ -35,7 +37,7 @@ ListNode* accessNode(ListNode* head, int index) { } /* 在链表中查找值为 target 的首个结点 */ -int findNode(ListNode* head, int target) { +int find(ListNode* head, int target) { int index = 0; while (head) { if (head->val == target) @@ -65,7 +67,7 @@ int main() { printLinkedList(n0); /* 插入结点 */ - insertNode(n0, newListNode(0)); + insert(n0, newListNode(0)); printf("插入结点后的链表为\r\n"); printLinkedList(n0); @@ -75,11 +77,11 @@ int main() { printLinkedList(n0); /* 访问结点 */ - ListNode* node = accessNode(n0, 3); + ListNode* node = access(n0, 3); printf("链表中索引 3 处的结点的值 = %d\r\n", node->val); /* 查找结点 */ - int index = findNode(n0, 2); + int index = find(n0, 2); printf("链表中值为 2 的结点的索引 = %d\r\n", index); return 0; From 845e70366d6cbb114b47f79a5ce6832e5e0fda7d Mon Sep 17 00:00:00 2001 From: Gonglja Date: Fri, 13 Jan 2023 15:07:12 +0800 Subject: [PATCH 5/7] feat(codes/cpp): revert the changes `size_t` back to `int` --- codes/cpp/chapter_array_and_linkedlist/list.cpp | 4 ++-- .../worst_best_time_complexity.cpp | 2 +- codes/cpp/chapter_searching/hashing_search.cpp | 2 +- codes/cpp/chapter_searching/linear_search.cpp | 2 +- codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/codes/cpp/chapter_array_and_linkedlist/list.cpp b/codes/cpp/chapter_array_and_linkedlist/list.cpp index aadf83d40..44bbf88c8 100644 --- a/codes/cpp/chapter_array_and_linkedlist/list.cpp +++ b/codes/cpp/chapter_array_and_linkedlist/list.cpp @@ -48,8 +48,8 @@ int main() { PrintUtil::printVector(list); /* 通过索引遍历列表 */ - size_t count = 0; - for (size_t i = 0; i < list.size(); i++) { + int count = 0; + for (int i = 0; i < list.size(); i++) { count++; } diff --git a/codes/cpp/chapter_computational_complexity/worst_best_time_complexity.cpp b/codes/cpp/chapter_computational_complexity/worst_best_time_complexity.cpp index c3f35adaf..c2916cb40 100644 --- a/codes/cpp/chapter_computational_complexity/worst_best_time_complexity.cpp +++ b/codes/cpp/chapter_computational_complexity/worst_best_time_complexity.cpp @@ -22,7 +22,7 @@ vector randomNumbers(int n) { /* 查找数组 nums 中数字 1 所在索引 */ int findOne(vector& nums) { - for (size_t i = 0; i < nums.size(); i++) { + for (int i = 0; i < nums.size(); i++) { if (nums[i] == 1) return i; } diff --git a/codes/cpp/chapter_searching/hashing_search.cpp b/codes/cpp/chapter_searching/hashing_search.cpp index 57c9f5d16..ebc2fb01c 100644 --- a/codes/cpp/chapter_searching/hashing_search.cpp +++ b/codes/cpp/chapter_searching/hashing_search.cpp @@ -33,7 +33,7 @@ int main() { vector nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 }; // 初始化哈希表 unordered_map map; - for (size_t i = 0; i < nums.size(); i++) { + for (int i = 0; i < nums.size(); i++) { map[nums[i]] = i; // key: 元素,value: 索引 } int index = hashingSearch(map, target); diff --git a/codes/cpp/chapter_searching/linear_search.cpp b/codes/cpp/chapter_searching/linear_search.cpp index 45a08ded7..7b5092762 100644 --- a/codes/cpp/chapter_searching/linear_search.cpp +++ b/codes/cpp/chapter_searching/linear_search.cpp @@ -9,7 +9,7 @@ /* 线性查找(数组) */ int linearSearch(vector& nums, int target) { // 遍历数组 - for (size_t i = 0; i < nums.size(); i++) { + for (int i = 0; i < nums.size(); i++) { // 找到目标元素,返回其索引 if (nums[i] == target) return i; diff --git a/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp b/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp index 5694e702e..ae22740d6 100644 --- a/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp +++ b/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp @@ -68,7 +68,7 @@ public: vector toVector() { ListNode* node = front; vector res(size()); - for (size_t i = 0; i < res.size(); i++) { + for (int i = 0; i < res.size(); i++) { res[i] = node->val; node = node->next; } From 750d4f502d6cc71a539d35c13ec45898d45383fe Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Fri, 13 Jan 2023 16:18:05 +0800 Subject: [PATCH 6/7] Update linked_list.c --- codes/c/chapter_array_and_linkedlist/linked_list.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/c/chapter_array_and_linkedlist/linked_list.c b/codes/c/chapter_array_and_linkedlist/linked_list.c index 8228c48c9..ea61c84ca 100644 --- a/codes/c/chapter_array_and_linkedlist/linked_list.c +++ b/codes/c/chapter_array_and_linkedlist/linked_list.c @@ -14,8 +14,8 @@ void insert(ListNode* n0, ListNode* P) { } /* 删除链表的结点 n0 之后的首个结点 */ -/* Keep Sample, 受 c 语言特性限制,removeNode 无法更改为 remove, - 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888 */ +// 由于引入了 stdio.h ,此处无法使用 remove 关键词 +// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888 void removeNode(ListNode* n0) { if (!n0->next) return; From 7ab9fd68c89f4f2949deb5c85714ed34d64c1d40 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Fri, 13 Jan 2023 16:19:04 +0800 Subject: [PATCH 7/7] Update linked_list.c --- codes/c/chapter_array_and_linkedlist/linked_list.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/c/chapter_array_and_linkedlist/linked_list.c b/codes/c/chapter_array_and_linkedlist/linked_list.c index ea61c84ca..a8de08735 100644 --- a/codes/c/chapter_array_and_linkedlist/linked_list.c +++ b/codes/c/chapter_array_and_linkedlist/linked_list.c @@ -6,7 +6,7 @@ #include "../include/include.h" - /* 在链表的结点 n0 之后插入结点 P */ +/* 在链表的结点 n0 之后插入结点 P */ void insert(ListNode* n0, ListNode* P) { ListNode *n1 = n0->next; n0->next = P;