# 邻接链表创建图

505人阅读 评论(0)


typedef char VertexType[MAX_VEX];
typedef struct ArcNode
{
struct node *nextarc;
}ArcNode;

typedef struct VexNode
{
VertexType vertex;
ArcNode *firstedge;

typedef struct
{
int vexnum,arcnum;
int kind;
}ALGraph;

int LocateVex(ALGraph *G,VertexType u)
{
int i;
for(i=0;i<G->vexnum;i++)
if(strcmp(u,G->vertices[i].vertex)==0)
return i;
return -1;
}

void Display(ALGraph *G)
{
int i = 0;
ArcNode *p;
for(i=0;i<G->vexnum;i++)
{
p = G->vertices[i].firstedge;
printf("%s",G->vertices[i].vertex);
while(p)
{
p=p->nextarc;
}
printf("\n");
}
}

int create(ALGraph *G)
{
int i,j,k,m;
VertexType va,vb;
ArcNode *p;
printf("please input the number of vex and edge\n");
scanf("%d %d",&G->vexnum,&G->arcnum);
for(i=0;i<G->vexnum;i++)
{
fflush(stdin);
printf("The vertex is:");
scanf("%s",G->vertices[i].vertex);
G->vertices[i].firstedge = NULL;
}
for(j=0;j<G->arcnum;j++)
{
printf("\ninput arc(vi-vj):");
scanf("%s%s",&va,&vb);
k = LocateVex(G,va);
m = LocateVex(G,vb);
printf("k,m is %d,%d",k,m);
p = (ArcNode*)malloc(sizeof(ArcNode));
if(!p)
exit(-1);
p->nextarc = G->vertices[k].firstedge;
G->vertices[k].firstedge = p;
p = (ArcNode*)malloc(sizeof(ArcNode));
p->nextarc = G->vertices[m].firstedge;
G->vertices[m].firstedge = p;
}

return 0;
}

int main(int argc,char** argv)
{
ALGraph g;
create(&g);
Display(&g);
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：25573次
• 积分：448
• 等级：
• 排名：千里之外
• 原创：22篇
• 转载：1篇
• 译文：0篇
• 评论：4条
评论排行
最新评论