mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 14:39:44 +08: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:
@@ -8,11 +8,11 @@
|
||||
|
||||
typedef struct Vertex Vertex;
|
||||
typedef struct Node Node;
|
||||
typedef struct linkList linkList;
|
||||
typedef struct LinkedList LinkedList;
|
||||
|
||||
void freeVertex(Vertex *);
|
||||
void freeLinklist(linkList *);
|
||||
linkList *newLinklist(Vertex *);
|
||||
void freeLinklist(LinkedList *);
|
||||
LinkedList *newLinklist(Vertex *);
|
||||
|
||||
/* 链表节点 */
|
||||
struct Node {
|
||||
@@ -34,7 +34,7 @@ struct Vertex {
|
||||
// 节点值
|
||||
int val;
|
||||
// 与其它节点相连接的边的链表
|
||||
linkList *linked;
|
||||
LinkedList *list;
|
||||
// 索引位,标记该顶点在顶点列表中的索引
|
||||
unsigned int pos;
|
||||
};
|
||||
@@ -44,52 +44,52 @@ Vertex *newVertex(int val) {
|
||||
Vertex *vet = (Vertex *)malloc(sizeof(Vertex));
|
||||
// 为新节点赋值并建立该节点的链表
|
||||
vet->val = val;
|
||||
vet->linked = newLinklist(vet);
|
||||
vet->list = newLinklist(vet);
|
||||
return vet;
|
||||
}
|
||||
|
||||
/* 顶点内存释放函数 */
|
||||
void freeVertex(Vertex *val) {
|
||||
// 释放该顶点和该顶点的链表的内存
|
||||
freeLinklist(val->linked);
|
||||
freeLinklist(val->list);
|
||||
free(val);
|
||||
}
|
||||
|
||||
/* 链表 */
|
||||
struct linkList {
|
||||
struct LinkedList {
|
||||
Node *head;
|
||||
Node *tail;
|
||||
};
|
||||
|
||||
/* 链表头插法 */
|
||||
void pushFront(linkList *l, Vertex *val) {
|
||||
void pushFront(LinkedList *list, Vertex *val) {
|
||||
Node *temp = newNode();
|
||||
temp->val = val;
|
||||
temp->next = l->head->next;
|
||||
l->head->next = temp;
|
||||
if (l->tail == l->head) {
|
||||
l->tail = temp;
|
||||
temp->next = list->head->next;
|
||||
list->head->next = temp;
|
||||
if (list->tail == list->head) {
|
||||
list->tail = temp;
|
||||
}
|
||||
}
|
||||
|
||||
/* 链表尾插法 */
|
||||
void pushBack(linkList *l, Vertex *val) {
|
||||
void pushBack(LinkedList *list, Vertex *val) {
|
||||
Node *temp = newNode();
|
||||
temp->val = val;
|
||||
temp->next = 0;
|
||||
l->tail->next = temp;
|
||||
l->tail = temp;
|
||||
list->tail->next = temp;
|
||||
list->tail = temp;
|
||||
}
|
||||
|
||||
/* 根据顶点地址与该顶点连接的删除边 */
|
||||
void removeLink(linkList *l, Vertex *val) {
|
||||
Node *temp = l->head->next;
|
||||
Node *front = l->head;
|
||||
void removeLink(LinkedList *list, Vertex *val) {
|
||||
Node *temp = list->head->next;
|
||||
Node *front = list->head;
|
||||
while (temp != 0) {
|
||||
if (temp->val == val) {
|
||||
front->next = temp->next;
|
||||
if (l->tail == temp) {
|
||||
l->tail = front;
|
||||
if (list->tail == temp) {
|
||||
list->tail = front;
|
||||
}
|
||||
free(temp);
|
||||
return;
|
||||
@@ -97,21 +97,20 @@ void removeLink(linkList *l, Vertex *val) {
|
||||
front = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
if (temp->next == 0) {
|
||||
printf("vertex not found!\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* 根据顶点地址删除顶点 */
|
||||
void removeItem(linkList *l, Vertex *val) {
|
||||
Node *temp = l->head->next;
|
||||
Node *front = l->head;
|
||||
void removeItem(LinkedList *list, Vertex *val) {
|
||||
Node *temp = list->head->next;
|
||||
Node *front = list->head;
|
||||
while (temp != 0) {
|
||||
if (temp->val == val) {
|
||||
front->next = temp->next;
|
||||
if (l->tail == temp) {
|
||||
l->tail = front;
|
||||
if (list->tail == temp) {
|
||||
list->tail = front;
|
||||
}
|
||||
freeVertex(val);
|
||||
free(temp);
|
||||
@@ -120,137 +119,125 @@ void removeItem(linkList *l, Vertex *val) {
|
||||
front = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
if (temp->next == 0) {
|
||||
printf("vertex not found!\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* 释放链表内存 */
|
||||
void freeLinklist(linkList *l) {
|
||||
Node *temp = l->head->next;
|
||||
void freeLinklist(LinkedList *list) {
|
||||
Node *temp = list->head->next;
|
||||
while (temp != 0) {
|
||||
free(l->head);
|
||||
l->head = temp;
|
||||
free(list->head);
|
||||
list->head = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
free(l->head);
|
||||
l->head = 0;
|
||||
free(l);
|
||||
free(list->head);
|
||||
list->head = 0;
|
||||
free(list);
|
||||
}
|
||||
|
||||
/* 链表构造函数 */
|
||||
linkList *newLinklist(Vertex *val) {
|
||||
linkList *newLinklist = (linkList *)malloc(sizeof(linkList));
|
||||
|
||||
LinkedList *newLinklist(Vertex *val) {
|
||||
LinkedList *newLinklist = (LinkedList *)malloc(sizeof(LinkedList));
|
||||
newLinklist->head = newNode();
|
||||
newLinklist->head->val = val;
|
||||
newLinklist->tail = newLinklist->head;
|
||||
newLinklist->head->next = 0;
|
||||
|
||||
return newLinklist;
|
||||
}
|
||||
|
||||
/* 基于邻接链表实现的无向图类结构 */
|
||||
struct graphAdjList {
|
||||
Vertex **verticesList; // 邻接表
|
||||
typedef struct {
|
||||
Vertex **vertices; // 邻接表
|
||||
unsigned int size; // 顶点数量
|
||||
unsigned int capacity; // 顶点容量
|
||||
};
|
||||
|
||||
typedef struct graphAdjList graphAdjList;
|
||||
} GraphAdjList;
|
||||
|
||||
/* 添加边 */
|
||||
void addEdge(graphAdjList *t, int i, int j) {
|
||||
void addEdge(GraphAdjList *graph, int i, int j) {
|
||||
// 越界检查
|
||||
if (i < 0 || j < 0 || i == j || i >= t->size || j >= t->size) {
|
||||
if (i < 0 || j < 0 || i == j || i >= graph->size || j >= graph->size) {
|
||||
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
// 查找欲添加边的顶点 vet1 - vet2
|
||||
Vertex *vet1 = t->verticesList[i];
|
||||
Vertex *vet2 = t->verticesList[j];
|
||||
|
||||
Vertex *vet1 = graph->vertices[i];
|
||||
Vertex *vet2 = graph->vertices[j];
|
||||
// 连接顶点 vet1 - vet2
|
||||
pushBack(vet1->linked, vet2);
|
||||
pushBack(vet2->linked, vet1);
|
||||
pushBack(vet1->list, vet2);
|
||||
pushBack(vet2->list, vet1);
|
||||
}
|
||||
|
||||
/* 删除边 */
|
||||
void removeEdge(graphAdjList *t, int i, int j) {
|
||||
void removeEdge(GraphAdjList *graph, int i, int j) {
|
||||
// 越界检查
|
||||
if (i < 0 || j < 0 || i == j || i >= t->size || j >= t->size) {
|
||||
if (i < 0 || j < 0 || i == j || i >= graph->size || j >= graph->size) {
|
||||
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
|
||||
// 查找欲删除边的顶点 vet1 - vet2
|
||||
Vertex *vet1 = t->verticesList[i];
|
||||
Vertex *vet2 = t->verticesList[j];
|
||||
|
||||
Vertex *vet1 = graph->vertices[i];
|
||||
Vertex *vet2 = graph->vertices[j];
|
||||
// 移除待删除边 vet1 - vet2
|
||||
removeLink(vet1->linked, vet2);
|
||||
removeLink(vet2->linked, vet1);
|
||||
removeLink(vet1->list, vet2);
|
||||
removeLink(vet2->list, vet1);
|
||||
}
|
||||
|
||||
/* 添加顶点 */
|
||||
void addVertex(graphAdjList *t, int val) {
|
||||
void addVertex(GraphAdjList *graph, int val) {
|
||||
// 若大小超过容量,则扩容
|
||||
if (t->size >= t->capacity) {
|
||||
Vertex **tempList = (Vertex **)malloc(sizeof(Vertex *) * 2 * t->capacity);
|
||||
memcpy(tempList, t->verticesList, sizeof(Vertex *) * t->size);
|
||||
free(t->verticesList); // 释放原邻接表内存
|
||||
t->verticesList = tempList; // 指向新邻接表
|
||||
t->capacity = t->capacity * 2; // 容量扩大至2倍
|
||||
if (graph->size >= graph->capacity) {
|
||||
Vertex **tempList = (Vertex **)malloc(sizeof(Vertex *) * 2 * graph->capacity);
|
||||
memcpy(tempList, graph->vertices, sizeof(Vertex *) * graph->size);
|
||||
free(graph->vertices); // 释放原邻接表内存
|
||||
graph->vertices = tempList; // 指向新邻接表
|
||||
graph->capacity = graph->capacity * 2; // 容量扩大至2倍
|
||||
}
|
||||
// 申请新顶点内存并将新顶点地址存入顶点列表
|
||||
Vertex *newV = newVertex(val); // 建立新顶点
|
||||
newV->pos = t->size; // 为新顶点标记下标
|
||||
newV->linked = newLinklist(newV); // 为新顶点建立链表
|
||||
t->verticesList[t->size] = newV; // 将新顶点加入邻接表
|
||||
t->size++;
|
||||
Vertex *newV = newVertex(val); // 建立新顶点
|
||||
newV->pos = graph->size; // 为新顶点标记下标
|
||||
newV->list = newLinklist(newV); // 为新顶点建立链表
|
||||
graph->vertices[graph->size] = newV; // 将新顶点加入邻接表
|
||||
graph->size++;
|
||||
}
|
||||
|
||||
/* 删除顶点 */
|
||||
void removeVertex(graphAdjList *t, unsigned int index) {
|
||||
void removeVertex(GraphAdjList *graph, unsigned int index) {
|
||||
// 越界检查
|
||||
if (index < 0 || index >= t->size) {
|
||||
if (index < 0 || index >= graph->size) {
|
||||
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Vertex *vet = t->verticesList[index]; // 查找待删节点
|
||||
Vertex *vet = graph->vertices[index]; // 查找待删节点
|
||||
if (vet == 0) { // 若不存在该节点,则返回
|
||||
printf("index is:%d\n", index);
|
||||
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
|
||||
// 遍历待删除顶点的链表,将所有与待删除结点有关的边删除
|
||||
Node *temp = vet->linked->head->next;
|
||||
Node *temp = vet->list->head->next;
|
||||
while (temp != 0) {
|
||||
removeLink(temp->val->linked, vet); // 删除与该顶点有关的边
|
||||
removeLink(temp->val->list, vet); // 删除与该顶点有关的边
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
// 将顶点前移
|
||||
for (int i = index; i < t->size - 1; i++) {
|
||||
t->verticesList[i] = t->verticesList[i + 1]; // 顶点前移
|
||||
t->verticesList[i]->pos--; // 所有前移的顶点索引值减1
|
||||
for (int i = index; i < graph->size - 1; i++) {
|
||||
graph->vertices[i] = graph->vertices[i + 1]; // 顶点前移
|
||||
graph->vertices[i]->pos--; // 所有前移的顶点索引值减1
|
||||
}
|
||||
t->verticesList[t->size - 1] = 0; // 将被删除顶点的位置置 0
|
||||
t->size--;
|
||||
|
||||
graph->vertices[graph->size - 1] = 0; // 将被删除顶点的位置置 0
|
||||
graph->size--;
|
||||
// 释放内存
|
||||
freeVertex(vet);
|
||||
}
|
||||
|
||||
/* 打印顶点与邻接矩阵 */
|
||||
void printGraph(graphAdjList *t) {
|
||||
void printGraph(GraphAdjList *graph) {
|
||||
printf("邻接表 =\n");
|
||||
for (int i = 0; i < t->size; i++) {
|
||||
Node *n = t->verticesList[i]->linked->head->next;
|
||||
printf("%d: [", t->verticesList[i]->val);
|
||||
for (int i = 0; i < graph->size; i++) {
|
||||
Node *n = graph->vertices[i]->list->head->next;
|
||||
printf("%d: [", graph->vertices[i]->val);
|
||||
while (n != 0) {
|
||||
if (n->next != 0) {
|
||||
printf("%d, ", n->val->val);
|
||||
@@ -264,14 +251,14 @@ void printGraph(graphAdjList *t) {
|
||||
}
|
||||
|
||||
/* 构造函数 */
|
||||
graphAdjList *newGraphAdjList(unsigned int verticesCapacity) {
|
||||
GraphAdjList *newGraphAdjList(unsigned int verticesCapacity) {
|
||||
// 申请内存
|
||||
graphAdjList *newGraph = (graphAdjList *)malloc(sizeof(graphAdjList));
|
||||
GraphAdjList *newGraph = (GraphAdjList *)malloc(sizeof(GraphAdjList));
|
||||
// 建立顶点表并分配内存
|
||||
newGraph->verticesList = (Vertex **)malloc(sizeof(Vertex *) * verticesCapacity); // 为顶点列表分配内存
|
||||
memset(newGraph->verticesList, 0, sizeof(Vertex *) * verticesCapacity); // 顶点列表置 0
|
||||
newGraph->size = 0; // 初始化顶点数量
|
||||
newGraph->capacity = verticesCapacity; // 初始化顶点容量
|
||||
newGraph->vertices = (Vertex **)malloc(sizeof(Vertex *) * verticesCapacity); // 为顶点列表分配内存
|
||||
memset(newGraph->vertices, 0, sizeof(Vertex *) * verticesCapacity); // 顶点列表置 0
|
||||
newGraph->size = 0; // 初始化顶点数量
|
||||
newGraph->capacity = verticesCapacity; // 初始化顶点容量
|
||||
// 返回图指针
|
||||
return newGraph;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化无向图 */
|
||||
graphAdjList *graph = newGraphAdjList(5);
|
||||
GraphAdjList *graph = newGraphAdjList(5);
|
||||
// 初始化顶点
|
||||
addVertex(graph, 1);
|
||||
addVertex(graph, 3);
|
||||
|
||||
@@ -7,143 +7,133 @@
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 基于邻接矩阵实现的无向图类结构 */
|
||||
struct graphAdjMat {
|
||||
int *vertices; // 顶点列表
|
||||
unsigned int **adjMat; // 邻接矩阵,元素代表“边”,索引代表“顶点索引”
|
||||
unsigned int size; // 顶点数量
|
||||
unsigned int capacity; // 图容量
|
||||
};
|
||||
|
||||
typedef struct graphAdjMat graphAdjMat;
|
||||
typedef struct {
|
||||
int *vertices; // 顶点列表
|
||||
int **adjMat; // 邻接矩阵,元素代表“边”,索引代表“顶点索引”
|
||||
int size; // 顶点数量
|
||||
int capacity; // 图容量
|
||||
} GraphAdjMat;
|
||||
|
||||
/* 添加边 */
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
void addEdge(graphAdjMat *t, int i, int j) {
|
||||
void addEdge(GraphAdjMat *graph, int i, int j) {
|
||||
// 越界检查
|
||||
if (i < 0 || j < 0 || i >= t->size || j >= t->size || i == j) {
|
||||
if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {
|
||||
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
||||
exit(1);
|
||||
}
|
||||
// 添加边
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
t->adjMat[i][j] = 1;
|
||||
t->adjMat[j][i] = 1;
|
||||
graph->adjMat[i][j] = 1;
|
||||
graph->adjMat[j][i] = 1;
|
||||
}
|
||||
|
||||
/* 删除边 */
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
void removeEdge(graphAdjMat *t, int i, int j) {
|
||||
void removeEdge(GraphAdjMat *graph, int i, int j) {
|
||||
// 越界检查
|
||||
if (i < 0 || j < 0 || i >= t->size || j >= t->size || i == j) {
|
||||
if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {
|
||||
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
||||
exit(1);
|
||||
}
|
||||
// 删除边
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
t->adjMat[i][j] = 0;
|
||||
t->adjMat[j][i] = 0;
|
||||
graph->adjMat[i][j] = 0;
|
||||
graph->adjMat[j][i] = 0;
|
||||
}
|
||||
|
||||
/* 添加顶点 */
|
||||
void addVertex(graphAdjMat *t, int val) {
|
||||
void addVertex(GraphAdjMat *graph, int val) {
|
||||
// 如果实际使用不大于预设空间,则直接初始化新空间
|
||||
if (t->size < t->capacity) {
|
||||
t->vertices[t->size] = val; // 初始化新顶点值
|
||||
for (int i = 0; i < t->size; i++) {
|
||||
t->adjMat[i][t->size] = 0; // 邻接矩新列阵置0
|
||||
if (graph->size < graph->capacity) {
|
||||
graph->vertices[graph->size] = val; // 初始化新顶点值
|
||||
for (int i = 0; i < graph->size; i++) {
|
||||
graph->adjMat[i][graph->size] = 0; // 邻接矩新列阵置0
|
||||
}
|
||||
memset(t->adjMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); // 将新增行置 0
|
||||
t->size++;
|
||||
memset(graph->adjMat[graph->size], 0, sizeof(int) * (graph->size + 1)); // 将新增行置 0
|
||||
graph->size++;
|
||||
return;
|
||||
}
|
||||
|
||||
// 扩容,申请新的顶点数组
|
||||
int *temp = (int *)malloc(sizeof(int) * (t->size * 2));
|
||||
memcpy(temp, t->vertices, sizeof(int) * t->size);
|
||||
temp[t->size] = val;
|
||||
|
||||
int *temp = (int *)malloc(sizeof(int) * (graph->size * 2));
|
||||
memcpy(temp, graph->vertices, sizeof(int) * graph->size);
|
||||
temp[graph->size] = val;
|
||||
// 释放原数组
|
||||
free(t->vertices);
|
||||
t->vertices = temp;
|
||||
|
||||
free(graph->vertices);
|
||||
graph->vertices = temp;
|
||||
// 扩容,申请新的二维数组
|
||||
unsigned int **tempMat = (unsigned int **)malloc(sizeof(unsigned int *) * t->size * 2);
|
||||
unsigned int *tempMatLine = (unsigned int *)malloc(sizeof(unsigned int) * (t->size * 2) * (t->size * 2));
|
||||
memset(tempMatLine, 0, sizeof(unsigned int) * (t->size * 2) * (t->size * 2));
|
||||
for (int k = 0; k < t->size * 2; k++) {
|
||||
tempMat[k] = tempMatLine + k * (t->size * 2);
|
||||
int **tempMat = (int **)malloc(sizeof(int *) * graph->size * 2);
|
||||
int *tempMatLine = (int *)malloc(sizeof(int) * (graph->size * 2) * (graph->size * 2));
|
||||
memset(tempMatLine, 0, sizeof(int) * (graph->size * 2) * (graph->size * 2));
|
||||
for (int k = 0; k < graph->size * 2; k++) {
|
||||
tempMat[k] = tempMatLine + k * (graph->size * 2);
|
||||
}
|
||||
|
||||
for (int i = 0; i < t->size; i++) {
|
||||
memcpy(tempMat[i], t->adjMat[i], sizeof(unsigned int) * t->size); // 原数据复制到新数组
|
||||
for (int i = 0; i < graph->size; i++) {
|
||||
memcpy(tempMat[i], graph->adjMat[i], sizeof(int) * graph->size); // 原数据复制到新数组
|
||||
}
|
||||
|
||||
for (int i = 0; i < t->size; i++) {
|
||||
tempMat[i][t->size] = 0; // 将新增列置 0
|
||||
for (int i = 0; i < graph->size; i++) {
|
||||
tempMat[i][graph->size] = 0; // 将新增列置 0
|
||||
}
|
||||
memset(tempMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); // 将新增行置 0
|
||||
|
||||
memset(tempMat[graph->size], 0, sizeof(int) * (graph->size + 1)); // 将新增行置 0
|
||||
// 释放原数组
|
||||
free(t->adjMat[0]);
|
||||
free(t->adjMat);
|
||||
|
||||
free(graph->adjMat[0]);
|
||||
free(graph->adjMat);
|
||||
// 扩容后,指向新地址
|
||||
t->adjMat = tempMat; // 指向新的邻接矩阵地址
|
||||
t->capacity = t->size * 2;
|
||||
t->size++;
|
||||
graph->adjMat = tempMat; // 指向新的邻接矩阵地址
|
||||
graph->capacity = graph->size * 2;
|
||||
graph->size++;
|
||||
}
|
||||
|
||||
/* 删除顶点 */
|
||||
void removeVertex(graphAdjMat *t, unsigned int index) {
|
||||
void removeVertex(GraphAdjMat *graph, int index) {
|
||||
// 越界检查
|
||||
if (index < 0 || index >= t->size) {
|
||||
if (index < 0 || index >= graph->size) {
|
||||
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
||||
exit(1);
|
||||
}
|
||||
for (int i = index; i < t->size - 1; i++) {
|
||||
t->vertices[i] = t->vertices[i + 1]; // 清除删除的顶点,并将其后所有顶点前移
|
||||
for (int i = index; i < graph->size - 1; i++) {
|
||||
graph->vertices[i] = graph->vertices[i + 1]; // 清除删除的顶点,并将其后所有顶点前移
|
||||
}
|
||||
t->vertices[t->size - 1] = 0; // 将被前移的最后一个顶点置 0
|
||||
|
||||
graph->vertices[graph->size - 1] = 0; // 将被前移的最后一个顶点置 0
|
||||
// 清除邻接矩阵中删除的列
|
||||
for (int i = 0; i < t->size - 1; i++) {
|
||||
for (int i = 0; i < graph->size - 1; i++) {
|
||||
if (i < index) {
|
||||
for (int j = index; j < t->size - 1; j++) {
|
||||
t->adjMat[i][j] = t->adjMat[i][j + 1]; // 被删除列后的所有列前移
|
||||
for (int j = index; j < graph->size - 1; j++) {
|
||||
graph->adjMat[i][j] = graph->adjMat[i][j + 1]; // 被删除列后的所有列前移
|
||||
}
|
||||
} else {
|
||||
memcpy(t->adjMat[i], t->adjMat[i + 1], sizeof(unsigned int) * t->size); // 被删除行的下方所有行上移
|
||||
for (int j = index; j < t->size; j++) {
|
||||
t->adjMat[i][j] = t->adjMat[i][j + 1]; // 被删除列后的所有列前移
|
||||
memcpy(graph->adjMat[i], graph->adjMat[i + 1], sizeof(int) * graph->size); // 被删除行的下方所有行上移
|
||||
for (int j = index; j < graph->size; j++) {
|
||||
graph->adjMat[i][j] = graph->adjMat[i][j + 1]; // 被删除列后的所有列前移
|
||||
}
|
||||
}
|
||||
}
|
||||
t->size--;
|
||||
graph->size--;
|
||||
}
|
||||
|
||||
/* 打印顶点与邻接矩阵 */
|
||||
void printGraph(graphAdjMat *t) {
|
||||
if (t->size == 0) {
|
||||
void printGraph(GraphAdjMat *graph) {
|
||||
if (graph->size == 0) {
|
||||
printf("graph is empty\n");
|
||||
return;
|
||||
}
|
||||
printf("顶点列表 = [");
|
||||
for (int i = 0; i < t->size; i++) {
|
||||
if (i != t->size - 1) {
|
||||
printf("%d, ", t->vertices[i]);
|
||||
for (int i = 0; i < graph->size; i++) {
|
||||
if (i != graph->size - 1) {
|
||||
printf("%d, ", graph->vertices[i]);
|
||||
} else {
|
||||
printf("%d", t->vertices[i]);
|
||||
printf("%d", graph->vertices[i]);
|
||||
}
|
||||
}
|
||||
printf("]\n");
|
||||
printf("邻接矩阵 =\n[\n");
|
||||
for (int i = 0; i < t->size; i++) {
|
||||
for (int i = 0; i < graph->size; i++) {
|
||||
printf(" [");
|
||||
for (int j = 0; j < t->size; j++) {
|
||||
if (j != t->size - 1) {
|
||||
printf("%u, ", t->adjMat[i][j]);
|
||||
for (int j = 0; j < graph->size; j++) {
|
||||
if (j != graph->size - 1) {
|
||||
printf("%u, ", graph->adjMat[i][j]);
|
||||
} else {
|
||||
printf("%u", t->adjMat[i][j]);
|
||||
printf("%u", graph->adjMat[i][j]);
|
||||
}
|
||||
}
|
||||
printf("],\n");
|
||||
@@ -152,26 +142,24 @@ void printGraph(graphAdjMat *t) {
|
||||
}
|
||||
|
||||
/* 构造函数 */
|
||||
graphAdjMat *newGraphAjdMat(unsigned int numberVertices, int *vertices, unsigned int **adjMat) {
|
||||
GraphAdjMat *newGraphAjdMat(int numberVertices, int *vertices, int **adjMat) {
|
||||
// 申请内存
|
||||
graphAdjMat *newGraph = (graphAdjMat *)malloc(sizeof(graphAdjMat)); // 为图分配内存
|
||||
newGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); // 为顶点列表分配内存
|
||||
newGraph->adjMat = (unsigned int **)malloc(sizeof(unsigned int *) * numberVertices * 2); // 为邻接矩阵分配二维内存
|
||||
unsigned int *temp = (unsigned int *)malloc(sizeof(unsigned int) * numberVertices * 2 * numberVertices * 2); // 为邻接矩阵分配一维内存
|
||||
newGraph->size = numberVertices; // 初始化顶点数量
|
||||
newGraph->capacity = numberVertices * 2; // 初始化图容量
|
||||
|
||||
GraphAdjMat *newGraph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat)); // 为图分配内存
|
||||
newGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); // 为顶点列表分配内存
|
||||
newGraph->adjMat = (int **)malloc(sizeof(int *) * numberVertices * 2); // 为邻接矩阵分配二维内存
|
||||
int *temp = (int *)malloc(sizeof(int) * numberVertices * 2 * numberVertices * 2); // 为邻接矩阵分配一维内存
|
||||
newGraph->size = numberVertices; // 初始化顶点数量
|
||||
newGraph->capacity = numberVertices * 2; // 初始化图容量
|
||||
// 配置二维数组
|
||||
for (int i = 0; i < numberVertices * 2; i++) {
|
||||
newGraph->adjMat[i] = temp + i * numberVertices * 2; // 将二维指针指向一维数组
|
||||
}
|
||||
|
||||
// 赋值
|
||||
memcpy(newGraph->vertices, vertices, sizeof(int) * numberVertices);
|
||||
for (int i = 0; i < numberVertices; i++) {
|
||||
memcpy(newGraph->adjMat[i], adjMat[i], sizeof(unsigned int) * numberVertices); // 将传入的邻接矩阵赋值给结构体内邻接矩阵
|
||||
memcpy(newGraph->adjMat[i], adjMat[i],
|
||||
sizeof(int) * numberVertices); // 将传入的邻接矩阵赋值给结构体内邻接矩阵
|
||||
}
|
||||
|
||||
// 返回结构体指针
|
||||
return newGraph;
|
||||
}
|
||||
@@ -180,10 +168,10 @@ graphAdjMat *newGraphAjdMat(unsigned int numberVertices, int *vertices, unsigned
|
||||
int main() {
|
||||
/* 初始化无向图 */
|
||||
int vertices[5] = {1, 3, 2, 5, 4};
|
||||
unsigned int **edge = (unsigned int **)malloc(sizeof(unsigned int *) * 5);
|
||||
int **edge = (int **)malloc(sizeof(int *) * 5);
|
||||
// 用于构建二维数组的一维指针
|
||||
unsigned int *temp = (unsigned int *)malloc(sizeof(unsigned int) * 25);
|
||||
memset(temp, 0, sizeof(unsigned int) * 25);
|
||||
int *temp = (int *)malloc(sizeof(int) * 25);
|
||||
memset(temp, 0, sizeof(int) * 25);
|
||||
for (int k = 0; k < 5; k++) {
|
||||
edge[k] = temp + k * 5;
|
||||
}
|
||||
@@ -195,7 +183,7 @@ int main() {
|
||||
edge[2][4] = edge[4][2] = 1;
|
||||
edge[3][4] = edge[4][3] = 1;
|
||||
// 建立无向图
|
||||
graphAdjMat *graph = newGraphAjdMat(5, vertices, edge);
|
||||
GraphAdjMat *graph = newGraphAjdMat(5, vertices, edge);
|
||||
free(edge);
|
||||
free(temp);
|
||||
printf("\n初始化后,图为:\n");
|
||||
|
||||
@@ -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("[");
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user