C语言编程,分别以邻接矩阵和邻接表作为存储结构,实现以下图的基本操作: 1.增加一个新顶点v,InsertVex(G, v); 2.删除顶点v及其相关的边,DeleteVex(G, v); 3.增加一条边<v,w>,InsertArc(G, v, w); 4.删除一条边<v,w>,DeleteArc(G, v, w)。
时间: 2024-03-19 08:43:49 浏览: 136
好的,我会为您解答这个问题。
首先,我们需要定义邻接矩阵和邻接表的结构体:
```
// 邻接矩阵结构体
typedef struct {
int vertex[MAX_VERTEX_NUM]; // 存储顶点信息
int arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 存储边信息
int vexnum; // 图中当前顶点数
int arcnum; // 图中当前边数
} MGraph;
// 邻接表结构体
typedef struct ArcNode {
int adjvex; // 该边所指向的顶点的位置
struct ArcNode *nextarc; // 指向下一条边的指针
int info; // 存储边的权值
} ArcNode;
typedef struct VNode {
int data; // 存储顶点信息
ArcNode *firstarc; // 指向第一条依附该顶点的边的指针
} VNode, AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjList vertices; // 存储图中所有顶点的邻接表
int vexnum; // 图中当前顶点数
int arcnum; // 图中当前边数
} ALGraph;
```
接下来,我们可以实现题目要求的基本操作。
1.增加一个新顶点v,InsertVex(G, v):
```
// 邻接矩阵实现
void InsertVex(MGraph *G, int v) {
if (G->vexnum == MAX_VERTEX_NUM) {
printf("Error: the graph is full!\n");
return;
}
G->vertex[G->vexnum] = v;
for (int i = 0; i <= G->vexnum; i++) {
G->arc[G->vexnum][i] = 0;
G->arc[i][G->vexnum] = 0;
}
G->vexnum++;
}
// 邻接表实现
void InsertVex(ALGraph *G, int v) {
if (G->vexnum == MAX_VERTEX_NUM) {
printf("Error: the graph is full!\n");
return;
}
G->vertices[G->vexnum].data = v;
G->vertices[G->vexnum].firstarc = NULL;
G->vexnum++;
}
```
2.删除顶点v及其相关的边,DeleteVex(G, v):
```
// 邻接矩阵实现
void DeleteVex(MGraph *G, int v) {
int i, j;
for (i = 0; i < G->vexnum; i++) {
if (G->vertex[i] == v) {
for (j = i; j < G->vexnum - 1; j++) {
G->vertex[j] = G->vertex[j + 1];
}
G->vexnum--;
break;
}
}
if (i == G->vexnum) {
printf("Error: the vertex is not found!\n");
return;
}
for (j = 0; j < G->vexnum; j++) {
if (G->arc[i][j] != 0) {
G->arcnum--;
}
for (int k = i; k < G->vexnum - 1; k++) {
G->arc[k][j] = G->arc[k + 1][j];
}
}
for (j = 0; j < G->vexnum; j++) {
if (G->arc[j][i] != 0) {
G->arcnum--;
}
for (int k = i; k < G->vexnum - 1; k++) {
G->arc[j][k] = G->arc[j][k + 1];
}
}
}
// 邻接表实现
void DeleteVex(ALGraph *G, int v) {
int i, j;
ArcNode *p, *q;
for (i = 0; i < G->vexnum; i++) {
if (G->vertices[i].data == v) {
p = G->vertices[i].firstarc;
while (p != NULL) {
q = p;
p = p->nextarc;
G->arcnum--;
free(q);
}
for (j = i; j < G->vexnum - 1; j++) {
G->vertices[j] = G->vertices[j + 1];
}
G->vexnum--;
break;
}
}
if (i == G->vexnum) {
printf("Error: the vertex is not found!\n");
return;
}
for (i = 0; i < G->vexnum; i++) {
p = G->vertices[i].firstarc;
while (p != NULL) {
if (p->adjvex == v) {
q->nextarc = p->nextarc;
G->arcnum--;
free(p);
p = q->nextarc;
} else {
q = p;
p = p->nextarc;
}
}
}
}
```
3.增加一条边<v,w>,InsertArc(G, v, w):
```
// 邻接矩阵实现
void InsertArc(MGraph *G, int v, int w) {
int i, j;
for (i = 0; i < G->vexnum; i++) {
if (G->vertex[i] == v) {
break;
}
}
for (j = 0; j < G->vexnum; j++) {
if (G->vertex[j] == w) {
break;
}
}
if (i == G->vexnum || j == G->vexnum) {
printf("Error: the vertex is not found!\n");
return;
}
if (G->arc[i][j] == 0) {
G->arc[i][j] = 1;
G->arcnum++;
}
}
// 邻接表实现
void InsertArc(ALGraph *G, int v, int w) {
int i, j;
ArcNode *p;
for (i = 0; i < G->vexnum; i++) {
if (G->vertices[i].data == v) {
break;
}
}
for (j = 0; j < G->vexnum; j++) {
if (G->vertices[j].data == w) {
break;
}
}
if (i == G->vexnum || j == G->vexnum) {
printf("Error: the vertex is not found!\n");
return;
}
p = G->vertices[i].firstarc;
while (p != NULL) {
if (p->adjvex == j) {
printf("Error: the arc already exists!\n");
return;
}
p = p->nextarc;
}
p = (ArcNode *)malloc(sizeof(ArcNode));
p->adjvex = j;
p->nextarc = G->vertices[i].firstarc;
G->vertices[i].firstarc = p;
G->arcnum++;
}
```
4.删除一条边<v,w>,DeleteArc(G, v, w):
```
// 邻接矩阵实现
void DeleteArc(MGraph *G, int v, int w) {
int i, j;
for (i = 0; i < G->vexnum; i++) {
if (G->vertex[i] == v) {
break;
}
}
for (j = 0; j < G->vexnum; j++) {
if (G->vertex[j] == w) {
break;
}
}
if (i == G->vexnum || j == G->vexnum) {
printf("Error: the vertex is not found!\n");
return;
}
if (G->arc[i][j] == 0) {
printf("Error: the arc is not found!\n");
return;
}
G->arc[i][j] = 0;
G->arcnum--;
}
// 邻接表实现
void DeleteArc(ALGraph *G, int v, int w) {
int i, j;
ArcNode *p, *q;
for (i = 0; i < G->vexnum; i++) {
if (G->vertices[i].data == v) {
break;
}
}
for (j = 0; j < G->vexnum; j++) {
if (G->vertices[j].data == w) {
break;
}
}
if (i == G->vexnum || j == G->vexnum) {
printf("Error: the vertex is not found!\n");
return;
}
p = G->vertices[i].firstarc;
q = NULL;
while (p != NULL) {
if (p->adjvex == j) {
if (q == NULL) {
G->vertices[i].firstarc = p->nextarc;
} else {
q->nextarc = p->nextarc;
}
G->arcnum--;
free(p);
break;
}
q = p;
p = p->nextarc;
}
if (p == NULL) {
printf("Error: the arc is not found!\n");
}
}
```
以上就是以邻接矩阵和邻接表作为存储结构,实现基本操作的 C 语言代码。
阅读全文