Add ru version (#1865)

* Add Russian docs site baseline

* Add Russian localized codebase

* Polish Russian code wording

* Update ru code translation.

* Update code translation and chapter covers.

* Fix pythontutor extraction.

* Add README and landing page.

* placeholder of profiles

* Use figures of English version

* Remove chapter paperbook
This commit is contained in:
Yudong Jin
2026-03-28 04:24:07 +08:00
committed by GitHub
parent 2ca570cc33
commit 772183705e
1958 changed files with 108186 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
add_executable(utils
common_test.c
common.h print_util.h
list_node.h tree_node.h
uthash.h)
+36
View File
@@ -0,0 +1,36 @@
/**
* File: common.h
* Created Time: 2022-12-20
* Author: MolDuM (moldum@163.com)、Reanon (793584285@qq.com)
*/
#ifndef COMMON_H
#define COMMON_H
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include "list_node.h"
#include "print_util.h"
#include "tree_node.h"
#include "vertex.h"
// hash table lib
#include "uthash.h"
#include "vector.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif // COMMON_H
+35
View File
@@ -0,0 +1,35 @@
/**
* File: include_test.c
* Created Time: 2023-01-10
* Author: Reanon (793584285@qq.com)
*/
#include "common.h"
void testListNode() {
int nums[] = {2, 3, 5, 6, 7};
int size = sizeof(nums) / sizeof(int);
ListNode *head = arrToLinkedList(nums, size);
printLinkedList(head);
}
void testTreeNode() {
int nums[] = {1, 2, 3, INT_MAX, 5, 6, INT_MAX};
int size = sizeof(nums) / sizeof(int);
TreeNode *root = arrayToTree(nums, size);
// print tree
printTree(root);
// tree to arr
int *arr = treeToArray(root, &size);
printArray(arr, size);
}
int main(int argc, char *argv[]) {
printf("==testListNode==\n");
testListNode();
printf("==testTreeNode==\n");
testTreeNode();
return 0;
}
+59
View File
@@ -0,0 +1,59 @@
/**
* File: list_node.h
* Created Time: 2023-01-09
* Author: Reanon (793584285@qq.com)
*/
#ifndef LIST_NODE_H
#define LIST_NODE_H
#ifdef __cplusplus
extern "C" {
#endif
/* Структура узла связного списка */
typedef struct ListNode {
int val; // Значение узла
struct ListNode *next; // Ссылка на следующий узел
} ListNode;
/* Конструктор, инициализирующий новый узел */
ListNode *newListNode(int val) {
ListNode *node;
node = (ListNode *)malloc(sizeof(ListNode));
node->val = val;
node->next = NULL;
return node;
}
/* Десериализовать массив в связный список */
ListNode *arrToLinkedList(const int *arr, size_t size) {
if (size <= 0) {
return NULL;
}
ListNode *dummy = newListNode(0);
ListNode *node = dummy;
for (int i = 0; i < size; i++) {
node->next = newListNode(arr[i]);
node = node->next;
}
return dummy->next;
}
/* Освободить память, выделенную под связный список */
void freeMemoryLinkedList(ListNode *cur) {
// Освободить память
ListNode *pre;
while (cur != NULL) {
pre = cur;
cur = cur->next;
free(pre);
}
}
#ifdef __cplusplus
}
#endif
#endif // LIST_NODE_H
+131
View File
@@ -0,0 +1,131 @@
/**
* File: print_util.h
* Created Time: 2022-12-21
* Author: MolDum (moldum@163.com), Reanon (793584285@qq.com)
*/
#ifndef PRINT_UTIL_H
#define PRINT_UTIL_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list_node.h"
#include "tree_node.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Вывести массив */
void printArray(int arr[], int size) {
if (arr == NULL || size == 0) {
printf("[]");
return;
}
printf("[");
for (int i = 0; i < size - 1; i++) {
printf("%d, ", arr[i]);
}
printf("%d]\n", arr[size - 1]);
}
/* Вывести массив */
void printArrayFloat(float arr[], int size) {
if (arr == NULL || size == 0) {
printf("[]");
return;
}
printf("[");
for (int i = 0; i < size - 1; i++) {
printf("%.2f, ", arr[i]);
}
printf("%.2f]\n", arr[size - 1]);
}
/* Вывести связный список */
void printLinkedList(ListNode *node) {
if (node == NULL) {
return;
}
while (node->next != NULL) {
printf("%d -> ", node->val);
node = node->next;
}
printf("%d\n", node->val);
}
typedef struct Trunk {
struct Trunk *prev;
char *str;
} Trunk;
Trunk *newTrunk(Trunk *prev, char *str) {
Trunk *trunk = (Trunk *)malloc(sizeof(Trunk));
trunk->prev = prev;
trunk->str = (char *)malloc(sizeof(char) * 10);
strcpy(trunk->str, str);
return trunk;
}
void showTrunks(Trunk *trunk) {
if (trunk == NULL) {
return;
}
showTrunks(trunk->prev);
printf("%s", trunk->str);
}
/**
* Вывести двоичное дерево
* Этот вывод дерева заимствован из TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
void printTreeHelper(TreeNode *node, Trunk *prev, bool isRight) {
if (node == NULL) {
return;
}
char *prev_str = " ";
Trunk *trunk = newTrunk(prev, prev_str);
printTreeHelper(node->right, trunk, true);
if (prev == NULL) {
trunk->str = "———";
} else if (isRight) {
trunk->str = "/———";
prev_str = " |";
} else {
trunk->str = "\\———";
prev->str = prev_str;
}
showTrunks(trunk);
printf("%d\n", node->val);
if (prev != NULL) {
prev->str = prev_str;
}
trunk->str = " |";
printTreeHelper(node->left, trunk, false);
}
/* Вывести двоичное дерево */
void printTree(TreeNode *root) {
printTreeHelper(root, NULL, false);
}
/* Вывести кучу */
void printHeap(int arr[], int size) {
TreeNode *root;
printf("Массивное представление кучи:");
printArray(arr, size);
printf("Древовидное представление кучи:\n");
root = arrayToTree(arr, size);
printTree(root);
}
#ifdef __cplusplus
}
#endif
#endif // PRINT_UTIL_H
+107
View File
@@ -0,0 +1,107 @@
/**
* File: tree_node.h
* Created Time: 2023-01-09
* Author: Reanon (793584285@qq.com)
*/
#ifndef TREE_NODE_H
#define TREE_NODE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <limits.h>
#define MAX_NODE_SIZE 5000
/* Структура узла двоичного дерева */
typedef struct TreeNode {
int val; // Значение узла
int height; // Высота узла
struct TreeNode *left; // Указатель на левый дочерний узел
struct TreeNode *right; // Указатель на правый дочерний узел
} 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;
}
// Правила кодирования сериализации см.:
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
// Массивное представление двоичного дерева:
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
// Связное представление двоичного дерева:
// /——— 15
// /——— 7
// /——— 3
// | \——— 6
// | \——— 12
// ——— 1
// \——— 2
// | /——— 9
// \——— 4
// \——— 8
/* Десериализовать список в двоичное дерево: рекурсия */
TreeNode *arrayToTreeDFS(int *arr, int size, int i) {
if (i < 0 || i >= size || arr[i] == INT_MAX) {
return NULL;
}
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
root->val = arr[i];
root->left = arrayToTreeDFS(arr, size, 2 * i + 1);
root->right = arrayToTreeDFS(arr, size, 2 * i + 2);
return root;
}
/* Десериализовать список в двоичное дерево */
TreeNode *arrayToTree(int *arr, int size) {
return arrayToTreeDFS(arr, size, 0);
}
/* Сериализовать двоичное дерево в список: рекурсия */
void treeToArrayDFS(TreeNode *root, int i, int *res, int *size) {
if (root == NULL) {
return;
}
while (i >= *size) {
res = realloc(res, (*size + 1) * sizeof(int));
res[*size] = INT_MAX;
(*size)++;
}
res[i] = root->val;
treeToArrayDFS(root->left, 2 * i + 1, res, size);
treeToArrayDFS(root->right, 2 * i + 2, res, size);
}
/* Сериализовать двоичное дерево в список */
int *treeToArray(TreeNode *root, int *size) {
*size = 0;
int *res = NULL;
treeToArrayDFS(root, 0, res, size);
return res;
}
/* Освободить память двоичного дерева */
void freeMemoryTree(TreeNode *root) {
if (root == NULL)
return;
freeMemoryTree(root->left);
freeMemoryTree(root->right);
free(root);
}
#ifdef __cplusplus
}
#endif
#endif // TREE_NODE_H
File diff suppressed because it is too large Load Diff
+259
View File
@@ -0,0 +1,259 @@
/**
* File: vector.h
* Created Time: 2023-07-13
* Author: Zuoxun (845242523@qq.com)、Gonglja (glj0@outlook.com)
*/
#ifndef VECTOR_H
#define VECTOR_H
#ifdef __cplusplus
extern "C" {
#endif
/* Определить тип вектора */
typedef struct vector {
int size; // Текущий размер вектора
int capacity; // Текущая емкость вектора
int depth; // Текущая глубина вектора
void **data; // Массив указателей на данные
} vector;
/* Создать вектор */
vector *newVector() {
vector *v = malloc(sizeof(vector));
v->size = 0;
v->capacity = 4;
v->depth = 1;
v->data = malloc(v->capacity * sizeof(void *));
return v;
}
/* Создать вектор, указав размер и значение элементов по умолчанию */
vector *_newVector(int size, void *elem, int elemSize) {
vector *v = malloc(sizeof(vector));
v->size = size;
v->capacity = size;
v->depth = 1;
v->data = malloc(v->capacity * sizeof(void *));
for (int i = 0; i < size; i++) {
void *tmp = malloc(sizeof(char) * elemSize);
memcpy(tmp, elem, elemSize);
v->data[i] = tmp;
}
return v;
}
/* Уничтожить вектор */
void delVector(vector *v) {
if (v) {
if (v->depth == 0) {
return;
} else if (v->depth == 1) {
for (int i = 0; i < v->size; i++) {
free(v->data[i]);
}
free(v);
} else {
for (int i = 0; i < v->size; i++) {
delVector(v->data[i]);
}
v->depth--;
}
}
}
/* Добавить элемент в конец вектора (копированием) */
void vectorPushback(vector *v, void *elem, int elemSize) {
if (v->size == v->capacity) {
v->capacity *= 2;
v->data = realloc(v->data, v->capacity * sizeof(void *));
}
void *tmp = malloc(sizeof(char) * elemSize);
memcpy(tmp, elem, elemSize);
v->data[v->size++] = tmp;
}
/* Извлечь элемент из конца вектора */
void vectorPopback(vector *v) {
if (v->size != 0) {
free(v->data[v->size - 1]);
v->size--;
}
}
/* Очистить вектор */
void vectorClear(vector *v) {
delVector(v);
v->size = 0;
v->capacity = 4;
v->depth = 1;
v->data = malloc(v->capacity * sizeof(void *));
}
/* Получить размер вектора */
int vectorSize(vector *v) {
return v->size;
}
/* Получить последний элемент вектора */
void *vectorBack(vector *v) {
int n = v->size;
return n > 0 ? v->data[n - 1] : NULL;
}
/* Получить первый элемент вектора */
void *vectorFront(vector *v) {
return v->size > 0 ? v->data[0] : NULL;
}
/* Получить элемент вектора по индексу pos */
void *vectorAt(vector *v, int pos) {
if (pos < 0 || pos >= v->size) {
printf("vectorAt: out of range\n");
return NULL;
}
return v->data[pos];
}
/* Установить элемент вектора по индексу pos */
void vectorSet(vector *v, int pos, void *elem, int elemSize) {
if (pos < 0 || pos >= v->size) {
printf("vectorSet: out of range\n");
return;
}
free(v->data[pos]);
void *tmp = malloc(sizeof(char) * elemSize);
memcpy(tmp, elem, elemSize);
v->data[pos] = tmp;
}
/* Расширение вектора */
void vectorExpand(vector *v) {
v->capacity *= 2;
v->data = realloc(v->data, v->capacity * sizeof(void *));
}
/* Сжатие вектора */
void vectorShrink(vector *v) {
v->capacity /= 2;
v->data = realloc(v->data, v->capacity * sizeof(void *));
}
/* Вставить элемент по индексу pos в вектор */
void vectorInsert(vector *v, int pos, void *elem, int elemSize) {
if (v->size == v->capacity) {
vectorExpand(v);
}
for (int j = v->size; j > pos; j--) {
v->data[j] = v->data[j - 1];
}
void *tmp = malloc(sizeof(char) * elemSize);
memcpy(tmp, elem, elemSize);
v->data[pos] = tmp;
v->size++;
}
/* Удалить элемент вектора по индексу pos */
void vectorErase(vector *v, int pos) {
if (v->size != 0) {
free(v->data[pos]);
for (int j = pos; j < v->size - 1; j++) {
v->data[j] = v->data[j + 1];
}
v->size--;
}
}
/* Обмен элементов вектора */
void vectorSwap(vector *v, int i, int j) {
void *tmp = v->data[i];
v->data[i] = v->data[j];
v->data[j] = tmp;
}
/* Пуст ли вектор */
bool vectorEmpty(vector *v) {
return v->size == 0;
}
/* Заполнен ли вектор */
bool vectorFull(vector *v) {
return v->size == v->capacity;
}
/* Равны ли векторы */
bool vectorEqual(vector *v1, vector *v2) {
if (v1->size != v2->size) {
printf("size not equal\n");
return false;
}
for (int i = 0; i < v1->size; i++) {
void *a = v1->data[i];
void *b = v2->data[i];
if (memcmp(a, b, sizeof(a)) != 0) {
printf("data %d not equal\n", i);
return false;
}
}
return true;
}
/* Отсортировать содержимое вектора */
void vectorSort(vector *v, int (*cmp)(const void *, const void *)) {
qsort(v->data, v->size, sizeof(void *), cmp);
}
/* Функция печати: нужно передать функцию для вывода значения переменной */
/* В настоящее время поддерживается только вывод vector глубины 1 */
void printVector(vector *v, void (*printFunc)(vector *v, void *p)) {
if (v) {
if (v->depth == 0) {
return;
} else if (v->depth == 1) {
if(v->size == 0) {
printf("\n");
return;
}
for (int i = 0; i < v->size; i++) {
if (i == 0) {
printf("[");
} else if (i == v->size - 1) {
printFunc(v, v->data[i]);
printf("]\r\n");
break;
}
printFunc(v, v->data[i]);
printf(",");
}
} else {
for (int i = 0; i < v->size; i++) {
printVector(v->data[i], printFunc);
}
v->depth--;
}
}
}
/* В настоящее время поддерживается только вывод vector глубины 2 */
void printVectorMatrix(vector *vv, void (*printFunc)(vector *v, void *p)) {
printf("[\n");
for (int i = 0; i < vv->size; i++) {
vector *v = (vector *)vv->data[i];
printf(" [");
for (int j = 0; j < v->size; j++) {
printFunc(v, v->data[j]);
if (j != v->size - 1)
printf(",");
}
printf("],");
printf("\n");
}
printf("]\n");
}
#ifdef __cplusplus
}
#endif
#endif // VECTOR_H
+49
View File
@@ -0,0 +1,49 @@
/**
* File: vertex.h
* Created Time: 2023-10-28
* Author: krahets (krahets@163.com)
*/
#ifndef VERTEX_H
#define VERTEX_H
#ifdef __cplusplus
extern "C" {
#endif
/* Структура вершины */
typedef struct {
int val;
} Vertex;
/* Конструктор, инициализирующий новый узел */
Vertex *newVertex(int val) {
Vertex *vet;
vet = (Vertex *)malloc(sizeof(Vertex));
vet->val = val;
return vet;
}
/* Преобразовать массив значений в массив вершин */
Vertex **valsToVets(int *vals, int size) {
Vertex **vertices = (Vertex **)malloc(size * sizeof(Vertex *));
for (int i = 0; i < size; ++i) {
vertices[i] = newVertex(vals[i]);
}
return vertices;
}
/* Преобразовать массив вершин в массив значений */
int *vetsToVals(Vertex **vertices, int size) {
int *vals = (int *)malloc(size * sizeof(int));
for (int i = 0; i < size; ++i) {
vals[i] = vertices[i]->val;
}
return vals;
}
#ifdef __cplusplus
}
#endif
#endif // VERTEX_H