void print(Graph *g) { int i,j; printf("顶点数量为:%d\n",g->num_vertices); printf("图的边为:\n"); for(i=0;i<g->num_vertices;i++); { for(j=0;j<g->num_vertices;j++) { printf("%d",g->edges[i][j]); } } }
时间: 2024-01-12 21:03:47 浏览: 43
这段代码中的问题是在第一个 for 循环中,该循环结尾有一个分号,这会导致循环体为空,导致程序无法输出任何内容。应该删除分号,将循环体放在大括号内。正确的代码如下:
```c
void print(Graph *g) {
int i,j;
printf("顶点数量为:%d\n",g->num_vertices);
printf("图的边为:\n");
for(i=0;i<g->num_vertices;i++) {
for(j=0;j<g->num_vertices;j++) {
printf("%d",g->edges[i][j]);
}
}
}
```
相关问题
翻译以下句子:void print_graph(Graph *graph) { for (int i = 0; i < graph->num_vertices; i++) { GraphNode *current_node = graph->adj_list[i]; printf("Vertex %d: ", i); while (current_node != NULL) { printf("%d ", current_node->vertex); current_node = current_node->next; } printf("\n"); } }
void print_graph(Graph *graph) {
// 遍历所有顶点
for (int i = 0; i < graph->num_vertices; i++) {
GraphNode *current_node = graph->adj_list[i];
printf("Vertex %d: ", i);
// 遍历与该顶点相邻的所有节点
while (current_node != NULL) {
printf("%d ", current_node->vertex);
current_node = current_node->next;
}
printf("\n");
}
}
该函数用于打印一个图,其中Graph是图的结构体,num_vertices表示图中顶点的数量,adj_list是一个指针数组,每个指针指向一个链表,链表中存储了与相应顶点相邻的所有节点。函数首先遍历所有顶点,然后对于每个顶点,遍历与之相邻的所有节点并打印出来。
请用C语言编写程序创建一个有向图。有向图中包含n个顶点,编号为0至n-1
以下是用 C 语言创建有向图的程序:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100 // 最大顶点数
typedef struct Node {
int vertex; // 顶点编号
struct Node* next; // 指向下一个节点的指针
} Node;
typedef struct Graph {
int num_vertices; // 顶点数
Node* adj_list[MAX_VERTICES]; // 邻接链表数组
} Graph;
// 创建一个节点
Node* create_node(int vertex) {
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->vertex = vertex;
new_node->next = NULL;
return new_node;
}
// 添加一条有向边
void add_edge(Graph* graph, int src, int dest) {
Node* new_node = create_node(dest);
new_node->next = graph->adj_list[src];
graph->adj_list[src] = new_node;
}
// 打印有向图
void print_graph(Graph* graph) {
for (int i = 0; i < graph->num_vertices; i++) {
printf("%d -> ", i);
Node* temp = graph->adj_list[i];
while (temp) {
printf("%d -> ", temp->vertex);
temp = temp->next;
}
printf("NULL\n");
}
}
int main() {
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->num_vertices = 4;
for (int i = 0; i < graph->num_vertices; i++) {
graph->adj_list[i] = NULL;
}
add_edge(graph, 0, 1);
add_edge(graph, 1, 2);
add_edge(graph, 2, 3);
add_edge(graph, 3, 0);
print_graph(graph);
return 0;
}
```
这个程序创建了一个有向图,其中有 4 个顶点,编号为 0 至 3。然后,它添加了一些有向边,并打印了这个有向图。你可以根据自己的需求修改顶点数和边的数量。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)