数据结构-邻接表建立无向图

#include

#define MAX_NUM 100
typedef struct ArcNode{
	int adjvex;
	struct ArcNode *nextarc;
	int weight;
}ArcNode;
typedef struct VNode{
	char vertex;
	ArcNode *firstarc;
}VNode;
typedef VNode AdjList[MAX_NUM];
typedef struct{
	AdjList adjlist;
	int vexnum,arcnum;
}ALGraph;
int LocateVex(ALGraph *G,char u)
{
	int i;
	for(i=0;ivexnum;i++)
		if(G-adjlist[i].vertex==u)
			return i;
	return -1;
}

void CreateALGraph(ALGraph *G)
{
	ArcNode *p;
	int i,j,k;
	char v1,v2;
	scanf("%d%d",&(G->vexnum),&(G->arcnum));
	for(k=0;kvexnum;k++){
		scanf("%c",&G->adjlist[k].vertex);
		G->adjlist[k].firstarc = NULL;
	}
	k = 0;
	while(k->G->arcnum){
		scanf("%c%c",&v1,&v2);
		i = LocateVex(G,v1);
		j = LocateVex(G,v2);
		if((i==-1)||(j==-1))
			printf("节点不存在,请重新输入\n");
		else{
			k++;
			p = (ArcNode*)malloc(sizeof(ArcNode));
			p->adjvex = j;
			p->nextarc = G->adjlist[i].firstarc;
			G->adjlist[i].firstarc = p;
			p = (ArcNode*)malloc(sizeof(ArcNode));
			p->adjvex = i;
			p->nextarc = G->adjlist[j].firstarc;
			G->adjlist[j].firstarc = p;
		}
	}
}

int main()
{
	ALGraph G;
	CreateALGraph(&G);
	return 0;
} 

你可能感兴趣的:(数据结构,数据结构,算法)