Skip to content

Commit 94b5a4e

Browse files
committed
feat(tool-list): migrate C-algorithm repo into c-classic module
Encapsulate fooSynaptic/C-algorithm sources under tool-list/algorithms/c-classic with Makefile, README, catalog registration, and small compile fixes.
1 parent efeec3b commit 94b5a4e

16 files changed

Lines changed: 1645 additions & 0 deletions

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,13 @@ tool-list/algorithms/greedy/greedy
7878
tool-list/algorithms/heapsort/heapmax
7979
tool-list/algorithms/heapsort/arrtoheap
8080
tool-list/algorithms/heapsort/inittree
81+
tool-list/algorithms/c-classic/heap_sort
82+
tool-list/algorithms/c-classic/merge_sort
83+
tool-list/algorithms/c-classic/dijkstra
84+
tool-list/algorithms/c-classic/linklist
85+
tool-list/algorithms/c-classic/reverse_linklist
86+
tool-list/algorithms/c-classic/sqstack
87+
tool-list/algorithms/c-classic/longest_palmseq
88+
tool-list/algorithms/c-classic/palindrome_num
89+
tool-list/algorithms/c-classic/max
90+
tool-list/algorithms/c-classic/test

scripts/sync_catalog_from_legacy.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,30 @@
140140
"doc": "tool-list/algorithms/greedy/greedy.c",
141141
"related": {"reading": [], "solved": []},
142142
},
143+
{
144+
"id": "tool-c-classic",
145+
"name": "CClassicAlgorithms",
146+
"kind": "library",
147+
"paths": [
148+
"tool-list/algorithms/c-classic/Dijkstra.cpp",
149+
"tool-list/algorithms/c-classic/LinkList.cpp",
150+
"tool-list/algorithms/c-classic/SqStack.cpp",
151+
"tool-list/algorithms/c-classic/heap_sort.c",
152+
"tool-list/algorithms/c-classic/longest_palindrom.h",
153+
"tool-list/algorithms/c-classic/longest_palmseq.cpp",
154+
"tool-list/algorithms/c-classic/max.cpp",
155+
"tool-list/algorithms/c-classic/merge_sort.c",
156+
"tool-list/algorithms/c-classic/palindrome_num.cpp",
157+
"tool-list/algorithms/c-classic/reverse_linklist.cpp",
158+
"tool-list/algorithms/c-classic/test.cpp",
159+
"tool-list/algorithms/c-classic/Makefile",
160+
],
161+
"entry": "tool-list/algorithms/c-classic/Makefile",
162+
"tags": ["c", "cpp", "sorting", "graph", "linked-list", "stack", "palindrome"],
163+
"deps": [],
164+
"doc": "tool-list/algorithms/c-classic/README.md",
165+
"related": {"reading": [], "solved": []},
166+
},
143167
]
144168

145169

