C 图:关于邻接矩阵
我必须写一个程序,它将以邻接列表和邻接矩阵的形式显示图形结果。我已经通过一个关于YT的教程来指导自己如何实现邻接列表,并使用当前存储的数据(用户介绍自己的位置,如节点数、边数、边连接的边数以及连接的边数),我想知道/理解如何使用已存储的数据构建邻接矩阵C 图:关于邻接矩阵,c,pointers,matrix,graph,C,Pointers,Matrix,Graph,我必须写一个程序,它将以邻接列表和邻接矩阵的形式显示图形结果。我已经通过一个关于YT的教程来指导自己如何实现邻接列表,并使用当前存储的数据(用户介绍自己的位置,如节点数、边数、边连接的边数以及连接的边数),我想知道/理解如何使用已存储的数据构建邻接矩阵 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; void read_graf(st
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void read_graf(struct node *ad[], int no_of_nodes);
void print_graf(struct node *ad[], int no_of_nodes);
int main()
{
int op;
int i,j,k, nodes;
begin:
printf("Type in the number of edges: ");
scanf("%d", &nodes);
struct node *adj[nodes];
for (i=0;i<nodes;i++)
adj[i] = NULL;
read_graf(adj,nodes);
print_graf(adj,nodes);
printf("\nDo you want to try again? 1 for yes and 0 for no: ");
scanf("%d", &op);
if (op==1)
goto begin;
else
{
printf("Thank you for visiting! :)");
exit(0);
}
return 0;
}
void read_graf(struct node *ad[], int no_of_nodes)
{
struct node *new_node;
int i,j,k, val;
for (i=0;i<no_of_nodes;i++)
{
struct node *last= NULL;
printf("\n To how many edges is edge %d connected to: ", i+1);
scanf("%d", &k);
for (j=0;j<k;j++)
{
printf("To which edges is it connected : ");
scanf("%d", &val);
new_node = (struct node *)malloc(sizeof(struct node*));
new_node->data = val;
new_node->next = NULL;
if(ad[i]==NULL)
ad[i]= new_node;
else
last->next = new_node;
last = new_node;
}
}
}
void print_graf(struct node *ad[], int no_of_nodes)
{
struct node *ptr = NULL;
int i,j;
for(i=0;i<no_of_nodes;i++)
{
ptr = ad[i];
printf("\n x%d : ", i+1);
while(ptr != NULL)
{
printf("%d,\t", ptr->data);
ptr = ptr->next;
}
printf("0");
}
}
#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
};
void read_graf(结构节点*ad[],节点的int no_);
void print_graf(结构节点*ad[],节点的int no_);
int main()
{
int op;
int i,j,k,节点;
开始:
printf(“输入边数:”);
scanf(“%d”、&nodes);
结构节点*adj[节点];
对于(i=0;inxt=new_节点;
last=新的_节点;
}
}
}
无效打印图(结构节点*ad[],节点的整数编号)
{
结构节点*ptr=NULL;
int i,j;
对于(i=0;idata);
ptr=ptr->next;
}
printf(“0”);
}
}
如果只需要打印出邻接矩阵,并假设1
表示节点i和节点j之间的连接,则只需稍微修改print\u graf
函数内的内部循环即可
void print_as_mat(struct node *ad[], int no_of_nodes)
{
struct node *ptr = NULL;
for(int i = 0; i < no_of_nodes; ++i)
{
ptr = ad[i];
// Loop over all the possible nodes
for(int j = 0; j < no_of_nodes; ++j)
{
if ( ptr && ptr->data == j ) {
// ^^^^^^^^^^^^^^ Check if the index correspond to the
// current node in the adjacency list.
printf(" 1");
// update the current node in the list, but only here.
ptr = ptr->next;
}
else {
printf(" 0");
}
}
// The row is completed.
putchar('\n');
}
}
void print\u as\u mat(结构节点*ad[],节点的整数编号)
{
结构节点*ptr=NULL;
for(int i=0;idata==j){
//^^^^^^^^^^^^^^^^^^检查索引是否与
//邻接列表中的当前节点。
printf(“1”);
//更新列表中的当前节点,但仅在此处更新。
ptr=ptr->next;
}
否则{
printf(“0”);
}
}
//行已完成。
putchar('\n');
}
}
<代码>数据> <代码>代表节点连接的边的索引,考虑<代码> pReTr.GRAF 的内部循环,使其成为所有节点的代码<代码> > <代码>。而不是打印<代码> PTR >数据< /代码>,如果<代码> j=PTR > >数据< /代码>或<代码> 1 > /代码>,则应打印< <代码> 0 < /代码>。(在本例中更新ptr
)@Bob_uuuuu抱歉,但我不太明白这一点。我应该做一个i和j的双循环,其中我应该包括ptr=ad[i][j],然后在这个循环中,a应该使用if语句打印0 if!=ptr->data,或者如果不是,则打印1?我尝试了这个,void print\u graf2(struct node*ad[],int no\u nodes){struct node*ptr=NULL;int i,j;for(i=0;i@Bob__但是它给了我这个|错误:当从类型“struct node”分配给类型“struct node*”时不兼容类型|我现在尝试了这个,但是当矩阵应该是这样的:F(x1)={x2,x3};F(x2)={x3};F(x3)={x1}x1x2x3x1:01x2:01x3:1000它最终看起来是这样的:010100010
啊,我想我明白了,它从它应该是的位置打印出1到j+1的位置,但是,tbh,我真的不明白它为什么这样做(xd),我尝试过不同的方法,但没有成功help@Vlad如果你从1开始计算边(这与C的基于零的索引规则相冲突),你需要这样做。我希望“算法”很清楚,它只是扫描每个可能的索引并将其与相邻节点列表中的当前节点进行比较。如果存在连接,则打印1并移动到列表的下一个元素,否则打印0。选中所有可能的索引(连接)后,数组的下一个元素(下一个节点列表)哦,我的天啊。我现在觉得自己像个白痴,我什么都试过了,但“j+1”,我想天才隐藏在简单中:)。谢谢你的帮助,非常感谢。