基于邻接矩阵存储结构实现有向网络的典型操作(初始化、销毁、增加顶点、删除顶点、增加弧、删除弧、查 找一个顶点、修改一个顶点、查找一个弧、修改一个弧、判空、判满、图中顶点个数、图中的弧个数、图的输出、深 度优先遍历、广度优先遍历),用c语言表示
时间: 2023-09-15 08:18:54 浏览: 55
数据结构-c语言-带main函数-图7.2-图的存储结构-图的邻接矩阵的创建-有向图。
5星 · 资源好评率100%
以下是使用C语言表示基于邻接矩阵存储结构的有向网络的典型操作的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
typedef struct {
int matrix[MAX_VERTICES][MAX_VERTICES];
int numVertices;
} DirectedGraph;
void initializeGraph(DirectedGraph* graph, int numVertices) {
graph->numVertices = numVertices;
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
graph->matrix[i][j] = 0;
}
}
}
void destroyGraph(DirectedGraph* graph) {
graph->numVertices = 0;
}
void addVertex(DirectedGraph* graph) {
if (graph->numVertices < MAX_VERTICES) {
graph->numVertices++;
} else {
printf("Graph is full.\n");
}
}
void deleteVertex(DirectedGraph* graph, int vertex) {
if (vertex >= 0 && vertex < graph->numVertices) {
for (int i = 0; i < graph->numVertices; i++) {
graph->matrix[i][vertex] = 0;
graph->matrix[vertex][i] = 0;
}
for (int i = vertex; i < graph->numVertices - 1; i++) {
for (int j = 0; j < graph->numVertices; j++) {
graph->matrix[j][i] = graph->matrix[j][i + 1];
}
}
for (int i = vertex; i < graph->numVertices - 1; i++) {
for (int j = 0; j < graph->numVertices; j++) {
graph->matrix[i][j] = graph->matrix[i + 1][j];
}
}
graph->numVertices--;
} else {
printf("Invalid vertex.\n");
}
}
void addArc(DirectedGraph* graph, int src, int dest) {
if (src >= 0 && src < graph->numVertices && dest >= 0 && dest < graph->numVertices) {
graph->matrix[src][dest] = 1;
} else {
printf("Invalid vertices.\n");
}
}
void deleteArc(DirectedGraph* graph, int src, int dest) {
if (src >= 0 && src < graph->numVertices && dest >= 0 && dest < graph->numVertices) {
graph->matrix[src][dest] = 0;
} else {
printf("Invalid vertices.\n");
}
}
int findVertex(DirectedGraph* graph, int vertex) {
if (vertex >= 0 && vertex < graph->numVertices) {
return 1;
} else {
return 0;
}
}
void modifyVertex(DirectedGraph* graph, int vertex, int newValue) {
if (vertex >= 0 && vertex < graph->numVertices) {
// Modify the vertex value here
} else {
printf("Invalid vertex.\n");
}
}
int findArc(DirectedGraph* graph, int src, int dest) {
if (src >= 0 && src < graph->numVertices && dest >= 0 && dest < graph->numVertices) {
if (graph->matrix[src][dest] == 1) {
return 1;
} else {
return 0;
}
} else {
printf("Invalid vertices.\n");
return 0;
}
}
void modifyArc(DirectedGraph* graph, int src, int dest, int newValue) {
if (src >= 0 && src < graph->numVertices && dest >= 0 && dest < graph->numVertices) {
// Modify the arc value here
} else {
printf("Invalid vertices.\n");
}
}
int isEmpty(DirectedGraph* graph) {
if (graph->numVertices == 0) {
return 1;
} else {
return 0;
}
}
int isFull(DirectedGraph* graph) {
if (graph->numVertices == MAX_VERTICES) {
return 1;
} else {
return 0;
}
}
int getNumVertices(DirectedGraph* graph) {
return graph->numVertices;
}
int getNumArcs(DirectedGraph* graph) {
int c
阅读全文