Simplify struct declarations of C.

Use PascalCase for all structs in C.
SImplify n_queens.c
Format C code for chapter of graph.
This commit is contained in:
krahets
2023-10-18 02:16:26 +08:00
parent 070d23ee6e
commit 1e49574332
35 changed files with 503 additions and 599 deletions

View File

@@ -7,16 +7,14 @@
#include "graph_adjacency_list.c"
/* 哈希表 */
struct hashTable {
typedef struct {
unsigned int size;
unsigned int *array;
};
typedef struct hashTable hashTable;
} HashTable;
/* 初始化哈希表 */
hashTable *newHash(unsigned int size) {
hashTable *h = (hashTable *)malloc(sizeof(hashTable));
HashTable *newHash(unsigned int size) {
HashTable *h = (HashTable *)malloc(sizeof(HashTable));
h->array = (unsigned int *)malloc(sizeof(unsigned int) * size);
memset(h->array, 0, sizeof(unsigned int) * size);
h->size = size;
@@ -24,12 +22,12 @@ hashTable *newHash(unsigned int size) {
}
/* 标记索引过的顶点 */
void hashMark(hashTable *h, int index) {
void hashMark(HashTable *h, int index) {
h->array[index % h->size] = 1;
}
/* 查询顶点是否已被标记 */
int hashQuery(hashTable *h, int index) {
int hashQuery(HashTable *h, int index) {
// 若顶点已被标记,则返回 1
if (h->array[index % h->size] == 1) {
return 1;
@@ -39,14 +37,14 @@ int hashQuery(hashTable *h, int index) {
}
/* 释放哈希表内存 */
void freeHash(hashTable *h) {
void freeHash(HashTable *h) {
free(h->array);
free(h);
}
/* 深度优先遍历 DFS 辅助函数 */
int resIndex = 0;
void dfs(graphAdjList *graph, hashTable *visited, Vertex *vet, Vertex **res) {
void dfs(GraphAdjList *graph, HashTable *visited, Vertex *vet, Vertex **res) {
if (hashQuery(visited, vet->pos) == 1) {
return; // 跳过已被访问过的顶点
}
@@ -54,7 +52,7 @@ void dfs(graphAdjList *graph, hashTable *visited, Vertex *vet, Vertex **res) {
res[resIndex] = vet; // 将顶点存入数组
resIndex++;
// 遍历该顶点链表
Node *n = vet->linked->head->next;
Node *n = vet->list->head->next;
while (n != 0) {
// 递归访问邻接顶点
dfs(graph, visited, n->val, res);
@@ -65,12 +63,12 @@ void dfs(graphAdjList *graph, hashTable *visited, Vertex *vet, Vertex **res) {
/* 深度优先遍历 DFS */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
Vertex **graphDFS(graphAdjList *graph, Vertex *startVet) {
Vertex **graphDFS(GraphAdjList *graph, Vertex *startVet) {
// 顶点遍历序列
Vertex **res = (Vertex **)malloc(sizeof(Vertex *) * graph->size);
memset(res, 0, sizeof(Vertex *) * graph->size);
// 哈希表,用于记录已被访问过的顶点
hashTable *visited = newHash(graph->size);
HashTable *visited = newHash(graph->size);
dfs(graph, visited, startVet, res);
// 释放哈希表内存并将数组索引归零
freeHash(visited);
@@ -81,7 +79,7 @@ Vertex **graphDFS(graphAdjList *graph, Vertex *startVet) {
/* Driver Code */
int main() {
graphAdjList *graph = newGraphAdjList(10);
GraphAdjList *graph = newGraphAdjList(10);
for (int i = 0; i < 7; i++) {
addVertex(graph, i);
}
@@ -95,7 +93,7 @@ int main() {
printGraph(graph);
// 深度优先遍历 DFS
Vertex **vet = graphDFS(graph, graph->verticesList[0]);
Vertex **vet = graphDFS(graph, graph->vertices[0]);
// 输出遍历结果
printf("\n深度优先遍历DFS顶点序列为\n");