mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-24 12:06:07 +00:00
add c code for graph operation (#601)
* Create chapter_graph * Delete chapter_graph * add C code for graph * add C code for graph * Create graph_adjacency_list.c add C code for graph * Update CMakeLists.txt * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update graph_adjacency_list.c * Update CMakeLists.txt for c code of graph * Update format of c code * Update format of c code * Update format of c code * Update verticesList Change the data structure of the storage list from a linked list to a linear table * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * Create graph_adjacency_list_test.c * Create graph_bfs * Update CMakeLists.txt * Update graph_adjacency_list.c * mv graph_bfs to graph_bfs.c * Update graph_bfs.c * Delete graph_bfs * Update graph_adjacency_list.c * update c code for graph operation. * Update CMakeLists.txt * Update graph_dfs.c * Update graph_dfs.c * Update CMakeLists.txt * Update graph_dfs.c * Update note of graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update output "初始化后,图为:" of graph_dfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update name of arrayVertex * Update name of arrayVertex * Update note of graph_dfs.c * Update note of graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_adjacency_matrix.c * Update graph_adjacency_list_test.c * Update graph_adjacency_list.c * Update graph_dfs.c * Update graph_bfs.c * Update comment * Update comment * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * update comment * update comment * update comment for graph operation * update comment of graph operation * update comment * update comment --------- Co-authored-by: Yudong Jin <krahets@163.com> Co-authored-by: libr <libr@info2soft.com>
This commit is contained in:
@@ -41,11 +41,11 @@ struct Vertex {
|
||||
|
||||
/* 顶点节点构造函数 */
|
||||
Vertex *newVertex(int val) {
|
||||
Vertex *v = (Vertex *)malloc(sizeof(Vertex));
|
||||
Vertex *vet = (Vertex *)malloc(sizeof(Vertex));
|
||||
// 为新节点赋值并建立该节点的链表
|
||||
v->val = val;
|
||||
v->linked = newLinklist(v);
|
||||
return v;
|
||||
vet->val = val;
|
||||
vet->linked = newLinklist(vet);
|
||||
return vet;
|
||||
}
|
||||
|
||||
/* 顶点内存释放函数 */
|
||||
@@ -103,25 +103,6 @@ void removeLink(linkList *l, Vertex *val) {
|
||||
}
|
||||
}
|
||||
|
||||
/* 根据索引查找链表中节点 */
|
||||
Node *findByindex(linkList *l, unsigned int index) {
|
||||
unsigned int i = 0;
|
||||
Node *temp = l->head->next;
|
||||
while (temp != 0) {
|
||||
if (i == index) {
|
||||
return temp;
|
||||
}
|
||||
temp = temp->next;
|
||||
i++;
|
||||
}
|
||||
|
||||
if (temp->next == 0) {
|
||||
printf("vertex not found!\n");
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 根据顶点地址删除顶点 */
|
||||
void removeNode(linkList *l, Vertex *val) {
|
||||
Node *temp = l->head->next;
|
||||
@@ -172,12 +153,9 @@ linkList *newLinklist(Vertex *val) {
|
||||
|
||||
/* 基于邻接链表实现的无向图类结构 */
|
||||
struct graphAdjList {
|
||||
// 顶点列表
|
||||
Vertex **verticesList;
|
||||
// 顶点数量
|
||||
unsigned int size;
|
||||
// 当前容量
|
||||
unsigned int capacity;
|
||||
Vertex **verticesList; // 邻接表
|
||||
unsigned int size; // 顶点数量
|
||||
unsigned int capacity; // 顶点容量
|
||||
};
|
||||
|
||||
typedef struct graphAdjList graphAdjList;
|
||||
@@ -189,13 +167,13 @@ void addEdge(graphAdjList *t, int i, int j) {
|
||||
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
// 查找待连接的节点
|
||||
Vertex *v1 = t->verticesList[i];
|
||||
Vertex *v2 = t->verticesList[j];
|
||||
// 查找欲添加边的顶点 vet1 - vet2
|
||||
Vertex *vet1 = t->verticesList[i];
|
||||
Vertex *vet2 = t->verticesList[j];
|
||||
|
||||
// 连接节点
|
||||
pushBack(v1->linked, v2);
|
||||
pushBack(v2->linked, v1);
|
||||
// 连接顶点 vet1 - vet2
|
||||
pushBack(vet1->linked, vet2);
|
||||
pushBack(vet2->linked, vet1);
|
||||
}
|
||||
|
||||
/* 删除边 */
|
||||
@@ -206,13 +184,13 @@ void removeEdge(graphAdjList *t, int i, int j) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 查找待删除边的相关节点
|
||||
Vertex *v1 = t->verticesList[i];
|
||||
Vertex *v2 = t->verticesList[j];
|
||||
// 查找欲删除边的顶点 vet1 - vet2
|
||||
Vertex *vet1 = t->verticesList[i];
|
||||
Vertex *vet2 = t->verticesList[j];
|
||||
|
||||
// 移除待删除边
|
||||
removeLink(v1->linked, v2);
|
||||
removeLink(v2->linked, v1);
|
||||
// 移除待删除边 vet1 - vet2
|
||||
removeLink(vet1->linked, vet2);
|
||||
removeLink(vet2->linked, vet1);
|
||||
}
|
||||
|
||||
/* 添加顶点 */
|
||||
@@ -221,16 +199,15 @@ void addVertex(graphAdjList *t, 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;
|
||||
free(t->verticesList); // 释放原邻接表内存
|
||||
t->verticesList = tempList; // 指向新邻接表
|
||||
t->capacity = t->capacity * 2; // 容量扩大至2倍
|
||||
}
|
||||
// 申请新顶点内存并将新顶点地址存入顶点列表
|
||||
Vertex *newV = newVertex(val);
|
||||
newV->pos = t->size;
|
||||
newV->linked = newLinklist(newV);
|
||||
t->verticesList[t->size] = newV;
|
||||
Vertex *newV = newVertex(val); // 建立新顶点
|
||||
newV->pos = t->size; // 为新顶点标记下标
|
||||
newV->linked = newLinklist(newV); // 为新顶点建立链表
|
||||
t->verticesList[t->size] = newV; // 将新顶点加入邻接表
|
||||
t->size++;
|
||||
}
|
||||
|
||||
@@ -242,31 +219,30 @@ void removeVertex(graphAdjList *t, unsigned int index) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// 查找待删节点
|
||||
Vertex *v = t->verticesList[index];
|
||||
// 若不存在该节点,则返回
|
||||
if (v == 0) {
|
||||
Vertex *vet = t->verticesList[index]; // 查找待删节点
|
||||
if (vet == 0) { // 若不存在该节点,则返回
|
||||
printf("index is:%d\n", index);
|
||||
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
|
||||
// 遍历待删除节点链表,将所有与待删除结点有关的边删除
|
||||
Node *temp = v->linked->head->next;
|
||||
// 遍历待删除顶点的链表,将所有与待删除结点有关的边删除
|
||||
Node *temp = vet->linked->head->next;
|
||||
while (temp != 0) {
|
||||
removeLink(temp->val->linked, v);
|
||||
temp = temp->next;
|
||||
removeLink(temp->val->linked, vet); // 删除与该顶点有关的边
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
// 定点列表前移
|
||||
// 将顶点前移
|
||||
for (int i = index; i < t->size - 1; i++) {
|
||||
t->verticesList[i] = t->verticesList[i + 1];
|
||||
t->verticesList[i] = t->verticesList[i + 1]; // 顶点前移
|
||||
t->verticesList[i]->pos--; // 所有前移的顶点索引值减1
|
||||
}
|
||||
t->verticesList[t->size - 1] = 0;
|
||||
t->verticesList[t->size - 1] = 0; // 将被删除顶点的位置置 0
|
||||
t->size--;
|
||||
|
||||
//释放被删除顶点的内存
|
||||
freeVertex(v);
|
||||
freeVertex(vet);
|
||||
}
|
||||
|
||||
/* 打印顶点与邻接矩阵 */
|
||||
@@ -288,14 +264,14 @@ void printGraph(graphAdjList *t) {
|
||||
}
|
||||
|
||||
/* 构造函数 */
|
||||
graphAdjList *newGraphic(unsigned int verticesNumber) {
|
||||
graphAdjList *newGraphAdjList(unsigned int verticesCapacity) {
|
||||
// 申请内存
|
||||
graphAdjList *newGraph = (graphAdjList *)malloc(sizeof(graphAdjList));
|
||||
// 建立顶点表并分配内存
|
||||
newGraph->verticesList = (Vertex **)malloc(sizeof(Vertex *) * verticesNumber);
|
||||
memset(newGraph->verticesList, 0, sizeof(Vertex *) * verticesNumber);
|
||||
// 初始化大小和容量
|
||||
newGraph->size = 0;
|
||||
newGraph->capacity = verticesNumber;
|
||||
return newGraph;
|
||||
newGraph->verticesList = (Vertex **)malloc(sizeof(Vertex *) * verticesCapacity); // 为顶点列表分配内存
|
||||
memset(newGraph->verticesList, 0, sizeof(Vertex *) * verticesCapacity); // 顶点列表置 0
|
||||
newGraph->size = 0; // 初始化顶点数量
|
||||
newGraph->capacity = verticesCapacity; // 初始化顶点容量
|
||||
// 返回图指针
|
||||
return newGraph;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user