完善代码:void AddEdge(Graph* G,char v1,char v2,int weight) { ArcNode* p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = v2; p->weight = weight; // 添加权值 p->next = G->vertices[v1].firstarc; G->vertices[v1].firstarc = p; ++G->arcnum; p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = v1; p->weight = weight; // 添加权值 p->next = G->vertices[v2].firstarc; G->vertices[v2].firstarc = p; ++G->arcnum; }
时间: 2024-03-19 15:41:53 浏览: 62
void AddEdge(Graph* G, char v1, char v2, int weight) {
ArcNode* p1 = (ArcNode*)malloc(sizeof(ArcNode));
p1->adjvex = v2;
p1->weight = weight;
p1->next = G->vertices[v1].firstarc;
G->vertices[v1].firstarc = p1;
++G->arcnum;
ArcNode* p2 = (ArcNode*)malloc(sizeof(ArcNode));
p2->adjvex = v1;
p2->weight = weight;
p2->next = G->vertices[v2].firstarc;
G->vertices[v2].firstarc = p2;
++G->arcnum;
}
在原有代码上,增加了 ArcNode* p2 指针,用于添加双向边。在第一段代码中,我们添加了指向 v2 的边,而在第二段代码中,我们添加了指向 v1 的边。这样就可以确保两个顶点之间的边是双向的。
相关问题
在下列添加边的代码中增加边的权值:// 添加边 void AddEdge(Graph* G, int v1, int v2) { ArcNode* p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = v2; p->next = G->vertices[v1].firstarc; G->vertices[v1].firstarc = p; ++G->arcnum; p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = v1; p->next = G->vertices[v2].firstarc; G->vertices[v2].firstarc = p; ++G->arcnum; }
可以在代码中增加一个变量来表示边的权值,例如我们可以在ArcNode结构体中增加一个weight变量,然后在AddEdge函数中对这个变量进行赋值即可。
修改后的代码如下:
```
// 添加边
void AddEdge(Graph* G, int v1, int v2, int weight) {
ArcNode* p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = v2;
p->weight = weight; // 添加权值
p->next = G->vertices[v1].firstarc;
G->vertices[v1].firstarc = p;
++G->arcnum;
p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = v1;
p->weight = weight; // 添加权值
p->next = G->vertices[v2].firstarc;
G->vertices[v2].firstarc = p;
++G->arcnum;
}
```
在调用AddEdge函数时,需要传入一个表示边权值的参数。例如,如果要添加从v1到v2的权值为3的边,可以这样调用:
```
AddEdge(&G, v1, v2, 3);
```
将下列代码改成六个结点10条边的无向图:#include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 100 // 最大结点数 typedef struct ArcNode { // 弧结点类型 int adjvex; // 邻接点在顶点数组中的下标 struct ArcNode* next; // 指向下一个邻接点的指针 } ArcNode; typedef struct VertexNode { // 顶点类型 char data; // 顶点信息 ArcNode* firstarc; // 指向第一个邻接点的指针 } VertexNode, AdjList[MAX_VERTEX_NUM]; typedef struct Graph { // 图类型 AdjList vertices; // 顶点数组 int vexnum, arcnum; // 顶点数、弧数 } Graph; // 初始化图 void InitGraph(Graph* G) { G->vexnum = G->arcnum = 0; for (int i = 0; i < MAX_VERTEX_NUM; ++i) { G->vertices[i].data = '\0'; G->vertices[i].firstarc = NULL; } } // 添加结点 void AddVertex(Graph* G, char ch) { G->vertices[G->vexnum].data = ch; ++G->vexnum; } // 添加边 void AddEdge(Graph* G, int v1, int v2) { ArcNode* p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = v2; p->next = G->vertices[v1].firstarc; G->vertices[v1].firstarc = p; ++G->arcnum; } // 输出每个结点的度 void PrintDegree(Graph* G) { for (int i = 0; i < G->vexnum; ++i) { int degree = 0; ArcNode* p = G->vertices[i].firstarc; while (p) { ++degree; p = p->next; } printf("结点%c的度为%d\n", G->vertices[i].data, degree); } } int main() { Graph G; InitGraph(&G); AddVertex(&G, 'A'); AddVertex(&G, 'B'); AddVertex(&G, 'C'); AddVertex(&G, 'D'); AddEdge(&G, 0, 1); AddEdge(&G, 0, 2); AddEdge(&G, 1, 2); AddEdge(&G, 2, 0); AddEdge(&G, 2, 3); AddEdge(&G, 3, 3); PrintDegree(&G); return 0; }
将代码改成六个结点十条边的无向图,可以按照以下方式修改:
1. 修改结点数为6,即将MAX_VERTEX_NUM改为6,同时在InitGraph函数中将G->vexnum初始值改为6。
2. 根据题目要求,顶点分别为A、B、C、D、E、F。因此在main函数中添加以下语句:
```
AddVertex(&G, 'A');
AddVertex(&G, 'B');
AddVertex(&G, 'C');
AddVertex(&G, 'D');
AddVertex(&G, 'E');
AddVertex(&G, 'F');
```
3. 添加边的过程中,注意无向图的边是双向的,因此需要添加两条边。具体来说,添加边的代码如下:
```
AddEdge(&G, 0, 1); // A-B
AddEdge(&G, 1, 0); // B-A
AddEdge(&G, 0, 2); // A-C
AddEdge(&G, 2, 0); // C-A
AddEdge(&G, 1, 2); // B-C
AddEdge(&G, 2, 1); // C-B
AddEdge(&G, 2, 3); // C-D
AddEdge(&G, 3, 2); // D-C
AddEdge(&G, 3, 4); // D-E
AddEdge(&G, 4, 3); // E-D
```
4. 最后,修改输出语句中的结点名称为题目给定的名称即可。修改后的代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 6 // 最大结点数
typedef struct ArcNode { // 弧结点类型
int adjvex; // 邻接点在顶点数组中的下标
struct ArcNode* next; // 指向下一个邻接点的指针
} ArcNode;
typedef struct VertexNode { // 顶点类型
char data; // 顶点信息
ArcNode* firstarc; // 指向第一个邻接点的指针
} VertexNode, AdjList[MAX_VERTEX_NUM];
typedef struct Graph { // 图类型
AdjList vertices; // 顶点数组
int vexnum, arcnum; // 顶点数、弧数
} Graph;
// 初始化图
void InitGraph(Graph* G) {
G->vexnum = 6;
G->arcnum = 0;
for (int i = 0; i < MAX_VERTEX_NUM; ++i) {
G->vertices[i].data = '\0';
G->vertices[i].firstarc = NULL;
}
}
// 添加结点
void AddVertex(Graph* G, char ch) {
G->vertices[G->vexnum].data = ch;
++G->vexnum;
}
// 添加边
void AddEdge(Graph* G, int v1, int v2) {
ArcNode* p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = v2;
p->next = G->vertices[v1].firstarc;
G->vertices[v1].firstarc = p;
++G->arcnum;
p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = v1;
p->next = G->vertices[v2].firstarc;
G->vertices[v2].firstarc = p;
++G->arcnum;
}
// 输出每个结点的度
void PrintDegree(Graph* G) {
for (int i = 0; i < G->vexnum; ++i) {
int degree = 0;
ArcNode* p = G->vertices[i].firstarc;
while (p) {
++degree;
p = p->next;
}
printf("结点%c的度为%d\n", G->vertices[i].data, degree);
}
}
int main() {
Graph G;
InitGraph(&G);
AddVertex(&G, 'A');
AddVertex(&G, 'B');
AddVertex(&G, 'C');
AddVertex(&G, 'D');
AddVertex(&G, 'E');
AddVertex(&G, 'F');
AddEdge(&G, 0, 1); // A-B
AddEdge(&G, 1, 0); // B-A
AddEdge(&G, 0, 2); // A-C
AddEdge(&G, 2, 0); // C-A
AddEdge(&G, 1, 2); // B-C
AddEdge(&G, 2, 1); // C-B
AddEdge(&G, 2, 3); // C-D
AddEdge(&G, 3, 2); // D-C
AddEdge(&G, 3, 4); // D-E
AddEdge(&G, 4, 3); // E-D
PrintDegree(&G);
return 0;
}
```
阅读全文