c语言 图的遍历

来源:百度知道 编辑:UC知道 时间:2024/09/24 14:20:10
#include"stdio.h"
#define m 20
int visited[m];
typedef int datatype;
typedef struct arcnode{
int adjvex;
struct arcnode *nextarc;
}arcnode;
typedef struct vnode{
datatype data;
arcnode *firstarc;
}vnode;
typedef struct{
vnode adjlist[m];
int n,e;
}graph;

void creategraph(graph *g)
{
int i,j,k;
arcnode *p;
printf("Please input n and e:\n");
scanf("%d%d",&g->n,&g->e);
printf("Please input %d vertex:",g->n);
for(i=0;i<g->n;i++)
{
scanf("%d",&g->adjlist[i].data);
g->adjlist[i].firstarc=NULL;
}
printf("Please input %d edge:",g->e);
for(i=0;i<g->e;i++)
{
scanf("%d%d",&j,&k);
p=(struct arcnode*)malloc(sizeof(struct arcnode));
p->adjvex=k;
p->nextarc=g->ad

给指针g开辟一个空间 直接给指针赋值不用开辟空间 创建一个指针对象的时候要开辟空间

好久没来看数据结构了。哈,我只看出你漏了一句话#include"stdlib.h"。希望有好心人帮你回答,恕我无能为你解答啊。不好意思啊!天天开心。

你的算法好像是对的!我试验了一下:
测试数据:5,16 顶点数 边数
0 1 2 3 4 边
对应的边为:
0,3
0,2
0,1
1,4
1,3
1,0
2,4
2,3
2,2
2,0
3,4
3,2
3,1
4,3
4,2
4,1

0-1-2-3
1-0-3-4
2-0-1-3-4
3-1-2-4
4-1-2-3
结果为0 1 3 2 4 正确啊!
楼主把你的测试数据和结果贴出来,我看看!
看看是不是我的测试数据太少不全面!