建立图并输出,输入1是有向图,输入0是无向图用C语言实现
时间: 2024-03-22 10:37:25 浏览: 51
以下是用 C 语言实现建立图并输出的示例代码,包括有向图和无向图:
有向图:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
typedef struct GraphNode {
int vertex;
struct GraphNode *next;
} GraphNode;
typedef struct {
GraphNode *head;
} Vertex;
typedef struct {
Vertex vertices[MAX_VERTICES];
int vertex_count;
} Graph;
void init_graph(Graph *g) {
g->vertex_count = 0;
}
void add_vertex(Graph *g) {
if (g->vertex_count < MAX_VERTICES) {
g->vertices[g->vertex_count++].head = NULL;
}
else {
printf("Graph full, cannot add vertex.\n");
}
}
void add_edge(Graph *g, int src, int dest) {
if (src >= g->vertex_count || dest >= g->vertex_count) {
printf("Vertex does not exist.\n");
}
else {
GraphNode *new_node = (GraphNode*)malloc(sizeof(GraphNode));
new_node->vertex = dest;
new_node->next = g->vertices[src].head;
g->vertices[src].head = new_node;
}
}
void display_graph(Graph *g) {
int i;
for (i = 0; i < g->vertex_count; ++i) {
GraphNode *ptr = g->vertices[i].head;
printf("Adjacency list of vertex %d:\n", i);
while (ptr != NULL) {
printf("%d -> ", ptr->vertex);
ptr = ptr->next;
}
printf("NULL\n");
}
}
int main() {
Graph g;
int i, src, dest, edges, vertices;
init_graph(&g);
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
for (i = 0; i < vertices; ++i) {
add_vertex(&g);
}
printf("Enter the number of edges: ");
scanf("%d", &edges);
for (i = 0; i < edges; ++i) {
printf("Enter edge %d (src dest): ", i+1);
scanf("%d %d", &src, &dest);
add_edge(&g, src, dest);
}
display_graph(&g);
return 0;
}
```
无向图:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
typedef struct GraphNode {
int vertex;
struct GraphNode *next;
} GraphNode;
typedef struct {
GraphNode *head;
} Vertex;
typedef struct {
Vertex vertices[MAX_VERTICES];
int vertex_count;
} Graph;
void init_graph(Graph *g) {
g->vertex_count = 0;
}
void add_vertex(Graph *g) {
if (g->vertex_count < MAX_VERTICES) {
g->vertices[g->vertex_count++].head = NULL;
}
else {
printf("Graph full, cannot add vertex.\n");
}
}
void add_edge(Graph *g, int src, int dest) {
if (src >= g->vertex_count || dest >= g->vertex_count) {
printf("Vertex does not exist.\n");
}
else {
GraphNode *new_node = (GraphNode*)malloc(sizeof(GraphNode));
new_node->vertex = dest;
new_node->next = g->vertices[src].head;
g->vertices[src].head = new_node;
new_node = (GraphNode*)malloc(sizeof(GraphNode));
new_node->vertex = src;
new_node->next = g->vertices[dest].head;
g->vertices[dest].head = new_node;
}
}
void display_graph(Graph *g) {
int i;
for (i = 0; i < g->vertex_count; ++i) {
GraphNode *ptr = g->vertices[i].head;
printf("Adjacency list of vertex %d:\n", i);
while (ptr != NULL) {
printf("%d -> ", ptr->vertex);
ptr = ptr->next;
}
printf("NULL\n");
}
}
int main() {
Graph g;
int i, src, dest, edges, vertices;
init_graph(&g);
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
for (i = 0; i < vertices; ++i) {
add_vertex(&g);
}
printf("Enter the number of edges: ");
scanf("%d", &edges);
for (i = 0; i < edges; ++i) {
printf("Enter edge %d (src dest): ", i+1);
scanf("%d %d", &src, &dest);
add_edge(&g, src, dest);
}
display_graph(&g);
return 0;
}
```
阅读全文