数据结构之图论:寻找图中所有结点的邻接点

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#define maxnode 30
typedef int elemtype;
typedef struct arc
{
int adjvertex;
struct arc *nextarc;
}arctype;
typedef struct
{
elemtype vertex;
arctype *firstarc;
}vertextype;
typedef vertextype adjlisttype[maxnode];
int locvertex(adjlisttype graph ,int v)
{
int k;
for(k=0;k{
if(graph[k].vertex==v)
return k;
}
}
int main()
{
int i,j,n,e,k;
int v1,v2;
arctype *p,*q;
adjlisttype graph;
printf("\n请输入图中定点的个数n和边数e:\n");
scanf("%d%d",&n,&e);
printf("输入图中顶点的数据:\n");
for(k=0;k{
scanf("%d",&graph[k].vertex);
graph[k].firstarc=NULL;
}
printf("\n输入图中的各边,次序为弧尾编号,弧头编号:\n");
for(k=0;k{
scanf("%d%d",&v1,&v2);
i=locvertex(graph,v1);
j=locvertex(graph,v2);
q=(arctype *)malloc(sizeof(arctype));
q->adjvertex=j;
q->nextarc=graph[i].firstarc;
graph[i].firstarc=q;
p=(arctype *)malloc(sizeof(arctype));
p->adjvertex=i;
p->nextarc=graph[j].firstarc;
graph[j].firstarc=p;
}
printf("\n图的邻接表结构为:\n");
for(i=0;i{
printf("i=%d",i);
v1=graph[i].vertex;
printf("vertex:%d",v1);
p=graph[i].firstarc;
while(p!=NULL)
{
v2=p->adjvertex;
printf("-->%d",v2);
p=p->nextarc;
}
printf("\n");
}
return 0;
}

相关文档
最新文档