tool-list/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ tool-list/
1818
├── fastsort/
1919
├── greedy/
2020
├── datastruct/
21+
├── c-classic/ # C/C++ classics (ex fooSynaptic/C-algorithm)
2122
└── cpp/ # C++ 基础练习
2223
```
2324

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/**
2+
* @file Dijkstra.cpp
3+
* @brief Dijkstra's Shortest Path Algorithm Implementation
4+
*
5+
* Dijkstra's algorithm finds the shortest path from a source vertex to all
6+
* other vertices in a weighted graph with non-negative edge weights.
7+
*
8+
* Time Complexity: O(V²) where V is the number of vertices
9+
* (Can be improved to O(E + V log V) with a min-heap)
10+
*
11+
* Space Complexity: O(V²) for the adjacency matrix representation
12+
*/
13+
14+
#include <iostream>
15+
#include <climits>
16+
#include <algorithm>
17+
18+
/* Constants */
19+
#define MAX_VERTICES 10
20+
#define INFINITE_DISTANCE INT_MAX / 2 /* Use large value to avoid overflow */
21+
22+
/* Global arrays for Dijkstra's algorithm */
23+
int distance_arr[MAX_VERTICES]; /**< Shortest distance from source to each vertex */
24+
int predecessor_arr[MAX_VERTICES]; /**< Predecessor of each vertex in shortest path */
25+
int adjacency_matrix[MAX_VERTICES][MAX_VERTICES]; /**< Graph represented as adjacency matrix */
26+
27+
/**
28+
* @brief Initializes the adjacency matrix with sample data
29+
*
30+
* This is a placeholder - in practice, the graph would be loaded
31+
* from input or file.
32+
*/
33+
void init_graph()
34+
{
35+
/* Initialize with zeros (no edges) */
36+
for (int i = 0; i < MAX_VERTICES; i++) {
37+
for (int j = 0; j < MAX_VERTICES; j++) {
38+
adjacency_matrix[i][j] = (i == j) ? 0 : INFINITE_DISTANCE;
39+
}
40+
}
41+
/* Add sample edges here if needed for testing */
42+
}
43+
44+
/**
45+
* @brief Dijkstra's shortest path algorithm
46+
*
47+
* Finds the shortest path from the source vertex to all other vertices.
48+
* Uses a greedy approach, always selecting the unvisited vertex with
49+
* the smallest tentative distance.
50+
*
51+
* @param source_vertex The starting vertex for path calculations
52+
*/
53+
void dijkstra_shortest_path(int source_vertex)
54+
{
55+
bool visited[MAX_VERTICES] = {false}; /* Track visited vertices */
56+
int num_vertices = MAX_VERTICES;
57+
58+
/* Initialize distances and predecessors */
59+
for (int i = 0; i < num_vertices; ++i) {
60+
distance_arr[i] = adjacency_matrix[source_vertex][i];
61+
visited[i] = false;
62+
63+
if (distance_arr[i] == INFINITE_DISTANCE) {
64+
predecessor_arr[i] = -1; /* No path from source */
65+
} else {
66+
predecessor_arr[i] = source_vertex;
67+
}
68+
}
69+
70+
/* Distance to source is 0 */
71+
distance_arr[source_vertex] = 0;
72+
visited[source_vertex] = true;
73+
74+
/* Find shortest path for all remaining vertices */
75+
for (int i = 1; i < num_vertices; i++) {
76+
/* Find the unvisited vertex with minimum distance */
77+
int min_distance = INFINITE_DISTANCE;
78+
int min_vertex = source_vertex;
79+
80+
for (int v = 0; v < num_vertices; ++v) {
81+
if (!visited[v] && distance_arr[v] < min_distance) {
82+
min_vertex = v;
83+
min_distance = distance_arr[v];
84+
}
85+
}
86+
87+
/* Mark the selected vertex as visited */
88+
visited[min_vertex] = true;
89+
90+
/* Update distances to neighbors of the selected vertex */
91+
for (int v = 0; v < num_vertices; v++) {
92+
if (!visited[v] && adjacency_matrix[min_vertex][v] != INFINITE_DISTANCE) {
93+
int new_distance = distance_arr[min_vertex] + adjacency_matrix[min_vertex][v];
94+
95+
/* If found a shorter path, update distance and predecessor */
96+
if (new_distance < distance_arr[v]) {
97+
distance_arr[v] = new_distance;
98+
predecessor_arr[v] = min_vertex;
99+
}
100+
}
101+
}
102+
}
103+
}
104+
105+
/**
106+
* @brief Prints the shortest path from source to a target vertex
107+
*
108+
* @param target_vertex The destination vertex
109+
* @param source_vertex The source vertex (for stopping condition)
110+
*/
111+
void print_path(int target_vertex, int source_vertex)
112+
{
113+
if (target_vertex == source_vertex) {
114+
std::cout << source_vertex;
115+
return;
116+
}
117+
if (predecessor_arr[target_vertex] == -1) {
118+
std::cout << "No path exists";
119+
return;
120+
}
121+
print_path(predecessor_arr[target_vertex], source_vertex);
122+
std::cout << " -> " << target_vertex;
123+
}
124+
125+
/**
126+
* @brief Main function demonstrating Dijkstra's algorithm
127+
*
128+
* Note: This implementation requires manual initialization of the
129+
* adjacency matrix before running the algorithm.
130+
*/
131+
int main()
132+
{
133+
/* Initialize graph - add your edge weights here */
134+
init_graph();
135+
136+
/* Example: Create a simple graph */
137+
/* Vertex 0 to 1 with weight 4 */
138+
adjacency_matrix[0][1] = 4;
139+
adjacency_matrix[1][0] = 4;
140+
141+
/* Vertex 0 to 2 with weight 2 */
142+
adjacency_matrix[0][2] = 2;
143+
adjacency_matrix[2][0] = 2;
144+
145+
/* Vertex 1 to 2 with weight 1 */
146+
adjacency_matrix[1][2] = 1;
147+
adjacency_matrix[2][1] = 1;
148+
149+
/* Vertex 1 to 3 with weight 5 */
150+
adjacency_matrix[1][3] = 5;
151+
adjacency_matrix[3][1] = 5;
152+
153+
/* Vertex 2 to 3 with weight 8 */
154+
adjacency_matrix[2][3] = 8;
155+
adjacency_matrix[3][2] = 8;
156+
157+
int source = 0;
158+
dijkstra_shortest_path(source);
159+
160+
/* Print results */
161+
std::cout << "Shortest distances from vertex " << source << ":" << std::endl;
162+
for (int i = 0; i < MAX_VERTICES; i++) {
163+
if (distance_arr[i] != INFINITE_DISTANCE) {
164+
std::cout << "To vertex " << i << ": distance = " << distance_arr[i];
165+
std::cout << ", path: ";
166+
print_path(i, source);
167+
std::cout << std::endl;
168+
}
169+
}
170+
171+
return 0;
172+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* @file LinkList.cpp
3+
* @brief Singly Linked List implementation in C++
4+
*
5+
* This file provides basic linked list operations including:
6+
* - Initialization
7+
* - Node insertion at head
8+
* - List traversal and printing
9+
*/
10+
11+
#include <cstdio>
12+
#include <cstdlib>
13+
14+
/* Constants */
15+
#define MAX_SIZE 100
16+
17+
/**
18+
* @brief Node structure for singly linked list
19+
*/
20+
struct LinkNode {
21+
int data; /**< Data stored in the node */
22+
LinkNode* next; /**< Pointer to the next node */
23+
};
24+
25+
/* Function Prototypes */
26+
void init_list(LinkNode*& head);
27+
void add_node(int value, LinkNode* head);
28+
void print_list(const LinkNode* head);
29+
void free_list(LinkNode* head);
30+
31+
/**
32+
* @brief Initializes an empty linked list with a dummy head node
33+
*
34+
* @param head Reference to the head pointer (will be allocated)
35+
*/
36+
void init_list(LinkNode*& head)
37+
{
38+
head = (LinkNode*)malloc(sizeof(LinkNode));
39+
if (head == nullptr) {
40+
fprintf(stderr, "Error: Memory allocation failed\n");
41+
exit(EXIT_FAILURE);
42+
}
43+
head->next = nullptr;
44+
}
45+
46+
/**
47+
* @brief Adds a new node with given value at the head of the list
48+
*
49+
* @param value Integer value to store in the new node
50+
* @param head Pointer to the head of the list
51+
*/
52+
void add_node(int value, LinkNode* head)
53+
{
54+
if (head == nullptr) {
55+
fprintf(stderr, "Error: List not initialized\n");
56+
return;
57+
}
58+
59+
LinkNode* new_node = (LinkNode*)malloc(sizeof(LinkNode));
60+
if (new_node == nullptr) {
61+
fprintf(stderr, "Error: Memory allocation failed\n");
62+
return;
63+
}
64+
65+
new_node->data = value;
66+
new_node->next = head->next;
67+
head->next = new_node;
68+
}
69+
70+
/**
71+
* @brief Prints all elements in the linked list
72+
*
73+
* @param head Pointer to the head of the list
74+
*/
75+
void print_list(const LinkNode* head)
76+
{
77+
if (head == nullptr) {
78+
return;
79+
}
80+
81+
const LinkNode* current = head->next;
82+
while (current != nullptr) {
83+
printf("%d ", current->data);
84+
current = current->next;
85+
}
86+
printf("\n");
87+
}
88+
89+
/**
90+
* @brief Frees all memory allocated for the linked list
91+
*
92+
* @param head Pointer to the head of the list
93+
*/
94+
void free_list(LinkNode* head)
95+
{
96+
if (head == nullptr) {
97+
return;
98+
}
99+
100+
LinkNode* current = head;
101+
while (current != nullptr) {
102+
LinkNode* temp = current;
103+
current = current->next;
104+
free(temp);
105+
}
106+
}
107+
108+
/**
109+
* @brief Main function - demonstrates linked list operations
110+
*/
111+
int main()
112+
{
113+
LinkNode* head = nullptr;
114+
115+
init_list(head);
116+
117+
/* Add nodes to the list (inserts at head, so order is reversed) */
118+
add_node(1, head);
119+
add_node(2, head);
120+
add_node(3, head);
121+
122+
printf("Linked list contents: ");
123+
print_list(head);
124+
125+
/* Clean up */
126+
free_list(head);
127+
head = nullptr;
128+
129+
return 0;
130+
}

0 commit comments

Comments
 (0)