Format the C code in Clang-Format Style: Microsoft
This commit is contained in:
parent
1d6b7a5644
commit
9a98ff8a5e
4
codes/c/.gitignore
vendored
4
codes/c/.gitignore
vendored
@ -4,4 +4,6 @@
|
||||
!*.*
|
||||
# Unignore all dirs
|
||||
!*/
|
||||
*.dSYM/
|
||||
*.dSYM/
|
||||
|
||||
build/
|
@ -7,7 +7,7 @@
|
||||
#include "../include/include.h"
|
||||
|
||||
/* 随机返回一个数组元素 */
|
||||
int randomAccess(int* nums, int size) {
|
||||
int randomAccess(int *nums, int size) {
|
||||
// 在区间 [0, 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++) {
|
||||
res[i] = nums[i];
|
||||
@ -32,7 +32,7 @@ int* extend(int* nums, int size, int enlarge) {
|
||||
}
|
||||
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
void insert(int* nums, int size, int num, int index) {
|
||||
void insert(int *nums, int size, int num, int index) {
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
for (int i = size - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
@ -42,7 +42,7 @@ void insert(int* nums, int size, int num, int index) {
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
void removeItem(int* nums, int size, int index) {
|
||||
void removeItem(int *nums, int size, int index) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (int i = index; i < size - 1; i++) {
|
||||
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;
|
||||
// 通过索引遍历数组
|
||||
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++) {
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
@ -67,7 +67,6 @@ int find(int* nums, int size, int target) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化数组 */
|
||||
@ -76,21 +75,21 @@ int main() {
|
||||
printf("数组 arr = ");
|
||||
printArray(arr, size);
|
||||
|
||||
int nums[5] = { 1, 3, 2, 5, 4 };
|
||||
int nums[5] = {1, 3, 2, 5, 4};
|
||||
printf("数组 nums = ");
|
||||
printArray(nums, size);
|
||||
|
||||
|
||||
/* 随机访问 */
|
||||
int randomNum = randomAccess(nums, size);
|
||||
printf("在 nums 中获取随机元素 %d", randomNum);
|
||||
|
||||
|
||||
/* 长度扩展 */
|
||||
int enlarge = 3;
|
||||
int* res = extend(nums, size, enlarge);
|
||||
int *res = extend(nums, size, enlarge);
|
||||
size += enlarge;
|
||||
printf("将数组长度扩展至 8 ,得到 nums = ");
|
||||
printArray(res, size);
|
||||
|
||||
|
||||
/* 插入元素 */
|
||||
insert(res, size, 6, 3);
|
||||
printf("在索引 3 处插入数字 6 ,得到 nums = ");
|
||||
@ -100,10 +99,10 @@ int main() {
|
||||
removeItem(res, size, 2);
|
||||
printf("删除索引 2 处的元素,得到 nums = ");
|
||||
printArray(res, size);
|
||||
|
||||
|
||||
/* 遍历数组 */
|
||||
traverse(res, size);
|
||||
|
||||
|
||||
/* 查找元素 */
|
||||
int index = find(res, size, 3);
|
||||
printf("在 res 中查找元素 3 ,得到索引 = %d\n", index);
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include "../include/include.h"
|
||||
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
void insert(ListNode* n0, ListNode* P) {
|
||||
void insert(ListNode *n0, ListNode *P) {
|
||||
ListNode *n1 = n0->next;
|
||||
P->next = n1;
|
||||
n0->next = P;
|
||||
@ -16,7 +16,7 @@ void insert(ListNode* n0, ListNode* P) {
|
||||
/* 删除链表的节点 n0 之后的首个节点 */
|
||||
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
|
||||
// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888
|
||||
void removeNode(ListNode* n0) {
|
||||
void removeNode(ListNode *n0) {
|
||||
if (!n0->next)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
@ -28,7 +28,7 @@ void removeNode(ListNode* n0) {
|
||||
}
|
||||
|
||||
/* 访问链表中索引为 index 的节点 */
|
||||
ListNode* access(ListNode* head, int index) {
|
||||
ListNode *access(ListNode *head, int index) {
|
||||
while (head && head->next && index) {
|
||||
head = head->next;
|
||||
index--;
|
||||
@ -37,7 +37,7 @@ ListNode* access(ListNode* head, int index) {
|
||||
}
|
||||
|
||||
/* 在链表中查找值为 target 的首个节点 */
|
||||
int find(ListNode* head, int target) {
|
||||
int find(ListNode *head, int target) {
|
||||
int index = 0;
|
||||
while (head) {
|
||||
if (head->val == target)
|
||||
@ -48,22 +48,21 @@ int find(ListNode* head, int target) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化链表 */
|
||||
// 初始化各个节点
|
||||
ListNode* n0 = newListNode(1);
|
||||
ListNode* n1 = newListNode(3);
|
||||
ListNode* n2 = newListNode(2);
|
||||
ListNode* n3 = newListNode(5);
|
||||
ListNode* n4 = newListNode(4);
|
||||
// 初始化各个节点
|
||||
ListNode *n0 = newListNode(1);
|
||||
ListNode *n1 = newListNode(3);
|
||||
ListNode *n2 = newListNode(2);
|
||||
ListNode *n3 = newListNode(5);
|
||||
ListNode *n4 = newListNode(4);
|
||||
// 构建引用指向
|
||||
n0->next = n1;
|
||||
n1->next = n2;
|
||||
n2->next = n3;
|
||||
n3->next = n4;
|
||||
printf("初始化的链表为\r\n");
|
||||
printf("初始化的链表为\r\n");
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 插入节点 */
|
||||
@ -77,7 +76,7 @@ int main() {
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 访问节点 */
|
||||
ListNode* node = access(n0, 3);
|
||||
ListNode *node = access(n0, 3);
|
||||
printf("链表中索引 3 处的节点的值 = %d\r\n", node->val);
|
||||
|
||||
/* 查找节点 */
|
||||
|
@ -93,7 +93,7 @@ int removeNum(myList *list, int index) {
|
||||
void extendCapacity(myList *list) {
|
||||
// 先分配空间
|
||||
int newCapacity = capacity(list) * list->extendRatio;
|
||||
int *extend = (int *) malloc(sizeof(int) * newCapacity);
|
||||
int *extend = (int *)malloc(sizeof(int) * newCapacity);
|
||||
int *temp = list->nums;
|
||||
|
||||
// 拷贝旧数据到新数据
|
||||
@ -113,6 +113,7 @@ int *toArray(myList *list) {
|
||||
return list->nums;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化列表 */
|
||||
myList *list = newMyList();
|
||||
@ -157,4 +158,6 @@ int main() {
|
||||
|
||||
/* 释放分配内存 */
|
||||
delMyList(list);
|
||||
|
||||
return 0;
|
||||
}
|
@ -1 +1 @@
|
||||
add_executable(linear_search linear_search.c)
|
||||
add_executable(binary_search binary_search.c)
|
@ -15,9 +15,9 @@ int binarySearch(int *nums, int len, int target) {
|
||||
int mid = (left + right) / 2; // 计算中点索引 mid
|
||||
if (nums[mid] < target) // 此情况说明 target 在区间 [mid+1, right] 中
|
||||
left = mid + 1;
|
||||
else if (nums[mid] > target) // 此情况说明 target 在区间 [left, mid-1] 中
|
||||
else if (nums[mid] > target) // 此情况说明 target 在区间 [left, mid-1] 中
|
||||
right = mid - 1;
|
||||
else // 找到目标元素,返回其索引
|
||||
else // 找到目标元素,返回其索引
|
||||
return mid;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
@ -33,9 +33,9 @@ int binarySearch1(int *nums, int len, int target) {
|
||||
int mid = (left + right) / 2; // 计算中点索引 mid
|
||||
if (nums[mid] < target) // 此情况说明 target 在区间 [mid+1, right) 中
|
||||
left = mid + 1;
|
||||
else if (nums[mid] > target) // 此情况说明 target 在区间 [left, mid) 中
|
||||
else if (nums[mid] > target) // 此情况说明 target 在区间 [left, mid) 中
|
||||
right = mid;
|
||||
else // 找到目标元素,返回其索引
|
||||
else // 找到目标元素,返回其索引
|
||||
return mid;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
@ -45,8 +45,8 @@ int binarySearch1(int *nums, int len, int target) {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
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);
|
||||
printf("目标元素 6 的索引 = %d\n", index);
|
||||
@ -54,6 +54,6 @@ int main() {
|
||||
/* 二分查找(左闭右开) */
|
||||
index = binarySearch1(nums, 10, target);
|
||||
printf("目标元素 6 的索引 = %d\n", index);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ void constant(int n) {
|
||||
const int a = 0;
|
||||
int b = 0;
|
||||
int nums[1000];
|
||||
ListNode* node = newListNode(0);
|
||||
ListNode *node = newListNode(0);
|
||||
free(node);
|
||||
// 循环中的变量占用 O(1) 空间
|
||||
for (int i = 0; i < n; i++) {
|
||||
@ -38,11 +38,11 @@ typedef struct {
|
||||
/* 线性阶 */
|
||||
void linear(int n) {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
int *nums = malloc (sizeof(int) * n);
|
||||
int *nums = malloc(sizeof(int) * n);
|
||||
free(nums);
|
||||
|
||||
// 长度为 n 的列表占用 O(n) 空间
|
||||
ListNode** nodes = malloc(sizeof(ListNode *) * n);
|
||||
ListNode **nodes = malloc(sizeof(ListNode *) * n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes[i] = newListNode(i);
|
||||
}
|
||||
@ -56,7 +56,7 @@ void linear(int n) {
|
||||
hashTable *h = NULL;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
hashTable *tmp = malloc(sizeof (hashTable));
|
||||
hashTable *tmp = malloc(sizeof(hashTable));
|
||||
tmp->key = i;
|
||||
sprintf(tmp->val, "%d", i);
|
||||
HASH_ADD_INT(h, key, tmp);
|
||||
@ -81,7 +81,7 @@ void linearRecur(int n) {
|
||||
/* 平方阶 */
|
||||
void quadratic(int n) {
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
int** numMatrix = malloc(sizeof(int *) * n);
|
||||
int **numMatrix = malloc(sizeof(int *) * n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
int *tmp = malloc(sizeof(int) * n);
|
||||
for (int j = 0; j < n; j++) {
|
||||
@ -100,8 +100,8 @@ void quadratic(int n) {
|
||||
/* 平方阶(递归实现) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
int* nums = malloc(sizeof(int) * n);
|
||||
return 0;
|
||||
int *nums = malloc(sizeof(int) * n);
|
||||
printf("递归 n = %d 中的 nums 长度 = %d\r\n", n, n);
|
||||
free(nums);
|
||||
return quadraticRecur(n - 1);
|
||||
|
@ -12,7 +12,7 @@ int constant(int n) {
|
||||
int size = 100000;
|
||||
int i = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
count ++;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
@ -21,7 +21,7 @@ int constant(int n) {
|
||||
int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
count ++;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
@ -31,19 +31,18 @@ int arrayTraversal(int *nums, int n) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
for (int i = 0; i < n; i++) {
|
||||
count ++;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
int quadratic(int n)
|
||||
{
|
||||
int quadratic(int n) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count ++;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
@ -51,7 +50,7 @@ int quadratic(int n)
|
||||
|
||||
/* 平方阶(冒泡排序) */
|
||||
int bubbleSort(int *nums, int n) {
|
||||
int count = 0; // 计数器
|
||||
int count = 0; // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = n - 1; i > 0; i--) {
|
||||
// 内循环:冒泡操作
|
||||
@ -61,7 +60,7 @@ int bubbleSort(int *nums, int n) {
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -85,7 +84,8 @@ int exponential(int n) {
|
||||
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
@ -101,24 +101,26 @@ int logarithmic(float n) {
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
if (n <= 1)
|
||||
return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 线性对数阶 */
|
||||
int linearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count ++;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 阶乘阶(递归实现) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
@ -145,7 +147,7 @@ int main(int argc, char *argv[]) {
|
||||
count = quadratic(n);
|
||||
printf("平方阶的计算操作数量 = %d\n", count);
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
}
|
||||
count = bubbleSort(nums, n);
|
||||
printf("平方阶(冒泡排序)的计算操作数量 = %d\n", count);
|
||||
@ -172,5 +174,6 @@ int main(int argc, char *argv[]) {
|
||||
nums = NULL;
|
||||
}
|
||||
getchar();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ int *randomNumbers(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
// 随机打乱数组元素
|
||||
// 随机打乱数组元素
|
||||
for (int i = n - 1; i > 0; i--) {
|
||||
int j = rand() % (i + 1);
|
||||
int temp = nums[i];
|
||||
@ -29,7 +29,8 @@ int findOne(int *nums, int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (nums[i] == 1) return i;
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@ -51,5 +52,6 @@ int main(int argc, char *argv[]) {
|
||||
nums = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ void siftUp(maxHeap *h, int i);
|
||||
/* 构造方法,根据切片建堆 */
|
||||
maxHeap *newMaxHeap(int nums[], int size) {
|
||||
// 所有元素入堆
|
||||
maxHeap *h = (maxHeap *) malloc(sizeof(maxHeap));
|
||||
maxHeap *h = (maxHeap *)malloc(sizeof(maxHeap));
|
||||
h->size = size;
|
||||
memcpy(h->data, nums, size * sizeof(int));
|
||||
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];
|
||||
h->data[i] = h->data[j];
|
||||
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) {
|
||||
printf("heap is full!");
|
||||
@ -104,7 +104,6 @@ int pop(maxHeap *h) {
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
/* 从节点 i 开始,从顶至底堆化 */
|
||||
void siftDown(maxHeap *h, int i) {
|
||||
while (true) {
|
||||
@ -145,6 +144,7 @@ void siftUp(maxHeap *h, int i) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化堆 */
|
||||
// 初始化大顶堆
|
||||
@ -174,4 +174,4 @@ int main() {
|
||||
|
||||
// 释放内存
|
||||
free(heap);
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +1 @@
|
||||
add_executable(binary_search binary_search.c)
|
||||
add_executable(leetcode_two_sum leetcode_two_sum.c)
|
@ -8,8 +8,8 @@
|
||||
|
||||
/* 哈希表 */
|
||||
struct hashTable {
|
||||
int key; // key 为 int 型
|
||||
void* val; // val 为 void * 型
|
||||
int key; // key 为 int 型
|
||||
void *val; // val 为 void * 型
|
||||
UT_hash_handle hh; // 借助 uthash 实现的哈希表
|
||||
};
|
||||
|
||||
@ -19,20 +19,20 @@ typedef struct hashTable hashTable;
|
||||
void printHashTableVal(hashTable **ht) {
|
||||
struct hashTable *s;
|
||||
for (s = *ht; s != NULL; s = s->hh.next) {
|
||||
printf("key: %d: val: %d\n", s->key, *(int *)(s->val));
|
||||
printf("key: %d: val: %d\n", s->key, *(int *)(s->val));
|
||||
}
|
||||
}
|
||||
|
||||
/* 打印哈希表(当 hashTable 中 val 为 ListNode* 型使用这个) */
|
||||
void printHashTableLinklist(hashTable **ht) {
|
||||
struct hashTable *s;
|
||||
for (s = *ht; s != NULL; s = s->hh.next) {
|
||||
printf("key: %d: val: %d\n", s->key, ((ListNode *)s->val)->val);
|
||||
}
|
||||
struct hashTable *s;
|
||||
for (s = *ht; s != NULL; s = s->hh.next) {
|
||||
printf("key: %d: val: %d\n", s->key, ((ListNode *)s->val)->val);
|
||||
}
|
||||
}
|
||||
|
||||
/* 哈希表查找 */
|
||||
hashTable* find(hashTable *ht, int k) {
|
||||
hashTable *find(hashTable *ht, int k) {
|
||||
hashTable *s;
|
||||
HASH_FIND_INT(ht, &k, s);
|
||||
return s;
|
||||
@ -43,7 +43,7 @@ void addInt(hashTable **ht, int k, int v) {
|
||||
hashTable *s;
|
||||
HASH_FIND_INT(*ht, &k, s);
|
||||
if (s == NULL) {
|
||||
s = malloc (sizeof(*s));
|
||||
s = malloc(sizeof(*s));
|
||||
s->key = k;
|
||||
HASH_ADD_INT(*ht, key, s);
|
||||
}
|
||||
@ -53,11 +53,11 @@ void addInt(hashTable **ht, int k, int v) {
|
||||
}
|
||||
|
||||
/* 添加 k、v 到哈希表 ht 中,v 为 ListNode* 类型 */
|
||||
void addLinkNode(hashTable **ht, int k, ListNode* v) {
|
||||
void addLinkNode(hashTable **ht, int k, ListNode *v) {
|
||||
hashTable *s;
|
||||
HASH_FIND_INT(*ht, &k, s);
|
||||
if (s == NULL) {
|
||||
s = malloc (sizeof(*s));
|
||||
s = malloc(sizeof(*s));
|
||||
s->key = k;
|
||||
HASH_ADD_INT(*ht, key, s);
|
||||
}
|
||||
@ -76,7 +76,7 @@ void deleteAll(hashTable **ht) {
|
||||
/* 通过 key 访问哈希表,返回 int 类型 */
|
||||
int accessInt(hashTable **h, int key) {
|
||||
hashTable *s;
|
||||
int ret=-1;
|
||||
int ret = -1;
|
||||
if (s = find(*h, key)) {
|
||||
ret = *(int *)s->val;
|
||||
}
|
||||
@ -84,9 +84,9 @@ int accessInt(hashTable **h, int key) {
|
||||
}
|
||||
|
||||
/* 通过 key 访问哈希表,返回 ListNode* 类型 */
|
||||
ListNode* accessLinkNode(hashTable **h, int key) {
|
||||
ListNode *accessLinkNode(hashTable **h, int key) {
|
||||
hashTable *s;
|
||||
ListNode *res=NULL;
|
||||
ListNode *res = NULL;
|
||||
if (s = find(*h, key)) {
|
||||
res = (ListNode *)s->val;
|
||||
}
|
||||
@ -118,19 +118,19 @@ int main() {
|
||||
/* 哈希查找(数组) */
|
||||
int nums[] = {1, 5, 3, 2, 4, 7, 5, 9, 10, 8};
|
||||
// 初始化哈希表
|
||||
hashTable** ht = malloc(sizeof(*ht));
|
||||
hashTable **ht = malloc(sizeof(*ht));
|
||||
*ht = NULL;
|
||||
for (int i = 0; i < sizeof(nums)/sizeof(nums[0]); i++) {
|
||||
addInt(ht, nums[i], i); // key: 元素,value: 索引
|
||||
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
|
||||
addInt(ht, nums[i], i); // key: 元素,value: 索引
|
||||
}
|
||||
int index = hashingSearchArray(ht, target);
|
||||
printf("目标元素 3 的索引 = %d\r\n", index);
|
||||
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;
|
||||
|
||||
while (head != NULL) {
|
||||
|
@ -66,6 +66,7 @@ int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// ======= Test Case =======
|
||||
int nums[] = {2, 7, 11, 15};
|
||||
|
@ -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) {
|
||||
// 找到目标节点,返回之
|
||||
@ -36,14 +36,14 @@ int main() {
|
||||
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);
|
||||
printf("目标元素 3 的索引 = %d\n", index);
|
||||
|
||||
/* 在链表中执行线性查找 */
|
||||
ListNode* head = arrToLinkedList(nums, 10);
|
||||
ListNode* node = linearSearchLinkedList(head, target);
|
||||
if(node == NULL) {
|
||||
ListNode *head = arrToLinkedList(nums, 10);
|
||||
ListNode *node = linearSearchLinkedList(head, target);
|
||||
if (node == NULL) {
|
||||
printf("目标节点值 3 的对应节点对象为 NULL\n");
|
||||
} else {
|
||||
printf("目标节点值 3 的对应节点对象为 addr: %p val: %d\n", node, node->val);
|
||||
|
@ -7,8 +7,7 @@
|
||||
#include "../include/include.h"
|
||||
|
||||
/* 冒泡排序 */
|
||||
void bucketSort(double nums[], int size)
|
||||
{
|
||||
void bucketSort(double nums[], int size) {
|
||||
// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
|
||||
int k = size / 2;
|
||||
// 1. 将数组元素分配到各个桶中
|
||||
@ -23,18 +22,13 @@ void bucketSort(double nums[], int size)
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
// 设输入数据为浮点数,范围为 [0, 1)
|
||||
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);
|
||||
bucketSort(nums, size);
|
||||
|
||||
printf("桶排序完成后 nums = ");
|
||||
printf("[");
|
||||
for (int i = 0; i < size - 1; i++)
|
||||
{
|
||||
printf("%g, ", nums[i]);
|
||||
}
|
||||
printf("]");
|
||||
}
|
||||
printArray(nums, size);
|
||||
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ void countingSort(int nums[], int size) {
|
||||
for (int i = size - 1; i >= 0; i--) {
|
||||
int num = nums[i];
|
||||
res[counter[num] - 1] = num; // 将 num 放置到对应索引处
|
||||
counter[num]--; // 令前缀和自减 1 ,得到下次放置 num 的索引
|
||||
counter[num]--; // 令前缀和自减 1 ,得到下次放置 num 的索引
|
||||
}
|
||||
// 使用结果数组 res 覆盖原数组 nums
|
||||
memcpy(nums, res, size * sizeof(int));
|
||||
|
@ -9,18 +9,16 @@
|
||||
/* 插入排序 */
|
||||
void insertionSort(int nums[], int size) {
|
||||
// 外循环: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;
|
||||
// 内循环:将 base 插入到左边的正确位置
|
||||
while (j >= 0 && nums[j] > base)
|
||||
{
|
||||
while (j >= 0 && nums[j] > base) {
|
||||
// 1. 将 nums[j] 向右移动一位
|
||||
nums[j + 1] = nums[j];
|
||||
nums[j + 1] = nums[j];
|
||||
j--;
|
||||
}
|
||||
// 2. 将 base 赋值到正确位置
|
||||
nums[j + 1] = base;
|
||||
nums[j + 1] = base;
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,8 +27,7 @@ int main() {
|
||||
int nums[] = {4, 1, 3, 1, 5, 2};
|
||||
insertionSort(nums, 6);
|
||||
printf("插入排序完成后 nums = ");
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
for (int i = 0; i < 6; i++) {
|
||||
printf("%d ", nums[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
@ -13,15 +13,15 @@ void merge(int *nums, int left, int mid, int right) {
|
||||
int index;
|
||||
// 初始化辅助数组
|
||||
int tmp[right + 1 - left];
|
||||
for(index = left; index < right + 1; index++) {
|
||||
for (index = left; index < right + 1; index++) {
|
||||
tmp[index - left] = nums[index];
|
||||
}
|
||||
// 左子数组的起始索引和结束索引
|
||||
// 左子数组的起始索引和结束索引
|
||||
int leftStart = left - left, leftEnd = mid - left;
|
||||
// 右子数组的起始索引和结束索引
|
||||
// 右子数组的起始索引和结束索引
|
||||
int rightStart = mid + 1 - left, rightEnd = right - left;
|
||||
// i, j 分别指向左子数组、右子数组的首元素
|
||||
int i = leftStart, j = rightStart;
|
||||
int i = leftStart, j = rightStart;
|
||||
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for (int k = left; k <= right; k++) {
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
@ -39,7 +39,8 @@ void merge(int *nums, int left, int mid, 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; // 计算中点
|
||||
mergeSort(nums, left, mid); // 递归左子数组
|
||||
@ -51,10 +52,11 @@ void mergeSort(int *nums, int left, int right) {
|
||||
/* Driver Code */
|
||||
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);
|
||||
mergeSort(nums, 0, size - 1);
|
||||
printf("归并排序完成后 nums = ");
|
||||
printArray(nums, size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -72,13 +72,13 @@ int quickSortMedianPartition(int nums[], int left, int right) {
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // 从左向右找首个大于基准数的元素
|
||||
swap(nums, i, j); // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
}
|
||||
|
||||
// 中位基准数优化-快速排序
|
||||
@ -93,7 +93,6 @@ void quickSortMedian(int nums[], int left, int right) {
|
||||
quickSortMedian(nums, pivot + 1, right);
|
||||
}
|
||||
|
||||
|
||||
/* 快速排序类(尾递归优化) */
|
||||
/* 尾递归优化-哨兵划分 */
|
||||
int quickSortTailCallPartition(int nums[], int left, int right) {
|
||||
@ -101,16 +100,15 @@ int quickSortTailCallPartition(int nums[], int left, int right) {
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // 从左向右找首个大于基准数的元素
|
||||
swap(nums, i, j); // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
}
|
||||
|
||||
|
||||
// 快速排序(尾递归优化)
|
||||
void quickSortTailCall(int nums[], int left, int right) {
|
||||
// 子数组长度为 1 时终止
|
||||
@ -119,16 +117,15 @@ void quickSortTailCall(int nums[], int left, int right) {
|
||||
int pivot = quickSortTailCallPartition(nums, left, right);
|
||||
// 对两个子数组中较短的那个执行快排
|
||||
if (pivot - left < right - pivot) {
|
||||
quickSortTailCall(nums, left, pivot - 1); // 递归排序左子数组
|
||||
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
|
||||
quickSortTailCall(nums, left, pivot - 1); // 递归排序左子数组
|
||||
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
|
||||
} else {
|
||||
quickSortTailCall(nums, pivot + 1, right); // 递归排序右子数组
|
||||
right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1]
|
||||
right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 快速排序 */
|
||||
|
@ -15,7 +15,7 @@ int digit(int num, int exp) {
|
||||
/* 计数排序(根据 nums 第 k 位排序) */
|
||||
void countingSortDigit(int nums[], int size, int exp) {
|
||||
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
|
||||
int *counter = (int *) malloc((sizeof(int) * 10));
|
||||
int *counter = (int *)malloc((sizeof(int) * 10));
|
||||
// 统计 0~9 各数字的出现次数
|
||||
for (int i = 0; i < size; i++) {
|
||||
// 获取 nums[i] 第 k 位,记为 d
|
||||
@ -28,11 +28,11 @@ void countingSortDigit(int nums[], int size, int exp) {
|
||||
counter[i] += counter[i - 1];
|
||||
}
|
||||
// 倒序遍历,根据桶内统计结果,将各元素填入 res
|
||||
int *res = (int *) malloc(sizeof(int) * size);
|
||||
int *res = (int *)malloc(sizeof(int) * size);
|
||||
for (int i = size - 1; i >= 0; i--) {
|
||||
int d = digit(nums[i], exp);
|
||||
int j = counter[d] - 1; // 获取 d 在数组中的索引 j
|
||||
res[j] = nums[i]; // 将当前元素填入索引 j
|
||||
res[j] = nums[i]; // 将当前元素填入索引 j
|
||||
counter[d]--; // 将 d 的数量减 1
|
||||
}
|
||||
// 使用结果覆盖原数组 nums
|
||||
@ -59,12 +59,13 @@ void radixSort(int nums[], int size) {
|
||||
countingSortDigit(nums, size, exp);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// 基数排序
|
||||
int nums[] = {10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
int nums[] = {10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
88906420, 72429244, 30524779, 82060337, 63832996};
|
||||
int size = sizeof(nums) / sizeof(int);
|
||||
radixSort(nums, size);
|
||||
printf("基数排序完成后 nums = ");
|
||||
printArray(nums, size);
|
||||
}
|
||||
}
|
||||
|
@ -18,10 +18,10 @@ typedef struct ArrayDeque ArrayDeque;
|
||||
|
||||
/* 构造方法 */
|
||||
ArrayDeque *newArrayDeque(int capacity) {
|
||||
ArrayDeque *deque = (ArrayDeque *) malloc(sizeof(ArrayDeque));
|
||||
ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));
|
||||
// 初始化数组
|
||||
deque->queCapacity = capacity;
|
||||
deque->nums = (int *) malloc(sizeof(int) * deque->queCapacity);
|
||||
deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);
|
||||
deque->front = deque->queSize = 0;
|
||||
return deque;
|
||||
}
|
||||
@ -85,7 +85,7 @@ void pushLast(ArrayDeque *deque, int num) {
|
||||
int peekFirst(ArrayDeque *deque) {
|
||||
// 访问异常:双向队列为空
|
||||
assert(empty(deque) == 0);
|
||||
return deque->nums[deque->front];
|
||||
return deque->nums[deque->front];
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
@ -93,7 +93,7 @@ int peekLast(ArrayDeque *deque) {
|
||||
// 访问异常:双向队列为空
|
||||
assert(empty(deque) == 0);
|
||||
int last = dequeIndex(deque, deque->front + deque->queSize - 1);
|
||||
return deque->nums[last];
|
||||
return deque->nums[last];
|
||||
}
|
||||
|
||||
/* 队首出队 */
|
||||
@ -122,7 +122,6 @@ void printArrayDeque(ArrayDeque *deque) {
|
||||
printArray(arr, deque->queSize);
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化队列 */
|
||||
@ -166,6 +165,6 @@ int main() {
|
||||
|
||||
// 释放内存
|
||||
delArrayDeque(deque);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -18,10 +18,10 @@ typedef struct ArrayQueue ArrayQueue;
|
||||
|
||||
/* 构造方法 */
|
||||
ArrayQueue *newArrayQueue(int capacity) {
|
||||
ArrayQueue *queue = (ArrayQueue *) malloc(sizeof(ArrayQueue));
|
||||
ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));
|
||||
// 初始化数组
|
||||
queue->queCapacity = capacity;
|
||||
queue->nums = (int *) malloc(sizeof(int) * queue->queCapacity);
|
||||
queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);
|
||||
queue->front = queue->queSize = 0;
|
||||
return queue;
|
||||
}
|
||||
@ -49,8 +49,8 @@ bool empty(ArrayQueue *queue) {
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peek(ArrayQueue *queue) {
|
||||
assert(size(queue) != 0);
|
||||
return queue->nums[queue->front];
|
||||
assert(size(queue) != 0);
|
||||
return queue->nums[queue->front];
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
@ -85,7 +85,6 @@ void printArrayQueue(ArrayQueue *queue) {
|
||||
printArray(arr, queue->queSize);
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化队列 */
|
||||
@ -129,4 +128,4 @@ int main() {
|
||||
// 释放内存
|
||||
delArrayQueue(queue);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ int pop(arrayStack *s) {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化栈 */
|
||||
arrayStack * stack = newArrayStack();
|
||||
arrayStack *stack = newArrayStack();
|
||||
|
||||
/* 元素入栈 */
|
||||
push(stack, 1);
|
||||
@ -100,4 +100,4 @@ int main() {
|
||||
free(stack);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ typedef struct DoublyListNode DoublyListNode;
|
||||
|
||||
/* 双向链表节点构造方法 */
|
||||
DoublyListNode *newDoublyListNode(int num) {
|
||||
DoublyListNode* new = (DoublyListNode *) malloc(sizeof(DoublyListNode));
|
||||
DoublyListNode *new = (DoublyListNode *)malloc(sizeof(DoublyListNode));
|
||||
new->val = num;
|
||||
new->next = NULL;
|
||||
new->prev = NULL;
|
||||
@ -39,7 +39,7 @@ typedef struct LinkedListDeque LinkedListDeque;
|
||||
|
||||
/* 构造方法 */
|
||||
LinkedListDeque *newLinkedListDeque() {
|
||||
LinkedListDeque *deque = (LinkedListDeque *) malloc(sizeof(LinkedListDeque));
|
||||
LinkedListDeque *deque = (LinkedListDeque *)malloc(sizeof(LinkedListDeque));
|
||||
deque->front = NULL;
|
||||
deque->rear = NULL;
|
||||
deque->queSize = 0;
|
||||
@ -48,7 +48,7 @@ LinkedListDeque *newLinkedListDeque() {
|
||||
/* 析构方法 */
|
||||
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;
|
||||
deque->front = deque->front->next;
|
||||
free(tmp);
|
||||
@ -75,11 +75,11 @@ void push(LinkedListDeque *deque, int num, bool isFront) {
|
||||
deque->front = deque->rear = node;
|
||||
}
|
||||
// 队首入队操作
|
||||
else if (isFront){
|
||||
else if (isFront) {
|
||||
// 将 node 添加至链表头部
|
||||
deque->front->prev = node;
|
||||
node->next = deque->front;
|
||||
deque->front = node;// 更新头节点
|
||||
deque->front = node; // 更新头节点
|
||||
}
|
||||
// 对尾入队操作
|
||||
else {
|
||||
@ -119,7 +119,7 @@ int pop(LinkedListDeque *deque, bool isFront) {
|
||||
return -1;
|
||||
int val;
|
||||
// 队首出队操作
|
||||
if(isFront) {
|
||||
if (isFront) {
|
||||
val = peekFirst(deque); // 暂存头节点值
|
||||
DoublyListNode *fNext = deque->front->next;
|
||||
if (fNext) {
|
||||
@ -140,7 +140,7 @@ int pop(LinkedListDeque *deque, bool isFront) {
|
||||
}
|
||||
deque->rear = rPrev; // 更新尾节点
|
||||
}
|
||||
deque->queSize--; // 更新队列长度
|
||||
deque->queSize--; // 更新队列长度
|
||||
return val;
|
||||
}
|
||||
|
||||
@ -160,9 +160,9 @@ void printLinkedListDeque(LinkedListDeque *deque) {
|
||||
// 拷贝链表中的数据到数组
|
||||
int i;
|
||||
DoublyListNode *node;
|
||||
for (i=0, node = deque->front; i < deque->queSize; i++){
|
||||
arr[i] = node->val;
|
||||
node = node->next;
|
||||
for (i = 0, node = deque->front; i < deque->queSize; i++) {
|
||||
arr[i] = node->val;
|
||||
node = node->next;
|
||||
}
|
||||
printArray(arr, deque->queSize);
|
||||
}
|
||||
@ -211,4 +211,3 @@ int main() {
|
||||
delLinkedListdeque(deque);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@ typedef struct LinkedListQueue LinkedListQueue;
|
||||
|
||||
/* 构造方法 */
|
||||
LinkedListQueue *newLinkedListQueue() {
|
||||
LinkedListQueue *queue = (LinkedListQueue *) malloc(sizeof(LinkedListQueue));
|
||||
LinkedListQueue *queue = (LinkedListQueue *)malloc(sizeof(LinkedListQueue));
|
||||
queue->front = NULL;
|
||||
queue->rear = NULL;
|
||||
queue->queSize = 0;
|
||||
@ -25,7 +25,7 @@ LinkedListQueue *newLinkedListQueue() {
|
||||
/* 析构方法 */
|
||||
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;
|
||||
queue->front = queue->front->next;
|
||||
free(tmp);
|
||||
@ -46,13 +46,13 @@ bool empty(LinkedListQueue *queue) {
|
||||
|
||||
/* 入队 */
|
||||
void push(LinkedListQueue *queue, int num) {
|
||||
// 尾节点处添加 node
|
||||
// 尾节点处添加 node
|
||||
ListNode *node = newListNode(num);
|
||||
// 如果队列为空,则令头、尾节点都指向该节点
|
||||
if (queue->front == NULL) {
|
||||
queue->front = node;
|
||||
queue->rear = node;
|
||||
}
|
||||
}
|
||||
// 如果队列不为空,则将该节点添加到尾节点后
|
||||
else {
|
||||
queue->rear->next = node;
|
||||
@ -82,7 +82,7 @@ void printLinkedListQueue(LinkedListQueue *queue) {
|
||||
// 拷贝链表中的数据到数组
|
||||
int i;
|
||||
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;
|
||||
node = node->next;
|
||||
}
|
||||
@ -125,4 +125,3 @@ int main() {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
||||
/* 基于链表实现的栈 */
|
||||
struct linkedListStack {
|
||||
ListNode *top; // 将头节点作为栈顶
|
||||
int size; // 栈的长度
|
||||
int size; // 栈的长度
|
||||
};
|
||||
|
||||
typedef struct linkedListStack linkedListStack;
|
||||
@ -54,11 +54,11 @@ int peek(linkedListStack *s) {
|
||||
/* 入栈 */
|
||||
void push(linkedListStack *s, int num) {
|
||||
assert(s);
|
||||
ListNode *node = (ListNode *) malloc(sizeof(ListNode));
|
||||
node->next = s->top; // 更新新加节点指针域
|
||||
node->val = num; // 更新新加节点数据域
|
||||
s->top = node; // 更新栈顶
|
||||
s->size++; // 更新栈大小
|
||||
ListNode *node = (ListNode *)malloc(sizeof(ListNode));
|
||||
node->next = s->top; // 更新新加节点指针域
|
||||
node->val = num; // 更新新加节点数据域
|
||||
s->top = node; // 更新栈顶
|
||||
s->size++; // 更新栈大小
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
|
@ -15,7 +15,7 @@ typedef struct binarySearchTree binarySearchTree;
|
||||
|
||||
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 *bst = (binarySearchTree *) malloc(sizeof(binarySearchTree));
|
||||
binarySearchTree *bst = (binarySearchTree *)malloc(sizeof(binarySearchTree));
|
||||
TreeNode *root;
|
||||
// 从小到大排序数组
|
||||
qsort(nums, size, sizeof(int), sortIntHelper);
|
||||
@ -108,7 +108,8 @@ void removeNode(binarySearchTree *bst, int num) {
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != NULL) {
|
||||
// 找到待删除节点,跳出循环
|
||||
if (cur->val == num) break;
|
||||
if (cur->val == num)
|
||||
break;
|
||||
pre = cur;
|
||||
if (cur->val < num) {
|
||||
// 待删除节点在 root 的右子树中
|
||||
@ -159,13 +160,11 @@ int main() {
|
||||
TreeNode *node = search(bst, 7);
|
||||
printf("查找到的节点对象的节点值 = %d\n", node->val);
|
||||
|
||||
|
||||
/* 插入节点 */
|
||||
insert(bst, 16);
|
||||
printf("插入节点 16 后,二叉树为\n");
|
||||
printTree(getRoot(bst));
|
||||
|
||||
|
||||
/* 删除节点 */
|
||||
removeNode(bst, 1);
|
||||
printf("删除节点 1 后,二叉树为\n");
|
||||
|
@ -10,11 +10,11 @@
|
||||
int main() {
|
||||
/* 初始化二叉树 */
|
||||
// 初始化节点
|
||||
TreeNode* n1 = newTreeNode(1);
|
||||
TreeNode* n2 = newTreeNode(2);
|
||||
TreeNode* n3 = newTreeNode(3);
|
||||
TreeNode* n4 = newTreeNode(4);
|
||||
TreeNode* n5 = newTreeNode(5);
|
||||
TreeNode *n1 = newTreeNode(1);
|
||||
TreeNode *n2 = newTreeNode(2);
|
||||
TreeNode *n3 = newTreeNode(3);
|
||||
TreeNode *n4 = newTreeNode(4);
|
||||
TreeNode *n5 = newTreeNode(5);
|
||||
// 构建引用指向(即指针)
|
||||
n1->left = n2;
|
||||
n1->right = n3;
|
||||
@ -24,7 +24,7 @@ int main() {
|
||||
printTree(n1);
|
||||
|
||||
/* 插入与删除节点 */
|
||||
TreeNode* P = newTreeNode(0);
|
||||
TreeNode *P = newTreeNode(0);
|
||||
// 在 n1 -> n2 中间插入节点 P
|
||||
n1->left = P;
|
||||
P->left = n2;
|
||||
|
@ -15,14 +15,14 @@ int *levelOrder(TreeNode *root, int *size) {
|
||||
TreeNode **queue;
|
||||
|
||||
/* 辅助队列 */
|
||||
queue = (TreeNode **) malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
|
||||
queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
|
||||
// 队列指针
|
||||
front = 0, rear = 0;
|
||||
// 加入根节点
|
||||
queue[rear++] = root;
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
/* 辅助数组 */
|
||||
arr = (int *) malloc(sizeof(int) * MAX_NODE_SIZE);
|
||||
arr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);
|
||||
// 数组指针
|
||||
index = 0;
|
||||
while (front < rear) {
|
||||
@ -45,7 +45,6 @@ int *levelOrder(TreeNode *root, int *size) {
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化二叉树 */
|
||||
@ -63,4 +62,4 @@ int main() {
|
||||
printArray(arr, size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,8 @@ int *arr;
|
||||
|
||||
/* 前序遍历 */
|
||||
void preOrder(TreeNode *root, int *size) {
|
||||
if (root == NULL) return;
|
||||
if (root == NULL)
|
||||
return;
|
||||
// 访问优先级:根节点 -> 左子树 -> 右子树
|
||||
arr[(*size)++] = root->val;
|
||||
preOrder(root->left, size);
|
||||
@ -20,7 +21,8 @@ void preOrder(TreeNode *root, int *size) {
|
||||
|
||||
/* 中序遍历 */
|
||||
void inOrder(TreeNode *root, int *size) {
|
||||
if (root == NULL) return;
|
||||
if (root == NULL)
|
||||
return;
|
||||
// 访问优先级:左子树 -> 根节点 -> 右子树
|
||||
inOrder(root->left, size);
|
||||
arr[(*size)++] = root->val;
|
||||
@ -29,14 +31,14 @@ void inOrder(TreeNode *root, int *size) {
|
||||
|
||||
/* 后序遍历 */
|
||||
void postOrder(TreeNode *root, int *size) {
|
||||
if (root == NULL) return;
|
||||
if (root == NULL)
|
||||
return;
|
||||
// 访问优先级:左子树 -> 右子树 -> 根节点
|
||||
postOrder(root->left, size);
|
||||
postOrder(root->right, size);
|
||||
arr[(*size)++] = root->val;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
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;
|
||||
preOrder(root, &size);
|
||||
printf("前序遍历的节点打印序列 = ");
|
||||
|
@ -7,16 +7,16 @@
|
||||
#ifndef C_INCLUDE_H
|
||||
#define C_INCLUDE_H
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "list_node.h"
|
||||
#include "tree_node.h"
|
||||
#include "print_util.h"
|
||||
#include "tree_node.h"
|
||||
|
||||
// hash table lib
|
||||
#include "uthash.h"
|
||||
|
@ -22,7 +22,7 @@ typedef struct ListNode ListNode;
|
||||
/* 构造函数,初始化一个新节点 */
|
||||
ListNode *newListNode(int val) {
|
||||
ListNode *node, *next;
|
||||
node = (ListNode *) malloc(sizeof(ListNode));
|
||||
node = (ListNode *)malloc(sizeof(ListNode));
|
||||
node->val = val;
|
||||
node->next = NULL;
|
||||
return node;
|
||||
|
@ -8,8 +8,8 @@
|
||||
#define PRINT_UTIL_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "list_node.h"
|
||||
#include "tree_node.h"
|
||||
@ -18,7 +18,6 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @brief Print an Array
|
||||
*
|
||||
@ -69,9 +68,9 @@ struct Trunk {
|
||||
typedef struct Trunk Trunk;
|
||||
|
||||
Trunk *newTrunk(Trunk *prev, char *str) {
|
||||
Trunk *trunk = (Trunk *) malloc(sizeof(Trunk));
|
||||
Trunk *trunk = (Trunk *)malloc(sizeof(Trunk));
|
||||
trunk->prev = prev;
|
||||
trunk->str = (char *) malloc(sizeof(char) * 10);
|
||||
trunk->str = (char *)malloc(sizeof(char) * 10);
|
||||
strcpy(trunk->str, str);
|
||||
return trunk;
|
||||
}
|
||||
@ -146,7 +145,6 @@ static void printHeap(int arr[], int size) {
|
||||
printTree(root);
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -4,7 +4,6 @@
|
||||
* Author: Reanon (793584285@qq.com)
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TREE_NODE_H
|
||||
#define TREE_NODE_H
|
||||
|
||||
@ -24,11 +23,10 @@ struct TreeNode {
|
||||
|
||||
typedef struct TreeNode TreeNode;
|
||||
|
||||
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *) malloc(sizeof(TreeNode));
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
@ -55,7 +53,7 @@ TreeNode *arrToTree(const int *arr, size_t size) {
|
||||
/* 根节点 */
|
||||
root = newTreeNode(arr[0]);
|
||||
/* 辅助队列 */
|
||||
queue = (TreeNode **) malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
|
||||
queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
|
||||
// 队列指针
|
||||
front = 0, rear = 0;
|
||||
// 将根节点放入队尾
|
||||
@ -83,7 +81,6 @@ TreeNode *arrToTree(const int *arr, size_t size) {
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Generate a binary tree with an array
|
||||
*
|
||||
@ -100,13 +97,13 @@ int *treeToArr(TreeNode *root) {
|
||||
TreeNode *node;
|
||||
TreeNode **queue;
|
||||
/* 辅助队列 */
|
||||
queue = (TreeNode **) malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
|
||||
queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
|
||||
// 队列指针
|
||||
front = 0, rear = 0;
|
||||
// 将根节点放入队尾
|
||||
queue[rear++] = root;
|
||||
/* 辅助数组 */
|
||||
arr = (int *) malloc(sizeof(int) * MAX_NODE_SIZE);
|
||||
arr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);
|
||||
// 数组指针
|
||||
index = 0;
|
||||
while (front < rear) {
|
||||
|
@ -53,6 +53,7 @@ void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<ve
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
|
||||
cout << "\n初始化二叉树" << endl;
|
||||
|
@ -27,6 +27,7 @@ static void preOrder(TreeNode *root) {
|
||||
path.pop_back();
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
|
||||
cout << "\n初始化二叉树" << endl;
|
||||
|
@ -21,6 +21,7 @@ static void preOrder(TreeNode *root) {
|
||||
preOrder(root->right);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
|
||||
cout << "\n初始化二叉树" << endl;
|
||||
|
@ -26,6 +26,7 @@ static void preOrder(TreeNode *root) {
|
||||
path.pop_back();
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
|
||||
cout << "\n初始化二叉树" << endl;
|
||||
|
@ -19,6 +19,7 @@ void testPop(priority_queue<int> &heap) {
|
||||
printHeap(heap);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化堆 */
|
||||
// 初始化小顶堆
|
||||
|
@ -34,6 +34,7 @@ vector<int> twoSumHashTable(vector<int> &nums, int target) {
|
||||
return {};
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// ======= Test Case =======
|
||||
vector<int> nums = {2, 7, 11, 15};
|
||||
|
@ -4,6 +4,7 @@
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化列表 */
|
||||
List<int> list = [1,3,2,5,4];
|
||||
|
@ -121,6 +121,7 @@ int factorialRecur(int n) {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
||||
int n = 8;
|
||||
|
@ -27,6 +27,7 @@ int findOne(List<int> nums) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
for (var i = 0; i < 10; i++) {
|
||||
int n = 100;
|
||||
|
@ -1,4 +1,4 @@
|
||||
/*
|
||||
/**
|
||||
* File: worst_best_time_complexity.js
|
||||
* Created Time: 2023-01-05
|
||||
* Author: RiverTwilight (contact@rene.wang)
|
||||
|
@ -1,8 +1,8 @@
|
||||
/*
|
||||
* File: binary_search.ts
|
||||
* Created Time: 2022-12-27
|
||||
* Author: Daniel (better.sunjian@gmail.com)
|
||||
*/
|
||||
/**
|
||||
* File: binary_search.ts
|
||||
* Created Time: 2022-12-27
|
||||
* Author: Daniel (better.sunjian@gmail.com)
|
||||
*/
|
||||
|
||||
/* 二分查找(双闭区间) */
|
||||
function binarySearch(nums: number[], target: number): number {
|
||||
|
@ -1,4 +1,4 @@
|
||||
/*
|
||||
/**
|
||||
* File: worst_best_time_complexity.ts
|
||||
* Created Time: 2023-01-05
|
||||
* Author: RiverTwilight (contact@rene.wang)
|
||||
|
Loading…
x
Reference in New Issue
Block a user