mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-26 13:06:07 +00:00
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:
@@ -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,24 +37,22 @@ int hashQuery(hashTable *h, int index) {
|
||||
}
|
||||
|
||||
/* 释放哈希表内存 */
|
||||
void freeHash(hashTable *h) {
|
||||
void freeHash(HashTable *h) {
|
||||
free(h->array);
|
||||
free(h);
|
||||
}
|
||||
|
||||
/* 队列 */
|
||||
struct queue {
|
||||
typedef struct {
|
||||
Vertex **list;
|
||||
unsigned int size;
|
||||
int head;
|
||||
int tail;
|
||||
};
|
||||
|
||||
typedef struct queue queue;
|
||||
} Queue;
|
||||
|
||||
/* 初始化队列 */
|
||||
queue *newQueue(unsigned int size) {
|
||||
queue *q = (queue *)malloc(sizeof(queue));
|
||||
Queue *newQueue(unsigned int size) {
|
||||
Queue *q = (Queue *)malloc(sizeof(Queue));
|
||||
q->size = size;
|
||||
q->list = (Vertex **)malloc(sizeof(Vertex *) * size);
|
||||
q->head = 0;
|
||||
@@ -66,44 +62,44 @@ queue *newQueue(unsigned int size) {
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
void queuePush(queue *q, Vertex *vet) {
|
||||
void queuePush(Queue *q, Vertex *vet) {
|
||||
q->list[q->tail] = vet;
|
||||
q->tail++;
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
void queuePop(queue *q) {
|
||||
void queuePop(Queue *q) {
|
||||
q->head++;
|
||||
}
|
||||
|
||||
/* 队首元素 */
|
||||
Vertex *queueTop(queue *q) {
|
||||
Vertex *queueTop(Queue *q) {
|
||||
return q->list[q->head];
|
||||
}
|
||||
|
||||
/* 释放队列内存 */
|
||||
void freeQueue(queue *q) {
|
||||
void freeQueue(Queue *q) {
|
||||
free(q->list);
|
||||
free(q);
|
||||
}
|
||||
|
||||
/* 广度优先遍历 */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
Vertex **graphBFS(graphAdjList *t, Vertex *startVet) {
|
||||
Vertex **graphBFS(GraphAdjList *t, Vertex *startVet) {
|
||||
// 顶点遍历序列
|
||||
Vertex **res = (Vertex **)malloc(sizeof(Vertex *) * t->size);
|
||||
memset(res, 0, sizeof(Vertex *) * t->size);
|
||||
// 队列用于实现 BFS
|
||||
queue *que = newQueue(t->size);
|
||||
Queue *que = newQueue(t->size);
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
hashTable *visited = newHash(t->size);
|
||||
HashTable *visited = newHash(t->size);
|
||||
int resIndex = 0;
|
||||
queuePush(que, startVet); // 将第一个元素入队
|
||||
hashMark(visited, startVet->pos); // 标记第一个入队的顶点
|
||||
// 以顶点 vet 为起点,循环直至访问完所有顶点
|
||||
while (que->head < que->tail) {
|
||||
// 遍历该顶点的边链表,将所有与该顶点有连接的,并且未被标记的顶点入队
|
||||
Node *n = queueTop(que)->linked->head->next;
|
||||
Node *n = queueTop(que)->list->head->next;
|
||||
while (n != 0) {
|
||||
// 查询哈希表,若该索引的顶点已入队,则跳过,否则入队并标记
|
||||
if (hashQuery(visited, n->val->pos) == 1) {
|
||||
@@ -129,7 +125,7 @@ Vertex **graphBFS(graphAdjList *t, Vertex *startVet) {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化无向图 */
|
||||
graphAdjList *graph = newGraphAdjList(3);
|
||||
GraphAdjList *graph = newGraphAdjList(3);
|
||||
// 初始化顶点
|
||||
for (int i = 0; i < 10; i++) {
|
||||
addVertex(graph, i);
|
||||
@@ -150,7 +146,7 @@ int main() {
|
||||
printf("\n初始化后,图为:\n");
|
||||
printGraph(graph);
|
||||
printf("\n广度优先遍历(BFS)顶点序列为\n");
|
||||
Vertex **vets = graphBFS(graph, graph->verticesList[0]);
|
||||
Vertex **vets = graphBFS(graph, graph->vertices[0]);
|
||||
|
||||
// 打印广度优先遍历数组
|
||||
printf("[");
|
||||
|
||||
Reference in New Issue
Block a user