【数据结构】用邻接表存储图的简单问题
``````#include<stdio.h>
#define MAX_VERTEX_NUM 20//最多顶点个数
#define ERROR -1

typedef char VertexData;
//边表节点类型定义
typedef struct ArcNode{
struct ArcNode *nextarc;//指向下一条弧的指针
}ArcNode;
//表头节点类型定义
typedef struct VertexNode{
VertexData data;
ArcNode *firstarc;//指向该顶点第一条弧的指针；
int order;
}VertexNode;
typedef struct{
VertexNode vertex[MAX_VERTEX_NUM];
int vexnum,arcnum;

{
int j=ERROR,k;
for(k=0;k<G->vexnum;k++)
{
if(G->vertex[k].data==v)
{
j=G->vertex[k].order;
break;
}
}
return(j);
}

{
printf("请输入图的顶点数和弧数（最多不超过20）：");
scanf("%d %d",&G->vexnum,&G->arcnum);
printf("请输入顶点:");
int i,j,k;
char v1,v2;
for(i=0;i<G->vexnum;i++)
{
scanf("%c",&(G->vertex[i].data));
G->vertex[i].order=i+1;
G->vertex[i].firstarc=NULL;

}
printf("请输入一条弧的两个顶点（例如AB）:\n");
for(k=0;k<G->vexnum;k++)
{
scanf("%c %c",&v1,&v2);
ArcNode *p,*q;
p=(ArcNode*)malloc(sizeof(ArcNode));
q=(ArcNode*)malloc(sizeof(ArcNode));

i=LocateVertex(G,v1);
j=LocateVertex(G,v2);
p->nextarc=G->vertex[i].firstarc;
q->nextarc=G->vertex[j].firstarc;
G->vertex[i].firstarc=p;
G->vertex[j].firstarc=q;
fflush(stdin);
}

}
{
int i,j;
for(i=0;i<G->vexnum;i++)
{
printf("%c  ",G->vertex[i].data);
ArcNode *p;
p=G->vertex[i].firstarc;
while(p!=NULL)
{
p=p->nextarc;
}
printf("\n");

}
}

void main()
{
CreateGraph(&G);
print(&G);
}
``````

1个回答