Translate all code to English (#1836)

* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
This commit is contained in:
Yudong Jin
2025-12-31 07:44:52 +08:00
committed by GitHub
parent 45e1295241
commit 2778a6f9c7
1284 changed files with 71557 additions and 3275 deletions
+4
View File
@@ -0,0 +1,4 @@
add_executable(graph_adjacency_matrix graph_adjacency_matrix.c)
add_executable(graph_adjacency_list_test graph_adjacency_list_test.c)
add_executable(graph_bfs graph_bfs.c)
add_executable(graph_dfs graph_dfs.c)
@@ -0,0 +1,171 @@
/**
* File: graph_adjacency_list.c
* Created Time: 2023-07-07
* Author: NI-SW (947743645@qq.com)
*/
#include "../utils/common.h"
// Assume max node count is 100
#define MAX_SIZE 100
/* Node structure */
typedef struct AdjListNode {
Vertex *vertex; // Vertex
struct AdjListNode *next; // Successor node
} AdjListNode;
/* Undirected graph class based on adjacency list */
typedef struct {
AdjListNode *heads[MAX_SIZE]; // Node array
int size; // Node count
} GraphAdjList;
/* Constructor */
GraphAdjList *newGraphAdjList() {
GraphAdjList *graph = (GraphAdjList *)malloc(sizeof(GraphAdjList));
if (!graph) {
return NULL;
}
graph->size = 0;
for (int i = 0; i < MAX_SIZE; i++) {
graph->heads[i] = NULL;
}
return graph;
}
/* Destructor */
void delGraphAdjList(GraphAdjList *graph) {
for (int i = 0; i < graph->size; i++) {
AdjListNode *cur = graph->heads[i];
while (cur != NULL) {
AdjListNode *next = cur->next;
if (cur != graph->heads[i]) {
free(cur);
}
cur = next;
}
free(graph->heads[i]->vertex);
free(graph->heads[i]);
}
free(graph);
}
/* Find node corresponding to vertex */
AdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {
for (int i = 0; i < graph->size; i++) {
if (graph->heads[i]->vertex == vet) {
return graph->heads[i];
}
}
return NULL;
}
/* Add edge helper function */
void addEdgeHelper(AdjListNode *head, Vertex *vet) {
AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));
node->vertex = vet;
// Head insertion
node->next = head->next;
head->next = node;
}
/* Add edge */
void addEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {
AdjListNode *head1 = findNode(graph, vet1);
AdjListNode *head2 = findNode(graph, vet2);
assert(head1 != NULL && head2 != NULL && head1 != head2);
// Add edge vet1 - vet2
addEdgeHelper(head1, vet2);
addEdgeHelper(head2, vet1);
}
/* Remove edge helper function */
void removeEdgeHelper(AdjListNode *head, Vertex *vet) {
AdjListNode *pre = head;
AdjListNode *cur = head->next;
// Search for node corresponding to vet in list
while (cur != NULL && cur->vertex != vet) {
pre = cur;
cur = cur->next;
}
if (cur == NULL)
return;
// Remove node corresponding to vet from list
pre->next = cur->next;
// Free memory
free(cur);
}
/* Remove edge */
void removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {
AdjListNode *head1 = findNode(graph, vet1);
AdjListNode *head2 = findNode(graph, vet2);
assert(head1 != NULL && head2 != NULL);
// Remove edge vet1 - vet2
removeEdgeHelper(head1, head2->vertex);
removeEdgeHelper(head2, head1->vertex);
}
/* Add vertex */
void addVertex(GraphAdjList *graph, Vertex *vet) {
assert(graph != NULL && graph->size < MAX_SIZE);
AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode));
head->vertex = vet;
head->next = NULL;
// Add a new linked list in the adjacency list
graph->heads[graph->size++] = head;
}
/* Remove vertex */
void removeVertex(GraphAdjList *graph, Vertex *vet) {
AdjListNode *node = findNode(graph, vet);
assert(node != NULL);
// Remove the linked list corresponding to vertex vet in the adjacency list
AdjListNode *cur = node, *pre = NULL;
while (cur) {
pre = cur;
cur = cur->next;
free(pre);
}
// Traverse the linked lists of other vertices and remove all edges containing vet
for (int i = 0; i < graph->size; i++) {
cur = graph->heads[i];
pre = NULL;
while (cur) {
pre = cur;
cur = cur->next;
if (cur && cur->vertex == vet) {
pre->next = cur->next;
free(cur);
break;
}
}
}
// Move vertices after this vertex forward to fill gap
int i;
for (i = 0; i < graph->size; i++) {
if (graph->heads[i] == node)
break;
}
for (int j = i; j < graph->size - 1; j++) {
graph->heads[j] = graph->heads[j + 1];
}
graph->size--;
free(vet);
}
/* Print adjacency list */
void printGraph(const GraphAdjList *graph) {
printf("Adjacency list =\n");
for (int i = 0; i < graph->size; ++i) {
AdjListNode *node = graph->heads[i];
printf("%d: [", node->vertex->val);
node = node->next;
while (node) {
printf("%d, ", node->vertex->val);
node = node->next;
}
printf("]\n");
}
}
@@ -0,0 +1,55 @@
/**
* File: graph_adjacency_list_test.c
* Created Time: 2023-07-11
* Author: NI-SW (947743645@qq.com)
*/
#include "graph_adjacency_list.c"
/* Driver Code */
int main() {
int vals[] = {1, 3, 2, 5, 4};
int size = sizeof(vals) / sizeof(vals[0]);
Vertex **v = valsToVets(vals, size);
Vertex *edges[][2] = {{v[0], v[1]}, {v[0], v[3]}, {v[1], v[2]}, {v[2], v[3]}, {v[2], v[4]}, {v[3], v[4]}};
int egdeSize = sizeof(edges) / sizeof(edges[0]);
GraphAdjList *graph = newGraphAdjList();
// Add all vertices and edges
for (int i = 0; i < size; i++) {
addVertex(graph, v[i]);
}
for (int i = 0; i < egdeSize; i++) {
addEdge(graph, edges[i][0], edges[i][1]);
}
printf("\nAfter initialization, graph is\n");
printGraph(graph);
/* Add edge */
// Vertices 1, 3 are v[0], v[1]
addEdge(graph, v[0], v[2]);
printf("\nAfter adding edge 1-2, graph is\n");
printGraph(graph);
/* Remove edge */
// Vertex 3 is v[1]
removeEdge(graph, v[0], v[1]);
printf("\nAfter deleting edge 1-3, graph is\n");
printGraph(graph);
/* Add vertex */
Vertex *v5 = newVertex(6);
addVertex(graph, v5);
printf("\nAfter adding vertex 6, graph is\n");
printGraph(graph);
/* Remove vertex */
// Vertex 3 is v[1]
removeVertex(graph, v[1]);
printf("\nAfter deleting vertex 3, graph is:\n");
printGraph(graph);
// Free memory
delGraphAdjList(graph);
free(v);
return 0;
}
@@ -0,0 +1,150 @@
/**
* File: graph_adjacency_matrix.c
* Created Time: 2023-07-06
* Author: NI-SW (947743645@qq.com)
*/
#include "../utils/common.h"
// Assume max vertex count is 100
#define MAX_SIZE 100
/* Undirected graph structure based on adjacency matrix */
typedef struct {
int vertices[MAX_SIZE];
int adjMat[MAX_SIZE][MAX_SIZE];
int size;
} GraphAdjMat;
/* Constructor */
GraphAdjMat *newGraphAdjMat() {
GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));
graph->size = 0;
for (int i = 0; i < MAX_SIZE; i++) {
for (int j = 0; j < MAX_SIZE; j++) {
graph->adjMat[i][j] = 0;
}
}
return graph;
}
/* Destructor */
void delGraphAdjMat(GraphAdjMat *graph) {
free(graph);
}
/* Add vertex */
void addVertex(GraphAdjMat *graph, int val) {
if (graph->size == MAX_SIZE) {
fprintf(stderr, "Graph vertex count has reached maximum\n");
return;
}
// Add nth vertex and zero nth row and column
int n = graph->size;
graph->vertices[n] = val;
for (int i = 0; i <= n; i++) {
graph->adjMat[n][i] = graph->adjMat[i][n] = 0;
}
graph->size++;
}
/* Remove vertex */
void removeVertex(GraphAdjMat *graph, int index) {
if (index < 0 || index >= graph->size) {
fprintf(stderr, "Vertex index out of bounds\n");
return;
}
// Remove the vertex at index from the vertex list
for (int i = index; i < graph->size - 1; i++) {
graph->vertices[i] = graph->vertices[i + 1];
}
// Remove the row at index from the adjacency matrix
for (int i = index; i < graph->size - 1; i++) {
for (int j = 0; j < graph->size; j++) {
graph->adjMat[i][j] = graph->adjMat[i + 1][j];
}
}
// Remove the column at index from the adjacency matrix
for (int i = 0; i < graph->size; i++) {
for (int j = index; j < graph->size - 1; j++) {
graph->adjMat[i][j] = graph->adjMat[i][j + 1];
}
}
graph->size--;
}
/* Add edge */
// Parameters i, j correspond to the vertices element indices
void addEdge(GraphAdjMat *graph, int i, int j) {
if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {
fprintf(stderr, "Edge index out of bounds or equal\n");
return;
}
graph->adjMat[i][j] = 1;
graph->adjMat[j][i] = 1;
}
/* Remove edge */
// Parameters i, j correspond to the vertices element indices
void removeEdge(GraphAdjMat *graph, int i, int j) {
if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {
fprintf(stderr, "Edge index out of bounds or equal\n");
return;
}
graph->adjMat[i][j] = 0;
graph->adjMat[j][i] = 0;
}
/* Print adjacency matrix */
void printGraphAdjMat(GraphAdjMat *graph) {
printf("Vertex list = ");
printArray(graph->vertices, graph->size);
printf("Adjacency matrix =\n");
for (int i = 0; i < graph->size; i++) {
printArray(graph->adjMat[i], graph->size);
}
}
/* Driver Code */
int main() {
// Add edge
GraphAdjMat *graph = newGraphAdjMat();
int vertices[] = {1, 3, 2, 5, 4};
for (int i = 0; i < 5; i++) {
addVertex(graph, vertices[i]);
}
int edges[][2] = {{0, 1}, {0, 3}, {1, 2}, {2, 3}, {2, 4}, {3, 4}};
for (int i = 0; i < 6; i++) {
addEdge(graph, edges[i][0], edges[i][1]);
}
printf("\nAfter initialization, graph is\n");
printGraphAdjMat(graph);
/* Add edge */
// Add vertex
addEdge(graph, 0, 2);
printf("\nAfter adding edge 1-2, graph is\n");
printGraphAdjMat(graph);
/* Remove edge */
// Vertices 1, 3 have indices 0, 1 respectively
removeEdge(graph, 0, 1);
printf("\nAfter deleting edge 1-3, graph is\n");
printGraphAdjMat(graph);
/* Add vertex */
addVertex(graph, 6);
printf("\nAfter adding vertex 6, graph is\n");
printGraphAdjMat(graph);
/* Remove vertex */
// Vertex 3 has index 1
removeVertex(graph, 1);
printf("\nAfter deleting vertex 3, graph is\n");
printGraphAdjMat(graph);
// Free memory
delGraphAdjMat(graph);
return 0;
}
+116
View File
@@ -0,0 +1,116 @@
/**
* File: graph_bfs.c
* Created Time: 2023-07-11
* Author: NI-SW (947743645@qq.com)
*/
#include "graph_adjacency_list.c"
// Assume max node count is 100
#define MAX_SIZE 100
/* Node queue structure */
typedef struct {
Vertex *vertices[MAX_SIZE];
int front, rear, size;
} Queue;
/* Constructor */
Queue *newQueue() {
Queue *q = (Queue *)malloc(sizeof(Queue));
q->front = q->rear = q->size = 0;
return q;
}
/* Check if the queue is empty */
int isEmpty(Queue *q) {
return q->size == 0;
}
/* Enqueue operation */
void enqueue(Queue *q, Vertex *vet) {
q->vertices[q->rear] = vet;
q->rear = (q->rear + 1) % MAX_SIZE;
q->size++;
}
/* Dequeue operation */
Vertex *dequeue(Queue *q) {
Vertex *vet = q->vertices[q->front];
q->front = (q->front + 1) % MAX_SIZE;
q->size--;
return vet;
}
/* Check if vertex has been visited */
int isVisited(Vertex **visited, int size, Vertex *vet) {
// Traverse to find node using O(n) time
for (int i = 0; i < size; i++) {
if (visited[i] == vet)
return 1;
}
return 0;
}
/* Breadth-first traversal */
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
void graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {
// Queue used to implement BFS
Queue *queue = newQueue();
enqueue(queue, startVet);
visited[(*visitedSize)++] = startVet;
// Starting from vertex vet, loop until all vertices are visited
while (!isEmpty(queue)) {
Vertex *vet = dequeue(queue); // Dequeue the front vertex
res[(*resSize)++] = vet; // Record visited vertex
// Traverse all adjacent vertices of this vertex
AdjListNode *node = findNode(graph, vet);
while (node != NULL) {
// Skip vertices that have been visited
if (!isVisited(visited, *visitedSize, node->vertex)) {
enqueue(queue, node->vertex); // Only enqueue unvisited vertices
visited[(*visitedSize)++] = node->vertex; // Mark this vertex as visited
}
node = node->next;
}
}
// Free memory
free(queue);
}
/* Driver Code */
int main() {
// Add edge
int vals[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int size = sizeof(vals) / sizeof(vals[0]);
Vertex **v = valsToVets(vals, size);
Vertex *edges[][2] = {{v[0], v[1]}, {v[0], v[3]}, {v[1], v[2]}, {v[1], v[4]}, {v[2], v[5]}, {v[3], v[4]},
{v[3], v[6]}, {v[4], v[5]}, {v[4], v[7]}, {v[5], v[8]}, {v[6], v[7]}, {v[7], v[8]}};
int egdeSize = sizeof(edges) / sizeof(edges[0]);
GraphAdjList *graph = newGraphAdjList();
// Add all vertices and edges
for (int i = 0; i < size; i++) {
addVertex(graph, v[i]);
}
for (int i = 0; i < egdeSize; i++) {
addEdge(graph, edges[i][0], edges[i][1]);
}
printf("\nAfter initialization, graph is\n");
printGraph(graph);
// Breadth-first traversal
// Vertex traversal sequence
Vertex *res[MAX_SIZE];
int resSize = 0;
// Used to record visited vertices
Vertex *visited[MAX_SIZE];
int visitedSize = 0;
graphBFS(graph, v[0], res, &resSize, visited, &visitedSize);
printf("\nBreadth-first traversal (BFS) vertex sequence is\n");
printArray(vetsToVals(res, resSize), resSize);
// Free memory
delGraphAdjList(graph);
free(v);
return 0;
}
+75
View File
@@ -0,0 +1,75 @@
/**
* File: graph_dfs.c
* Created Time: 2023-07-13
* Author: NI-SW (947743645@qq.com)
*/
#include "graph_adjacency_list.c"
// Assume max node count is 100
#define MAX_SIZE 100
/* Check if vertex has been visited */
int isVisited(Vertex **res, int size, Vertex *vet) {
// Traverse to find node using O(n) time
for (int i = 0; i < size; i++) {
if (res[i] == vet) {
return 1;
}
}
return 0;
}
/* Depth-first traversal helper function */
void dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {
// Record visited vertex
res[(*resSize)++] = vet;
// Traverse all adjacent vertices of this vertex
AdjListNode *node = findNode(graph, vet);
while (node != NULL) {
// Skip vertices that have been visited
if (!isVisited(res, *resSize, node->vertex)) {
// Recursively visit adjacent vertices
dfs(graph, res, resSize, node->vertex);
}
node = node->next;
}
}
/* Depth-first traversal */
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
void graphDFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize) {
dfs(graph, res, resSize, startVet);
}
/* Driver Code */
int main() {
// Add edge
int vals[] = {0, 1, 2, 3, 4, 5, 6};
int size = sizeof(vals) / sizeof(vals[0]);
Vertex **v = valsToVets(vals, size);
Vertex *edges[][2] = {{v[0], v[1]}, {v[0], v[3]}, {v[1], v[2]}, {v[2], v[5]}, {v[4], v[5]}, {v[5], v[6]}};
int egdeSize = sizeof(edges) / sizeof(edges[0]);
GraphAdjList *graph = newGraphAdjList();
// Add all vertices and edges
for (int i = 0; i < size; i++) {
addVertex(graph, v[i]);
}
for (int i = 0; i < egdeSize; i++) {
addEdge(graph, edges[i][0], edges[i][1]);
}
printf("\nAfter initialization, graph is\n");
printGraph(graph);
// Depth-first traversal
Vertex *res[MAX_SIZE];
int resSize = 0;
graphDFS(graph, v[0], res, &resSize);
printf("\nDepth-first traversal (DFS) vertex sequence is\n");
printArray(vetsToVals(res, resSize), resSize);
// Free memory
delGraphAdjList(graph);
free(v);
return 0;
}