Publish the C codes to the docs. (#469)

This commit is contained in:
Yudong Jin
2023-04-18 20:21:31 +08:00
committed by GitHub
parent 6723cdbc7e
commit dbc4906582
29 changed files with 288 additions and 189 deletions

View File

@ -95,10 +95,9 @@
struct ListNode *next; // 指向下一节点的指针(引用)
};
// typedef 作用是为一种数据类型定义一个新名字
typedef struct ListNode ListNode;
/* 构造函数,初始化一个新节点 */
/* 构造函数 */
ListNode *newListNode(int val) {
ListNode *node, *next;
node = (ListNode *) malloc(sizeof(ListNode));
@ -379,7 +378,7 @@
=== "C"
```c title="linked_list.c"
[class]{}-[func]{insertNode}
[class]{}-[func]{insert}
```
=== "C#"
@ -573,7 +572,7 @@
=== "C"
```c title="linked_list.c"
[class]{}-[func]{findNode}
[class]{}-[func]{find}
```
=== "C#"
@ -692,7 +691,24 @@
=== "C"
```c title=""
/* 双向链表节点结构体 */
struct ListNode {
int val; // 节点值
struct ListNode *next; // 指向后继节点的指针(引用)
struct ListNode *prev; // 指向前驱节点的指针(引用)
};
typedef struct ListNode ListNode;
/* 构造函数 */
ListNode *newListNode(int val) {
ListNode *node, *next;
node = (ListNode *) malloc(sizeof(ListNode));
node->val = val;
node->next = NULL;
node->prev = NULL;
return node;
}
```
=== "C#"

View File

@ -73,7 +73,7 @@
=== "C"
```c title="list.c"
// C 未提供内置动态数组
```
=== "C#"
@ -171,7 +171,7 @@
=== "C"
```c title="list.c"
// C 未提供内置动态数组
```
=== "C#"
@ -329,7 +329,7 @@
=== "C"
```c title="list.c"
// C 未提供内置动态数组
```
=== "C#"
@ -491,7 +491,7 @@
=== "C"
```c title="list.c"
// C 未提供内置动态数组
```
=== "C#"
@ -599,7 +599,7 @@
=== "C"
```c title="list.c"
// C 未提供内置动态数组
```
=== "C#"
@ -675,7 +675,7 @@
=== "C"
```c title="list.c"
// C 未提供内置动态数组
```
=== "C#"

View File

@ -157,7 +157,10 @@ $$
=== "C"
```c title=""
// (i + j) 有可能超出 int 的取值范围
int m = (i + j) / 2;
// 更换为此写法则不会越界
int m = i + (j - i) / 2;
```
=== "C#"

View File

@ -182,7 +182,18 @@
=== "C"
```c title=""
/* 函数 */
int func() {
// do something...
return 0;
}
int algorithm(int n) { // 输入数据
const int a = 0; // 暂存数据(常量)
int b = 0; // 暂存数据(变量)
int c = func(); // 栈帧空间(调用函数)
return a + b + c; // 输出数据
}
```
=== "C#"
@ -329,7 +340,12 @@
=== "C"
```c title=""
void algorithm(int n) {
int a = 0; // O(1)
int b[10000]; // O(1)
if (n > 10)
vector<int> nums(n); // O(n)
}
```
=== "C#"
@ -491,7 +507,21 @@
=== "C"
```c title=""
int func() {
// do something
return 0;
}
/* 循环 O(1) */
void loop(int n) {
for (int i = 0; i < n; i++) {
func();
}
}
/* 递归 O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
}
```
=== "C#"
@ -611,7 +641,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceConstant}
[class]{}-[func]{constant}
```
=== "C#"
@ -675,7 +705,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceLinear}
[class]{}-[func]{linear}
```
=== "C#"
@ -737,7 +767,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceLinearRecur}
[class]{}-[func]{linearRecur}
```
=== "C#"
@ -803,7 +833,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceQuadratic}
[class]{}-[func]{quadratic}
```
=== "C#"
@ -865,7 +895,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceQuadraticRecur}
[class]{}-[func]{quadraticRecur}
```
=== "C#"

View File

@ -168,7 +168,7 @@
=== "C"
```c title="hash_map.c"
// C 未提供内置哈希表
```
=== "C#"
@ -333,7 +333,7 @@
=== "C"
```c title="hash_map.c"
// C 未提供内置哈希表
```
=== "C#"

View File

@ -249,7 +249,7 @@
=== "C"
```c title="heap.c"
// C 未提供内置 Heap 类
```
=== "C#"

View File

@ -102,7 +102,9 @@
=== "C"
```c title="merge_sort.c"
[class]{}-[func]{merge}
[class]{}-[func]{mergeSort}
```
=== "C#"

View File

@ -88,7 +88,7 @@
=== "C"
```c title="quick_sort.c"
[class]{quickSort}-[func]{partition}
[class]{}-[func]{partition}
```
=== "C#"
@ -162,7 +162,7 @@
=== "C"
```c title="quick_sort.c"
[class]{quickSort}-[func]{quickSort}
[class]{}-[func]{quickSort}
```
=== "C#"
@ -262,9 +262,9 @@
=== "C"
```c title="quick_sort.c"
[class]{quickSortMedian}-[func]{medianThree}
[class]{}-[func]{medianThree}
[class]{quickSortMedian}-[func]{partition}
[class]{}-[func]{partitionMedian}
```
=== "C#"
@ -336,7 +336,7 @@
=== "C"
```c title="quick_sort.c"
[class]{quickSortTailCall}-[func]{quickSort}
[class]{}-[func]{quickSortTailCall}
```
=== "C#"

View File

@ -215,7 +215,7 @@
=== "C"
```c title="deque.c"
// C 未提供内置双向队列
```
=== "C#"
@ -361,9 +361,9 @@
=== "C"
```c title="linkedlist_deque.c"
[class]{ListNode}-[func]{}
[class]{doublyListNode}-[func]{}
[class]{LinkedListDeque}-[func]{}
[class]{linkedListDeque}-[func]{}
```
=== "C#"
@ -450,7 +450,7 @@
=== "C"
```c title="array_deque.c"
[class]{ArrayDeque}-[func]{}
[class]{arrayDeque}-[func]{}
```
=== "C#"

View File

@ -189,7 +189,7 @@
=== "C"
```c title="queue.c"
// C 未提供内置队列
```
=== "C#"

View File

@ -188,7 +188,7 @@
=== "C"
```c title="stack.c"
// C 未提供内置栈
```
=== "C#"

View File

@ -34,9 +34,8 @@
```cpp title=""
/* 二叉树的数组表示 */
// 为了符合数据类型为 int 使用 int 最大值标记空位
// 该方法的使用前提是没有节点的值 = INT_MAX
vector<int> tree = { 1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15 };
// 使用 int 最大值标记空位,因此要求节点值不能为 INT_MAX
vector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
```
=== "Python"
@ -74,7 +73,9 @@
=== "C"
```c title=""
/* 二叉树的数组表示 */
// 使用 int 最大值标记空位,因此要求节点值不能为 INT_MAX
int tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
```
=== "C#"

View File

@ -62,7 +62,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "Go"
```go title=""
/* AVL 树节点 */
/* AVL 树节点结构体 */
type TreeNode struct {
Val int // 节点值
Height int // 节点高度
@ -74,6 +74,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "JavaScript"
```javascript title=""
/* AVL 树节点类 */
class TreeNode {
val; // 节点值
height; //节点高度
@ -91,6 +92,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "TypeScript"
```typescript title=""
/* AVL 树节点类 */
class TreeNode {
val: number; // 节点值
height: number; // 节点高度
@ -108,7 +110,27 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "C"
```c title=""
/* AVL 树节点结构体 */
struct TreeNode {
int val;
int height;
struct TreeNode *left;
struct TreeNode *right;
};
typedef struct TreeNode TreeNode;
/* 构造函数 */
TreeNode *newTreeNode(int val) {
TreeNode *node;
node = (TreeNode *)malloc(sizeof(TreeNode));
node->val = val;
node->height = 0;
node->left = NULL;
node->right = NULL;
return node;
}
```
=== "C#"
@ -200,9 +222,9 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "C"
```c title="avl_tree.c"
[class]{aVLTree}-[func]{height}
[class]{}-[func]{height}
[class]{aVLTree}-[func]{updateHeight}
[class]{}-[func]{updateHeight}
```
=== "C#"
@ -272,7 +294,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "C"
```c title="avl_tree.c"
[class]{aVLTree}-[func]{balanceFactor}
[class]{}-[func]{balanceFactor}
```
=== "C#"
@ -364,7 +386,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
=== "C"
```c title="avl_tree.c"
[class]{aVLTree}-[func]{rightRotate}
[class]{}-[func]{rightRotate}
```
=== "C#"
@ -436,7 +458,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
=== "C"
```c title="avl_tree.c"
[class]{aVLTree}-[func]{leftRotate}
[class]{}-[func]{leftRotate}
```
=== "C#"
@ -529,7 +551,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
=== "C"
```c title="avl_tree.c"
[class]{aVLTree}-[func]{rotate}
[class]{}-[func]{rotate}
```
=== "C#"
@ -609,7 +631,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
```c title="avl_tree.c"
[class]{aVLTree}-[func]{insert}
[class]{aVLTree}-[func]{insertHelper}
[class]{}-[func]{insertHelper}
```
=== "C#"
@ -691,9 +713,9 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
=== "C"
```c title="avl_tree.c"
[class]{aVLTree}-[func]{remove}
[class]{aVLTree}-[func]{removeNode}
[class]{aVLTree}-[func]{removeHelper}
[class]{}-[func]{removeHelper}
```
=== "C#"

View File

@ -237,7 +237,7 @@
=== "C"
```c title="binary_search_tree.c"
[class]{binarySearchTree}-[func]{remove}
[class]{binarySearchTree}-[func]{removeNode}
```
=== "C#"

View File

@ -87,7 +87,27 @@
=== "C"
```c title=""
/* 二叉树节点结构体 */
struct TreeNode {
int val; // 节点值
int height; // 节点高度
struct TreeNode *left; // 左子节点指针
struct TreeNode *right; // 右子节点指针
};
typedef struct TreeNode TreeNode;
/* 构造函数 */
TreeNode *newTreeNode(int val) {
TreeNode *node;
node = (TreeNode *)malloc(sizeof(TreeNode));
node->val = val;
node->height = 0;
node->left = NULL;
node->right = NULL;
return node;
}
```
=== "C#"
@ -256,7 +276,18 @@
=== "C"
```c title="binary_tree.c"
/* 初始化二叉树 */
// 初始化节点
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;
n2->left = n4;
n2->right = n5;
```
=== "C#"
@ -376,7 +407,13 @@
=== "C"
```c title="binary_tree.c"
/* 插入与删除节点 */
TreeNode *P = newTreeNode(0);
// 在 n1 -> n2 中间插入节点 P
n1->left = P;
P->left = n2;
// 删除节点 P
n1->left = n2;
```
=== "C#"