Format the C code in Clang-Format Style: Microsoft

This commit is contained in:
krahets 2023-04-17 21:13:15 +08:00
parent 1d6b7a5644
commit 9a98ff8a5e
46 changed files with 215 additions and 216 deletions

2
codes/c/.gitignore vendored
View File

@ -5,3 +5,5 @@
# Unignore all dirs # Unignore all dirs
!*/ !*/
*.dSYM/ *.dSYM/
build/

View File

@ -7,7 +7,7 @@
#include "../include/include.h" #include "../include/include.h"
/* 随机返回一个数组元素 */ /* 随机返回一个数组元素 */
int randomAccess(int* nums, int size) { int randomAccess(int *nums, int size) {
// 在区间 [0, size) 中随机抽取一个数字 // 在区间 [0, size) 中随机抽取一个数字
int randomIndex = rand() % size; int randomIndex = rand() % size;
// 获取并返回随机元素 // 获取并返回随机元素
@ -16,9 +16,9 @@ int randomAccess(int* nums, int size) {
} }
/* 扩展数组长度 */ /* 扩展数组长度 */
int* extend(int* nums, int size, int enlarge) { int *extend(int *nums, int size, int enlarge) {
// 初始化一个扩展长度后的数组 // 初始化一个扩展长度后的数组
int* res = (int *)malloc(sizeof(int) * (size + enlarge)); int *res = (int *)malloc(sizeof(int) * (size + enlarge));
// 将原数组中的所有元素复制到新数组 // 将原数组中的所有元素复制到新数组
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
res[i] = nums[i]; res[i] = nums[i];
@ -32,7 +32,7 @@ int* extend(int* nums, int size, int enlarge) {
} }
/* 在数组的索引 index 处插入元素 num */ /* 在数组的索引 index 处插入元素 num */
void insert(int* nums, int size, int num, int index) { void insert(int *nums, int size, int num, int index) {
// 把索引 index 以及之后的所有元素向后移动一位 // 把索引 index 以及之后的所有元素向后移动一位
for (int i = size - 1; i > index; i--) { for (int i = size - 1; i > index; i--) {
nums[i] = nums[i - 1]; nums[i] = nums[i - 1];
@ -42,7 +42,7 @@ void insert(int* nums, int size, int num, int index) {
} }
/* 删除索引 index 处元素 */ /* 删除索引 index 处元素 */
void removeItem(int* nums, int size, int index) { void removeItem(int *nums, int size, int index) {
// 把索引 index 之后的所有元素向前移动一位 // 把索引 index 之后的所有元素向前移动一位
for (int i = index; i < size - 1; i++) { for (int i = index; i < size - 1; i++) {
nums[i] = nums[i + 1]; nums[i] = nums[i + 1];
@ -50,7 +50,7 @@ void removeItem(int* nums, int size, int index) {
} }
/* 遍历数组 */ /* 遍历数组 */
void traverse(int* nums, int size) { void traverse(int *nums, int size) {
int count = 0; int count = 0;
// 通过索引遍历数组 // 通过索引遍历数组
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
@ -59,7 +59,7 @@ void traverse(int* nums, int size) {
} }
/* 在数组中查找指定元素 */ /* 在数组中查找指定元素 */
int find(int* nums, int size, int target) { int find(int *nums, int size, int target) {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (nums[i] == target) if (nums[i] == target)
return i; return i;
@ -67,7 +67,6 @@ int find(int* nums, int size, int target) {
return -1; return -1;
} }
/* Driver Code */ /* Driver Code */
int main() { int main() {
/* 初始化数组 */ /* 初始化数组 */
@ -76,7 +75,7 @@ int main() {
printf("数组 arr = "); printf("数组 arr = ");
printArray(arr, size); printArray(arr, size);
int nums[5] = { 1, 3, 2, 5, 4 }; int nums[5] = {1, 3, 2, 5, 4};
printf("数组 nums = "); printf("数组 nums = ");
printArray(nums, size); printArray(nums, size);
@ -86,7 +85,7 @@ int main() {
/* 长度扩展 */ /* 长度扩展 */
int enlarge = 3; int enlarge = 3;
int* res = extend(nums, size, enlarge); int *res = extend(nums, size, enlarge);
size += enlarge; size += enlarge;
printf("将数组长度扩展至 8 ,得到 nums = "); printf("将数组长度扩展至 8 ,得到 nums = ");
printArray(res, size); printArray(res, size);

View File

@ -7,7 +7,7 @@
#include "../include/include.h" #include "../include/include.h"
/* 在链表的节点 n0 之后插入节点 P */ /* 在链表的节点 n0 之后插入节点 P */
void insert(ListNode* n0, ListNode* P) { void insert(ListNode *n0, ListNode *P) {
ListNode *n1 = n0->next; ListNode *n1 = n0->next;
P->next = n1; P->next = n1;
n0->next = P; n0->next = P;
@ -16,7 +16,7 @@ void insert(ListNode* n0, ListNode* P) {
/* 删除链表的节点 n0 之后的首个节点 */ /* 删除链表的节点 n0 之后的首个节点 */
// 由于引入了 stdio.h ,此处无法使用 remove 关键词 // 由于引入了 stdio.h ,此处无法使用 remove 关键词
// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888 // 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888
void removeNode(ListNode* n0) { void removeNode(ListNode *n0) {
if (!n0->next) if (!n0->next)
return; return;
// n0 -> P -> n1 // n0 -> P -> n1
@ -28,7 +28,7 @@ void removeNode(ListNode* n0) {
} }
/* 访问链表中索引为 index 的节点 */ /* 访问链表中索引为 index 的节点 */
ListNode* access(ListNode* head, int index) { ListNode *access(ListNode *head, int index) {
while (head && head->next && index) { while (head && head->next && index) {
head = head->next; head = head->next;
index--; index--;
@ -37,7 +37,7 @@ ListNode* access(ListNode* head, int index) {
} }
/* 在链表中查找值为 target 的首个节点 */ /* 在链表中查找值为 target 的首个节点 */
int find(ListNode* head, int target) { int find(ListNode *head, int target) {
int index = 0; int index = 0;
while (head) { while (head) {
if (head->val == target) if (head->val == target)
@ -48,16 +48,15 @@ int find(ListNode* head, int target) {
return -1; return -1;
} }
/* Driver Code */ /* Driver Code */
int main() { int main() {
/* 初始化链表 */ /* 初始化链表 */
// 初始化各个节点 // 初始化各个节点
ListNode* n0 = newListNode(1); ListNode *n0 = newListNode(1);
ListNode* n1 = newListNode(3); ListNode *n1 = newListNode(3);
ListNode* n2 = newListNode(2); ListNode *n2 = newListNode(2);
ListNode* n3 = newListNode(5); ListNode *n3 = newListNode(5);
ListNode* n4 = newListNode(4); ListNode *n4 = newListNode(4);
// 构建引用指向 // 构建引用指向
n0->next = n1; n0->next = n1;
n1->next = n2; n1->next = n2;
@ -77,7 +76,7 @@ int main() {
printLinkedList(n0); printLinkedList(n0);
/* 访问节点 */ /* 访问节点 */
ListNode* node = access(n0, 3); ListNode *node = access(n0, 3);
printf("链表中索引 3 处的节点的值 = %d\r\n", node->val); printf("链表中索引 3 处的节点的值 = %d\r\n", node->val);
/* 查找节点 */ /* 查找节点 */

View File

@ -93,7 +93,7 @@ int removeNum(myList *list, int index) {
void extendCapacity(myList *list) { void extendCapacity(myList *list) {
// 先分配空间 // 先分配空间
int newCapacity = capacity(list) * list->extendRatio; int newCapacity = capacity(list) * list->extendRatio;
int *extend = (int *) malloc(sizeof(int) * newCapacity); int *extend = (int *)malloc(sizeof(int) * newCapacity);
int *temp = list->nums; int *temp = list->nums;
// 拷贝旧数据到新数据 // 拷贝旧数据到新数据
@ -113,6 +113,7 @@ int *toArray(myList *list) {
return list->nums; return list->nums;
} }
/* Driver Code */
int main() { int main() {
/* 初始化列表 */ /* 初始化列表 */
myList *list = newMyList(); myList *list = newMyList();
@ -157,4 +158,6 @@ int main() {
/* 释放分配内存 */ /* 释放分配内存 */
delMyList(list); delMyList(list);
return 0;
} }

View File

@ -1 +1 @@
add_executable(linear_search linear_search.c) add_executable(binary_search binary_search.c)

View File

@ -45,7 +45,7 @@ int binarySearch1(int *nums, int len, int target) {
/* Driver Code */ /* Driver Code */
int main() { int main() {
int target = 6; int target = 6;
int nums[10] = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 }; int nums[10] = {1, 3, 6, 8, 12, 15, 23, 67, 70, 92};
/* 二分查找(双闭区间) */ /* 二分查找(双闭区间) */
int index = binarySearch(nums, 10, target); int index = binarySearch(nums, 10, target);

View File

@ -17,7 +17,7 @@ void constant(int n) {
const int a = 0; const int a = 0;
int b = 0; int b = 0;
int nums[1000]; int nums[1000];
ListNode* node = newListNode(0); ListNode *node = newListNode(0);
free(node); free(node);
// 循环中的变量占用 O(1) 空间 // 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
@ -38,11 +38,11 @@ typedef struct {
/* 线性阶 */ /* 线性阶 */
void linear(int n) { void linear(int n) {
// 长度为 n 的数组占用 O(n) 空间 // 长度为 n 的数组占用 O(n) 空间
int *nums = malloc (sizeof(int) * n); int *nums = malloc(sizeof(int) * n);
free(nums); free(nums);
// 长度为 n 的列表占用 O(n) 空间 // 长度为 n 的列表占用 O(n) 空间
ListNode** nodes = malloc(sizeof(ListNode *) * n); ListNode **nodes = malloc(sizeof(ListNode *) * n);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
nodes[i] = newListNode(i); nodes[i] = newListNode(i);
} }
@ -56,7 +56,7 @@ void linear(int n) {
hashTable *h = NULL; hashTable *h = NULL;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
hashTable *tmp = malloc(sizeof (hashTable)); hashTable *tmp = malloc(sizeof(hashTable));
tmp->key = i; tmp->key = i;
sprintf(tmp->val, "%d", i); sprintf(tmp->val, "%d", i);
HASH_ADD_INT(h, key, tmp); HASH_ADD_INT(h, key, tmp);
@ -81,7 +81,7 @@ void linearRecur(int n) {
/* 平方阶 */ /* 平方阶 */
void quadratic(int n) { void quadratic(int n) {
// 二维列表占用 O(n^2) 空间 // 二维列表占用 O(n^2) 空间
int** numMatrix = malloc(sizeof(int *) * n); int **numMatrix = malloc(sizeof(int *) * n);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
int *tmp = malloc(sizeof(int) * n); int *tmp = malloc(sizeof(int) * n);
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
@ -101,7 +101,7 @@ void quadratic(int n) {
int quadraticRecur(int n) { int quadraticRecur(int n) {
if (n <= 0) if (n <= 0)
return 0; return 0;
int* nums = malloc(sizeof(int) * n); int *nums = malloc(sizeof(int) * n);
printf("递归 n = %d 中的 nums 长度 = %d\r\n", n, n); printf("递归 n = %d 中的 nums 长度 = %d\r\n", n, n);
free(nums); free(nums);
return quadraticRecur(n - 1); return quadraticRecur(n - 1);

View File

@ -12,7 +12,7 @@ int constant(int n) {
int size = 100000; int size = 100000;
int i = 0; int i = 0;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
count ++; count++;
} }
return count; return count;
} }
@ -21,7 +21,7 @@ int constant(int n) {
int linear(int n) { int linear(int n) {
int count = 0; int count = 0;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
count ++; count++;
} }
return count; return count;
} }
@ -31,19 +31,18 @@ int arrayTraversal(int *nums, int n) {
int count = 0; int count = 0;
// 循环次数与数组长度成正比 // 循环次数与数组长度成正比
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
count ++; count++;
} }
return count; return count;
} }
/* 平方阶 */ /* 平方阶 */
int quadratic(int n) int quadratic(int n) {
{
int count = 0; int count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数组长度成平方关系
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
count ++; count++;
} }
} }
return count; return count;
@ -85,7 +84,8 @@ int exponential(int n) {
/* 指数阶(递归实现) */ /* 指数阶(递归实现) */
int expRecur(int n) { int expRecur(int n) {
if (n == 1) return 1; if (n == 1)
return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1; return expRecur(n - 1) + expRecur(n - 1) + 1;
} }
@ -101,24 +101,26 @@ int logarithmic(float n) {
/* 对数阶(递归实现) */ /* 对数阶(递归实现) */
int logRecur(float n) { int logRecur(float n) {
if (n <= 1) return 0; if (n <= 1)
return 0;
return logRecur(n / 2) + 1; return logRecur(n / 2) + 1;
} }
/* 线性对数阶 */ /* 线性对数阶 */
int linearLogRecur(float n) { int linearLogRecur(float n) {
if (n <= 1) return 1; if (n <= 1)
int count = linearLogRecur(n / 2) + return 1;
linearLogRecur(n / 2); int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
count ++; count++;
} }
return count; return count;
} }
/* 阶乘阶(递归实现) */ /* 阶乘阶(递归实现) */
int factorialRecur(int n) { int factorialRecur(int n) {
if (n == 0) return 1; if (n == 0)
return 1;
int count = 0; int count = 0;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
count += factorialRecur(n - 1); count += factorialRecur(n - 1);
@ -172,5 +174,6 @@ int main(int argc, char *argv[]) {
nums = NULL; nums = NULL;
} }
getchar(); getchar();
return 0; return 0;
} }

View File

@ -29,7 +29,8 @@ int findOne(int *nums, int n) {
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
if (nums[i] == 1) return i; if (nums[i] == 1)
return i;
} }
return -1; return -1;
} }
@ -51,5 +52,6 @@ int main(int argc, char *argv[]) {
nums = NULL; nums = NULL;
} }
} }
return 0; return 0;
} }

View File

@ -23,7 +23,7 @@ void siftUp(maxHeap *h, int i);
/* 构造方法,根据切片建堆 */ /* 构造方法,根据切片建堆 */
maxHeap *newMaxHeap(int nums[], int size) { maxHeap *newMaxHeap(int nums[], int size) {
// 所有元素入堆 // 所有元素入堆
maxHeap *h = (maxHeap *) malloc(sizeof(maxHeap)); maxHeap *h = (maxHeap *)malloc(sizeof(maxHeap));
h->size = size; h->size = size;
memcpy(h->data, nums, size * sizeof(int)); memcpy(h->data, nums, size * sizeof(int));
for (int i = size - 1; i >= 0; i--) { for (int i = size - 1; i >= 0; i--) {
@ -49,7 +49,7 @@ int parent(maxHeap *h, int i) {
} }
/* 交换元素 */ /* 交换元素 */
int swap(maxHeap *h, int i, int j) { void swap(maxHeap *h, int i, int j) {
int temp = h->data[i]; int temp = h->data[i];
h->data[i] = h->data[j]; h->data[i] = h->data[j];
h->data[j] = temp; h->data[j] = temp;
@ -71,7 +71,7 @@ int peek(maxHeap *h) {
} }
/* 元素入堆 */ /* 元素入堆 */
int push(maxHeap *h, int val) { void push(maxHeap *h, int val) {
// 默认情况下,不应该添加这么多节点 // 默认情况下,不应该添加这么多节点
if (h->size == MAX_SIZE) { if (h->size == MAX_SIZE) {
printf("heap is full!"); printf("heap is full!");
@ -104,7 +104,6 @@ int pop(maxHeap *h) {
return val; return val;
} }
/* 从节点 i 开始,从顶至底堆化 */ /* 从节点 i 开始,从顶至底堆化 */
void siftDown(maxHeap *h, int i) { void siftDown(maxHeap *h, int i) {
while (true) { while (true) {
@ -145,6 +144,7 @@ void siftUp(maxHeap *h, int i) {
} }
} }
/* Driver Code */
int main() { int main() {
/* 初始化堆 */ /* 初始化堆 */
// 初始化大顶堆 // 初始化大顶堆

View File

@ -1,2 +1 @@
add_executable(binary_search binary_search.c)
add_executable(leetcode_two_sum leetcode_two_sum.c) add_executable(leetcode_two_sum leetcode_two_sum.c)

View File

@ -9,7 +9,7 @@
/* 哈希表 */ /* 哈希表 */
struct hashTable { struct hashTable {
int key; // key 为 int 型 int key; // key 为 int 型
void* val; // val 为 void * 型 void *val; // val 为 void * 型
UT_hash_handle hh; // 借助 uthash 实现的哈希表 UT_hash_handle hh; // 借助 uthash 实现的哈希表
}; };
@ -32,7 +32,7 @@ void printHashTableLinklist(hashTable **ht) {
} }
/* 哈希表查找 */ /* 哈希表查找 */
hashTable* find(hashTable *ht, int k) { hashTable *find(hashTable *ht, int k) {
hashTable *s; hashTable *s;
HASH_FIND_INT(ht, &k, s); HASH_FIND_INT(ht, &k, s);
return s; return s;
@ -43,7 +43,7 @@ void addInt(hashTable **ht, int k, int v) {
hashTable *s; hashTable *s;
HASH_FIND_INT(*ht, &k, s); HASH_FIND_INT(*ht, &k, s);
if (s == NULL) { if (s == NULL) {
s = malloc (sizeof(*s)); s = malloc(sizeof(*s));
s->key = k; s->key = k;
HASH_ADD_INT(*ht, key, s); HASH_ADD_INT(*ht, key, s);
} }
@ -53,11 +53,11 @@ void addInt(hashTable **ht, int k, int v) {
} }
/* 添加 k、v 到哈希表 ht 中v 为 ListNode* 类型 */ /* 添加 k、v 到哈希表 ht 中v 为 ListNode* 类型 */
void addLinkNode(hashTable **ht, int k, ListNode* v) { void addLinkNode(hashTable **ht, int k, ListNode *v) {
hashTable *s; hashTable *s;
HASH_FIND_INT(*ht, &k, s); HASH_FIND_INT(*ht, &k, s);
if (s == NULL) { if (s == NULL) {
s = malloc (sizeof(*s)); s = malloc(sizeof(*s));
s->key = k; s->key = k;
HASH_ADD_INT(*ht, key, s); HASH_ADD_INT(*ht, key, s);
} }
@ -76,7 +76,7 @@ void deleteAll(hashTable **ht) {
/* 通过 key 访问哈希表,返回 int 类型 */ /* 通过 key 访问哈希表,返回 int 类型 */
int accessInt(hashTable **h, int key) { int accessInt(hashTable **h, int key) {
hashTable *s; hashTable *s;
int ret=-1; int ret = -1;
if (s = find(*h, key)) { if (s = find(*h, key)) {
ret = *(int *)s->val; ret = *(int *)s->val;
} }
@ -84,9 +84,9 @@ int accessInt(hashTable **h, int key) {
} }
/* 通过 key 访问哈希表,返回 ListNode* 类型 */ /* 通过 key 访问哈希表,返回 ListNode* 类型 */
ListNode* accessLinkNode(hashTable **h, int key) { ListNode *accessLinkNode(hashTable **h, int key) {
hashTable *s; hashTable *s;
ListNode *res=NULL; ListNode *res = NULL;
if (s = find(*h, key)) { if (s = find(*h, key)) {
res = (ListNode *)s->val; res = (ListNode *)s->val;
} }
@ -118,9 +118,9 @@ int main() {
/* 哈希查找(数组) */ /* 哈希查找(数组) */
int nums[] = {1, 5, 3, 2, 4, 7, 5, 9, 10, 8}; int nums[] = {1, 5, 3, 2, 4, 7, 5, 9, 10, 8};
// 初始化哈希表 // 初始化哈希表
hashTable** ht = malloc(sizeof(*ht)); hashTable **ht = malloc(sizeof(*ht));
*ht = NULL; *ht = NULL;
for (int i = 0; i < sizeof(nums)/sizeof(nums[0]); i++) { for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
addInt(ht, nums[i], i); // key: 元素value: 索引 addInt(ht, nums[i], i); // key: 元素value: 索引
} }
int index = hashingSearchArray(ht, target); int index = hashingSearchArray(ht, target);
@ -128,9 +128,9 @@ int main() {
deleteAll(ht); deleteAll(ht);
/* 哈希查找(链表) */ /* 哈希查找(链表) */
ListNode *head = arrToLinkedList(nums, sizeof(nums)/sizeof(nums[0])); ListNode *head = arrToLinkedList(nums, sizeof(nums) / sizeof(nums[0]));
// 初始化哈希表 // 初始化哈希表
hashTable** ht1 = malloc(sizeof(*ht)); hashTable **ht1 = malloc(sizeof(*ht));
*ht1 = NULL; *ht1 = NULL;
while (head != NULL) { while (head != NULL) {

View File

@ -66,6 +66,7 @@ int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {
return NULL; return NULL;
} }
/* Driver Code */
int main() { int main() {
// ======= Test Case ======= // ======= Test Case =======
int nums[] = {2, 7, 11, 15}; int nums[] = {2, 7, 11, 15};

View File

@ -19,7 +19,7 @@ int linearSearchArray(int *nums, int len, int target) {
} }
/* 线性查找(链表) */ /* 线性查找(链表) */
ListNode* linearSearchLinkedList(ListNode* head, int target) { ListNode *linearSearchLinkedList(ListNode *head, int target) {
// 遍历链表 // 遍历链表
while (head != NULL) { while (head != NULL) {
// 找到目标节点,返回之 // 找到目标节点,返回之
@ -36,14 +36,14 @@ int main() {
int target = 3; int target = 3;
/* 在数组中执行线性查找 */ /* 在数组中执行线性查找 */
int nums[10] = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 }; int nums[10] = {1, 5, 3, 2, 4, 7, 5, 9, 10, 8};
int index = linearSearchArray(nums, 10, target); int index = linearSearchArray(nums, 10, target);
printf("目标元素 3 的索引 = %d\n", index); printf("目标元素 3 的索引 = %d\n", index);
/* 在链表中执行线性查找 */ /* 在链表中执行线性查找 */
ListNode* head = arrToLinkedList(nums, 10); ListNode *head = arrToLinkedList(nums, 10);
ListNode* node = linearSearchLinkedList(head, target); ListNode *node = linearSearchLinkedList(head, target);
if(node == NULL) { if (node == NULL) {
printf("目标节点值 3 的对应节点对象为 NULL\n"); printf("目标节点值 3 的对应节点对象为 NULL\n");
} else { } else {
printf("目标节点值 3 的对应节点对象为 addr: %p val: %d\n", node, node->val); printf("目标节点值 3 的对应节点对象为 addr: %p val: %d\n", node, node->val);

View File

@ -7,8 +7,7 @@
#include "../include/include.h" #include "../include/include.h"
/* 冒泡排序 */ /* 冒泡排序 */
void bucketSort(double nums[], int size) void bucketSort(double nums[], int size) {
{
// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素 // 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
int k = size / 2; int k = size / 2;
// 1. 将数组元素分配到各个桶中 // 1. 将数组元素分配到各个桶中
@ -23,18 +22,13 @@ void bucketSort(double nums[], int size)
} }
/* Driver Code */ /* Driver Code */
int main() int main() {
{
// 设输入数据为浮点数,范围为 [0, 1) // 设输入数据为浮点数,范围为 [0, 1)
double nums[] = {0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37}; double nums[] = {0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37};
int size = sizeof(nums) / sizeof(double); int size = sizeof(nums) / sizeof(double);
bucketSort(nums, size); bucketSort(nums, size);
printf("桶排序完成后 nums = "); printf("桶排序完成后 nums = ");
printf("["); printArray(nums, size);
for (int i = 0; i < size - 1; i++)
{
printf("%g, ", nums[i]);
}
printf("]");
} }

View File

@ -9,12 +9,10 @@
/* 插入排序 */ /* 插入排序 */
void insertionSort(int nums[], int size) { void insertionSort(int nums[], int size) {
// 外循环base = nums[1], nums[2], ..., nums[n-1] // 外循环base = nums[1], nums[2], ..., nums[n-1]
for (int i = 1; i < size; i++) for (int i = 1; i < size; i++) {
{
int base = nums[i], j = i - 1; int base = nums[i], j = i - 1;
// 内循环:将 base 插入到左边的正确位置 // 内循环:将 base 插入到左边的正确位置
while (j >= 0 && nums[j] > base) while (j >= 0 && nums[j] > base) {
{
// 1. 将 nums[j] 向右移动一位 // 1. 将 nums[j] 向右移动一位
nums[j + 1] = nums[j]; nums[j + 1] = nums[j];
j--; j--;
@ -29,8 +27,7 @@ int main() {
int nums[] = {4, 1, 3, 1, 5, 2}; int nums[] = {4, 1, 3, 1, 5, 2};
insertionSort(nums, 6); insertionSort(nums, 6);
printf("插入排序完成后 nums = "); printf("插入排序完成后 nums = ");
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++) {
{
printf("%d ", nums[i]); printf("%d ", nums[i]);
} }
printf("\n"); printf("\n");

View File

@ -13,7 +13,7 @@ void merge(int *nums, int left, int mid, int right) {
int index; int index;
// 初始化辅助数组 // 初始化辅助数组
int tmp[right + 1 - left]; int tmp[right + 1 - left];
for(index = left; index < right + 1; index++) { for (index = left; index < right + 1; index++) {
tmp[index - left] = nums[index]; tmp[index - left] = nums[index];
} }
// 左子数组的起始索引和结束索引 // 左子数组的起始索引和结束索引
@ -39,7 +39,8 @@ void merge(int *nums, int left, int mid, int right) {
/* 归并排序 */ /* 归并排序 */
void mergeSort(int *nums, int left, int right) { void mergeSort(int *nums, int left, int right) {
// 终止条件 // 终止条件
if (left >= right) return; // 当子数组长度为 1 时终止递归 if (left >= right)
return; // 当子数组长度为 1 时终止递归
// 划分阶段 // 划分阶段
int mid = (left + right) / 2; // 计算中点 int mid = (left + right) / 2; // 计算中点
mergeSort(nums, left, mid); // 递归左子数组 mergeSort(nums, left, mid); // 递归左子数组
@ -51,10 +52,11 @@ void mergeSort(int *nums, int left, int right) {
/* Driver Code */ /* Driver Code */
int main() { int main() {
/* 归并排序 */ /* 归并排序 */
int nums[] = { 7, 3, 2, 6, 0, 1, 5, 4 }; int nums[] = {7, 3, 2, 6, 0, 1, 5, 4};
int size = sizeof(nums) / sizeof(int); int size = sizeof(nums) / sizeof(int);
mergeSort(nums, 0, size - 1); mergeSort(nums, 0, size - 1);
printf("归并排序完成后 nums = "); printf("归并排序完成后 nums = ");
printArray(nums, size); printArray(nums, size);
return 0; return 0;
} }

View File

@ -93,7 +93,6 @@ void quickSortMedian(int nums[], int left, int right) {
quickSortMedian(nums, pivot + 1, right); quickSortMedian(nums, pivot + 1, right);
} }
/* 快速排序类(尾递归优化) */ /* 快速排序类(尾递归优化) */
/* 尾递归优化-哨兵划分 */ /* 尾递归优化-哨兵划分 */
int quickSortTailCallPartition(int nums[], int left, int right) { int quickSortTailCallPartition(int nums[], int left, int right) {
@ -110,7 +109,6 @@ int quickSortTailCallPartition(int nums[], int left, int right) {
return i; // 返回基准数的索引 return i; // 返回基准数的索引
} }
// 快速排序(尾递归优化) // 快速排序(尾递归优化)
void quickSortTailCall(int nums[], int left, int right) { void quickSortTailCall(int nums[], int left, int right) {
// 子数组长度为 1 时终止 // 子数组长度为 1 时终止
@ -128,7 +126,6 @@ void quickSortTailCall(int nums[], int left, int right) {
} }
} }
/* Driver Code */ /* Driver Code */
int main() { int main() {
/* 快速排序 */ /* 快速排序 */

View File

@ -15,7 +15,7 @@ int digit(int num, int exp) {
/* 计数排序(根据 nums 第 k 位排序) */ /* 计数排序(根据 nums 第 k 位排序) */
void countingSortDigit(int nums[], int size, int exp) { void countingSortDigit(int nums[], int size, int exp) {
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶 // 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
int *counter = (int *) malloc((sizeof(int) * 10)); int *counter = (int *)malloc((sizeof(int) * 10));
// 统计 0~9 各数字的出现次数 // 统计 0~9 各数字的出现次数
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
// 获取 nums[i] 第 k 位,记为 d // 获取 nums[i] 第 k 位,记为 d
@ -28,7 +28,7 @@ void countingSortDigit(int nums[], int size, int exp) {
counter[i] += counter[i - 1]; counter[i] += counter[i - 1];
} }
// 倒序遍历,根据桶内统计结果,将各元素填入 res // 倒序遍历,根据桶内统计结果,将各元素填入 res
int *res = (int *) malloc(sizeof(int) * size); int *res = (int *)malloc(sizeof(int) * size);
for (int i = size - 1; i >= 0; i--) { for (int i = size - 1; i >= 0; i--) {
int d = digit(nums[i], exp); int d = digit(nums[i], exp);
int j = counter[d] - 1; // 获取 d 在数组中的索引 j int j = counter[d] - 1; // 获取 d 在数组中的索引 j
@ -59,6 +59,7 @@ void radixSort(int nums[], int size) {
countingSortDigit(nums, size, exp); countingSortDigit(nums, size, exp);
} }
/* Driver Code */
int main() { int main() {
// 基数排序 // 基数排序
int nums[] = {10546151, 35663510, 42865989, 34862445, 81883077, int nums[] = {10546151, 35663510, 42865989, 34862445, 81883077,

View File

@ -18,10 +18,10 @@ typedef struct ArrayDeque ArrayDeque;
/* 构造方法 */ /* 构造方法 */
ArrayDeque *newArrayDeque(int capacity) { ArrayDeque *newArrayDeque(int capacity) {
ArrayDeque *deque = (ArrayDeque *) malloc(sizeof(ArrayDeque)); ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));
// 初始化数组 // 初始化数组
deque->queCapacity = capacity; deque->queCapacity = capacity;
deque->nums = (int *) malloc(sizeof(int) * deque->queCapacity); deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);
deque->front = deque->queSize = 0; deque->front = deque->queSize = 0;
return deque; return deque;
} }
@ -122,7 +122,6 @@ void printArrayDeque(ArrayDeque *deque) {
printArray(arr, deque->queSize); printArray(arr, deque->queSize);
} }
/* Driver Code */ /* Driver Code */
int main() { int main() {
/* 初始化队列 */ /* 初始化队列 */

View File

@ -18,10 +18,10 @@ typedef struct ArrayQueue ArrayQueue;
/* 构造方法 */ /* 构造方法 */
ArrayQueue *newArrayQueue(int capacity) { ArrayQueue *newArrayQueue(int capacity) {
ArrayQueue *queue = (ArrayQueue *) malloc(sizeof(ArrayQueue)); ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));
// 初始化数组 // 初始化数组
queue->queCapacity = capacity; queue->queCapacity = capacity;
queue->nums = (int *) malloc(sizeof(int) * queue->queCapacity); queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);
queue->front = queue->queSize = 0; queue->front = queue->queSize = 0;
return queue; return queue;
} }
@ -85,7 +85,6 @@ void printArrayQueue(ArrayQueue *queue) {
printArray(arr, queue->queSize); printArray(arr, queue->queSize);
} }
/* Driver Code */ /* Driver Code */
int main() { int main() {
/* 初始化队列 */ /* 初始化队列 */

View File

@ -67,7 +67,7 @@ int pop(arrayStack *s) {
/* Driver Code */ /* Driver Code */
int main() { int main() {
/* 初始化栈 */ /* 初始化栈 */
arrayStack * stack = newArrayStack(); arrayStack *stack = newArrayStack();
/* 元素入栈 */ /* 元素入栈 */
push(stack, 1); push(stack, 1);

View File

@ -17,7 +17,7 @@ typedef struct DoublyListNode DoublyListNode;
/* 双向链表节点构造方法 */ /* 双向链表节点构造方法 */
DoublyListNode *newDoublyListNode(int num) { DoublyListNode *newDoublyListNode(int num) {
DoublyListNode* new = (DoublyListNode *) malloc(sizeof(DoublyListNode)); DoublyListNode *new = (DoublyListNode *)malloc(sizeof(DoublyListNode));
new->val = num; new->val = num;
new->next = NULL; new->next = NULL;
new->prev = NULL; new->prev = NULL;
@ -39,7 +39,7 @@ typedef struct LinkedListDeque LinkedListDeque;
/* 构造方法 */ /* 构造方法 */
LinkedListDeque *newLinkedListDeque() { LinkedListDeque *newLinkedListDeque() {
LinkedListDeque *deque = (LinkedListDeque *) malloc(sizeof(LinkedListDeque)); LinkedListDeque *deque = (LinkedListDeque *)malloc(sizeof(LinkedListDeque));
deque->front = NULL; deque->front = NULL;
deque->rear = NULL; deque->rear = NULL;
deque->queSize = 0; deque->queSize = 0;
@ -48,7 +48,7 @@ LinkedListDeque *newLinkedListDeque() {
/* 析构方法 */ /* 析构方法 */
void delLinkedListdeque(LinkedListDeque *deque) { void delLinkedListdeque(LinkedListDeque *deque) {
// 释放所有节点 // 释放所有节点
for (int i=0; i<deque->queSize && deque->front != NULL; i++) { for (int i = 0; i < deque->queSize && deque->front != NULL; i++) {
DoublyListNode *tmp = deque->front; DoublyListNode *tmp = deque->front;
deque->front = deque->front->next; deque->front = deque->front->next;
free(tmp); free(tmp);
@ -75,11 +75,11 @@ void push(LinkedListDeque *deque, int num, bool isFront) {
deque->front = deque->rear = node; deque->front = deque->rear = node;
} }
// 队首入队操作 // 队首入队操作
else if (isFront){ else if (isFront) {
// 将 node 添加至链表头部 // 将 node 添加至链表头部
deque->front->prev = node; deque->front->prev = node;
node->next = deque->front; node->next = deque->front;
deque->front = node;// 更新头节点 deque->front = node; // 更新头节点
} }
// 对尾入队操作 // 对尾入队操作
else { else {
@ -119,7 +119,7 @@ int pop(LinkedListDeque *deque, bool isFront) {
return -1; return -1;
int val; int val;
// 队首出队操作 // 队首出队操作
if(isFront) { if (isFront) {
val = peekFirst(deque); // 暂存头节点值 val = peekFirst(deque); // 暂存头节点值
DoublyListNode *fNext = deque->front->next; DoublyListNode *fNext = deque->front->next;
if (fNext) { if (fNext) {
@ -160,7 +160,7 @@ void printLinkedListDeque(LinkedListDeque *deque) {
// 拷贝链表中的数据到数组 // 拷贝链表中的数据到数组
int i; int i;
DoublyListNode *node; DoublyListNode *node;
for (i=0, node = deque->front; i < deque->queSize; i++){ for (i = 0, node = deque->front; i < deque->queSize; i++) {
arr[i] = node->val; arr[i] = node->val;
node = node->next; node = node->next;
} }
@ -211,4 +211,3 @@ int main() {
delLinkedListdeque(deque); delLinkedListdeque(deque);
return 0; return 0;
} }

View File

@ -16,7 +16,7 @@ typedef struct LinkedListQueue LinkedListQueue;
/* 构造方法 */ /* 构造方法 */
LinkedListQueue *newLinkedListQueue() { LinkedListQueue *newLinkedListQueue() {
LinkedListQueue *queue = (LinkedListQueue *) malloc(sizeof(LinkedListQueue)); LinkedListQueue *queue = (LinkedListQueue *)malloc(sizeof(LinkedListQueue));
queue->front = NULL; queue->front = NULL;
queue->rear = NULL; queue->rear = NULL;
queue->queSize = 0; queue->queSize = 0;
@ -25,7 +25,7 @@ LinkedListQueue *newLinkedListQueue() {
/* 析构方法 */ /* 析构方法 */
void delLinkedListQueue(LinkedListQueue *queue) { void delLinkedListQueue(LinkedListQueue *queue) {
// 释放所有节点 // 释放所有节点
for (int i=0; i<queue->queSize && queue->front != NULL; i++) { for (int i = 0; i < queue->queSize && queue->front != NULL; i++) {
ListNode *tmp = queue->front; ListNode *tmp = queue->front;
queue->front = queue->front->next; queue->front = queue->front->next;
free(tmp); free(tmp);
@ -82,7 +82,7 @@ void printLinkedListQueue(LinkedListQueue *queue) {
// 拷贝链表中的数据到数组 // 拷贝链表中的数据到数组
int i; int i;
ListNode *node; ListNode *node;
for (i=0, node = queue->front; i < queue->queSize && queue->front != queue->rear; i++){ for (i = 0, node = queue->front; i < queue->queSize && queue->front != queue->rear; i++) {
arr[i] = node->val; arr[i] = node->val;
node = node->next; node = node->next;
} }
@ -125,4 +125,3 @@ int main() {
return 0; return 0;
} }

View File

@ -54,7 +54,7 @@ int peek(linkedListStack *s) {
/* 入栈 */ /* 入栈 */
void push(linkedListStack *s, int num) { void push(linkedListStack *s, int num) {
assert(s); assert(s);
ListNode *node = (ListNode *) malloc(sizeof(ListNode)); ListNode *node = (ListNode *)malloc(sizeof(ListNode));
node->next = s->top; // 更新新加节点指针域 node->next = s->top; // 更新新加节点指针域
node->val = num; // 更新新加节点数据域 node->val = num; // 更新新加节点数据域
s->top = node; // 更新栈顶 s->top = node; // 更新栈顶

View File

@ -15,7 +15,7 @@ typedef struct binarySearchTree binarySearchTree;
int sortIntHelper(const void *a, const void *b) { int sortIntHelper(const void *a, const void *b) {
// 从小到大排序 // 从小到大排序
return (*(int *) a - *(int *) b); return (*(int *)a - *(int *)b);
} }
/* 构建二叉搜索树 */ /* 构建二叉搜索树 */
@ -33,7 +33,7 @@ TreeNode *buildTree(int nums[], int i, int j) {
} }
binarySearchTree *newBinarySearchTree(int nums[], int size) { binarySearchTree *newBinarySearchTree(int nums[], int size) {
binarySearchTree *bst = (binarySearchTree *) malloc(sizeof(binarySearchTree)); binarySearchTree *bst = (binarySearchTree *)malloc(sizeof(binarySearchTree));
TreeNode *root; TreeNode *root;
// 从小到大排序数组 // 从小到大排序数组
qsort(nums, size, sizeof(int), sortIntHelper); qsort(nums, size, sizeof(int), sortIntHelper);
@ -108,7 +108,8 @@ void removeNode(binarySearchTree *bst, int num) {
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
while (cur != NULL) { while (cur != NULL) {
// 找到待删除节点,跳出循环 // 找到待删除节点,跳出循环
if (cur->val == num) break; if (cur->val == num)
break;
pre = cur; pre = cur;
if (cur->val < num) { if (cur->val < num) {
// 待删除节点在 root 的右子树中 // 待删除节点在 root 的右子树中
@ -159,13 +160,11 @@ int main() {
TreeNode *node = search(bst, 7); TreeNode *node = search(bst, 7);
printf("查找到的节点对象的节点值 = %d\n", node->val); printf("查找到的节点对象的节点值 = %d\n", node->val);
/* 插入节点 */ /* 插入节点 */
insert(bst, 16); insert(bst, 16);
printf("插入节点 16 后,二叉树为\n"); printf("插入节点 16 后,二叉树为\n");
printTree(getRoot(bst)); printTree(getRoot(bst));
/* 删除节点 */ /* 删除节点 */
removeNode(bst, 1); removeNode(bst, 1);
printf("删除节点 1 后,二叉树为\n"); printf("删除节点 1 后,二叉树为\n");

View File

@ -10,11 +10,11 @@
int main() { int main() {
/* 初始化二叉树 */ /* 初始化二叉树 */
// 初始化节点 // 初始化节点
TreeNode* n1 = newTreeNode(1); TreeNode *n1 = newTreeNode(1);
TreeNode* n2 = newTreeNode(2); TreeNode *n2 = newTreeNode(2);
TreeNode* n3 = newTreeNode(3); TreeNode *n3 = newTreeNode(3);
TreeNode* n4 = newTreeNode(4); TreeNode *n4 = newTreeNode(4);
TreeNode* n5 = newTreeNode(5); TreeNode *n5 = newTreeNode(5);
// 构建引用指向(即指针) // 构建引用指向(即指针)
n1->left = n2; n1->left = n2;
n1->right = n3; n1->right = n3;
@ -24,7 +24,7 @@ int main() {
printTree(n1); printTree(n1);
/* 插入与删除节点 */ /* 插入与删除节点 */
TreeNode* P = newTreeNode(0); TreeNode *P = newTreeNode(0);
// 在 n1 -> n2 中间插入节点 P // 在 n1 -> n2 中间插入节点 P
n1->left = P; n1->left = P;
P->left = n2; P->left = n2;

View File

@ -15,14 +15,14 @@ int *levelOrder(TreeNode *root, int *size) {
TreeNode **queue; TreeNode **queue;
/* 辅助队列 */ /* 辅助队列 */
queue = (TreeNode **) malloc(sizeof(TreeNode) * MAX_NODE_SIZE); queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
// 队列指针 // 队列指针
front = 0, rear = 0; front = 0, rear = 0;
// 加入根节点 // 加入根节点
queue[rear++] = root; queue[rear++] = root;
// 初始化一个列表,用于保存遍历序列 // 初始化一个列表,用于保存遍历序列
/* 辅助数组 */ /* 辅助数组 */
arr = (int *) malloc(sizeof(int) * MAX_NODE_SIZE); arr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);
// 数组指针 // 数组指针
index = 0; index = 0;
while (front < rear) { while (front < rear) {
@ -45,7 +45,6 @@ int *levelOrder(TreeNode *root, int *size) {
return arr; return arr;
} }
/* Driver Code */ /* Driver Code */
int main() { int main() {
/* 初始化二叉树 */ /* 初始化二叉树 */

View File

@ -11,7 +11,8 @@ int *arr;
/* 前序遍历 */ /* 前序遍历 */
void preOrder(TreeNode *root, int *size) { void preOrder(TreeNode *root, int *size) {
if (root == NULL) return; if (root == NULL)
return;
// 访问优先级:根节点 -> 左子树 -> 右子树 // 访问优先级:根节点 -> 左子树 -> 右子树
arr[(*size)++] = root->val; arr[(*size)++] = root->val;
preOrder(root->left, size); preOrder(root->left, size);
@ -20,7 +21,8 @@ void preOrder(TreeNode *root, int *size) {
/* 中序遍历 */ /* 中序遍历 */
void inOrder(TreeNode *root, int *size) { void inOrder(TreeNode *root, int *size) {
if (root == NULL) return; if (root == NULL)
return;
// 访问优先级:左子树 -> 根节点 -> 右子树 // 访问优先级:左子树 -> 根节点 -> 右子树
inOrder(root->left, size); inOrder(root->left, size);
arr[(*size)++] = root->val; arr[(*size)++] = root->val;
@ -29,14 +31,14 @@ void inOrder(TreeNode *root, int *size) {
/* 后序遍历 */ /* 后序遍历 */
void postOrder(TreeNode *root, int *size) { void postOrder(TreeNode *root, int *size) {
if (root == NULL) return; if (root == NULL)
return;
// 访问优先级:左子树 -> 右子树 -> 根节点 // 访问优先级:左子树 -> 右子树 -> 根节点
postOrder(root->left, size); postOrder(root->left, size);
postOrder(root->right, size); postOrder(root->right, size);
arr[(*size)++] = root->val; arr[(*size)++] = root->val;
} }
/* Driver Code */ /* Driver Code */
int main() { int main() {
/* 初始化二叉树 */ /* 初始化二叉树 */
@ -49,7 +51,7 @@ int main() {
/* 前序遍历 */ /* 前序遍历 */
// 初始化辅助数组 // 初始化辅助数组
arr = (int *) malloc(sizeof(int) * MAX_NODE_SIZE); arr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);
size = 0; size = 0;
preOrder(root, &size); preOrder(root, &size);
printf("前序遍历的节点打印序列 = "); printf("前序遍历的节点打印序列 = ");

View File

@ -7,16 +7,16 @@
#ifndef C_INCLUDE_H #ifndef C_INCLUDE_H
#define C_INCLUDE_H #define C_INCLUDE_H
#include <assert.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
#include <time.h> #include <time.h>
#include <assert.h>
#include "list_node.h" #include "list_node.h"
#include "tree_node.h"
#include "print_util.h" #include "print_util.h"
#include "tree_node.h"
// hash table lib // hash table lib
#include "uthash.h" #include "uthash.h"

View File

@ -22,7 +22,7 @@ typedef struct ListNode ListNode;
/* 构造函数,初始化一个新节点 */ /* 构造函数,初始化一个新节点 */
ListNode *newListNode(int val) { ListNode *newListNode(int val) {
ListNode *node, *next; ListNode *node, *next;
node = (ListNode *) malloc(sizeof(ListNode)); node = (ListNode *)malloc(sizeof(ListNode));
node->val = val; node->val = val;
node->next = NULL; node->next = NULL;
return node; return node;

View File

@ -8,8 +8,8 @@
#define PRINT_UTIL_H #define PRINT_UTIL_H
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "list_node.h" #include "list_node.h"
#include "tree_node.h" #include "tree_node.h"
@ -18,7 +18,6 @@
extern "C" { extern "C" {
#endif #endif
/** /**
* @brief Print an Array * @brief Print an Array
* *
@ -69,9 +68,9 @@ struct Trunk {
typedef struct Trunk Trunk; typedef struct Trunk Trunk;
Trunk *newTrunk(Trunk *prev, char *str) { Trunk *newTrunk(Trunk *prev, char *str) {
Trunk *trunk = (Trunk *) malloc(sizeof(Trunk)); Trunk *trunk = (Trunk *)malloc(sizeof(Trunk));
trunk->prev = prev; trunk->prev = prev;
trunk->str = (char *) malloc(sizeof(char) * 10); trunk->str = (char *)malloc(sizeof(char) * 10);
strcpy(trunk->str, str); strcpy(trunk->str, str);
return trunk; return trunk;
} }
@ -146,7 +145,6 @@ static void printHeap(int arr[], int size) {
printTree(root); printTree(root);
} }
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -4,7 +4,6 @@
* Author: Reanon (793584285@qq.com) * Author: Reanon (793584285@qq.com)
*/ */
#ifndef TREE_NODE_H #ifndef TREE_NODE_H
#define TREE_NODE_H #define TREE_NODE_H
@ -24,11 +23,10 @@ struct TreeNode {
typedef struct TreeNode TreeNode; typedef struct TreeNode TreeNode;
TreeNode *newTreeNode(int val) { TreeNode *newTreeNode(int val) {
TreeNode *node; TreeNode *node;
node = (TreeNode *) malloc(sizeof(TreeNode)); node = (TreeNode *)malloc(sizeof(TreeNode));
node->val = val; node->val = val;
node->height = 0; node->height = 0;
node->left = NULL; node->left = NULL;
@ -55,7 +53,7 @@ TreeNode *arrToTree(const int *arr, size_t size) {
/* 根节点 */ /* 根节点 */
root = newTreeNode(arr[0]); root = newTreeNode(arr[0]);
/* 辅助队列 */ /* 辅助队列 */
queue = (TreeNode **) malloc(sizeof(TreeNode) * MAX_NODE_SIZE); queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
// 队列指针 // 队列指针
front = 0, rear = 0; front = 0, rear = 0;
// 将根节点放入队尾 // 将根节点放入队尾
@ -83,7 +81,6 @@ TreeNode *arrToTree(const int *arr, size_t size) {
return root; return root;
} }
/** /**
* @brief Generate a binary tree with an array * @brief Generate a binary tree with an array
* *
@ -100,13 +97,13 @@ int *treeToArr(TreeNode *root) {
TreeNode *node; TreeNode *node;
TreeNode **queue; TreeNode **queue;
/* 辅助队列 */ /* 辅助队列 */
queue = (TreeNode **) malloc(sizeof(TreeNode) * MAX_NODE_SIZE); queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
// 队列指针 // 队列指针
front = 0, rear = 0; front = 0, rear = 0;
// 将根节点放入队尾 // 将根节点放入队尾
queue[rear++] = root; queue[rear++] = root;
/* 辅助数组 */ /* 辅助数组 */
arr = (int *) malloc(sizeof(int) * MAX_NODE_SIZE); arr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);
// 数组指针 // 数组指针
index = 0; index = 0;
while (front < rear) { while (front < rear) {

View File

@ -53,6 +53,7 @@ void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<ve
} }
} }
/* Driver Code */
int main() { int main() {
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7}); TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
cout << "\n初始化二叉树" << endl; cout << "\n初始化二叉树" << endl;

View File

@ -27,6 +27,7 @@ static void preOrder(TreeNode *root) {
path.pop_back(); path.pop_back();
} }
/* Driver Code */
int main() { int main() {
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7}); TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
cout << "\n初始化二叉树" << endl; cout << "\n初始化二叉树" << endl;

View File

@ -21,6 +21,7 @@ static void preOrder(TreeNode *root) {
preOrder(root->right); preOrder(root->right);
} }
/* Driver Code */
int main() { int main() {
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7}); TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
cout << "\n初始化二叉树" << endl; cout << "\n初始化二叉树" << endl;

View File

@ -26,6 +26,7 @@ static void preOrder(TreeNode *root) {
path.pop_back(); path.pop_back();
} }
/* Driver Code */
int main() { int main() {
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7}); TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
cout << "\n初始化二叉树" << endl; cout << "\n初始化二叉树" << endl;

View File

@ -19,6 +19,7 @@ void testPop(priority_queue<int> &heap) {
printHeap(heap); printHeap(heap);
} }
/* Driver Code */
int main() { int main() {
/* 初始化堆 */ /* 初始化堆 */
// 初始化小顶堆 // 初始化小顶堆

View File

@ -34,6 +34,7 @@ vector<int> twoSumHashTable(vector<int> &nums, int target) {
return {}; return {};
} }
/* Driver Code */
int main() { int main() {
// ======= Test Case ======= // ======= Test Case =======
vector<int> nums = {2, 7, 11, 15}; vector<int> nums = {2, 7, 11, 15};

View File

@ -4,6 +4,7 @@
* Author: Jefferson (JeffersonHuang77@gmail.com) * Author: Jefferson (JeffersonHuang77@gmail.com)
*/ */
/* Driver Code */
int main() { int main() {
/* 初始化列表 */ /* 初始化列表 */
List<int> list = [1,3,2,5,4]; List<int> list = [1,3,2,5,4];

View File

@ -121,6 +121,7 @@ int factorialRecur(int n) {
return count; return count;
} }
/* Driver Code */
int main() { int main() {
// n // n
int n = 8; int n = 8;

View File

@ -27,6 +27,7 @@ int findOne(List<int> nums) {
return -1; return -1;
} }
/* Driver Code */
int main() { int main() {
for (var i = 0; i < 10; i++) { for (var i = 0; i < 10; i++) {
int n = 100; int n = 100;

View File

@ -1,4 +1,4 @@
/* /**
* File: worst_best_time_complexity.js * File: worst_best_time_complexity.js
* Created Time: 2023-01-05 * Created Time: 2023-01-05
* Author: RiverTwilight (contact@rene.wang) * Author: RiverTwilight (contact@rene.wang)

View File

@ -1,8 +1,8 @@
/* /**
* File: binary_search.ts * File: binary_search.ts
* Created Time: 2022-12-27 * Created Time: 2022-12-27
* Author: Daniel (better.sunjian@gmail.com) * Author: Daniel (better.sunjian@gmail.com)
*/ */
/* 二分查找(双闭区间) */ /* 二分查找(双闭区间) */
function binarySearch(nums: number[], target: number): number { function binarySearch(nums: number[], target: number): number {

View File

@ -1,4 +1,4 @@
/* /**
* File: worst_best_time_complexity.ts * File: worst_best_time_complexity.ts
* Created Time: 2023-01-05 * Created Time: 2023-01-05
* Author: RiverTwilight (contact@rene.wang) * Author: RiverTwilight (contact@rene.wang)