您的当前位置:首页正文

将无向图的邻接矩阵转化为邻接表

2024-11-26 来源:个人技术集锦
typedef struct
{
	int vertex[max];
	int arc[max][max];
	int vexnum,aecnum;
}MGraph;
typedef struct ArcNode{
	int adjvex;
	struct ArcNode *next;
}ArcNode;
typedef struct VNode
{
	int data;
	ArcNode *first;
}VNode,AdjList[max];
typedef struct
{
	AdjList vertices;
	int vexnum.arcnum;
}ALGraph;

void MtoAL(MGraph G1,ALGraph G2)
{
	int i,j;
	ArcNode *p;
	for(i=0;i<G1.vexnum;i++)
		G2->vertices[i].first=NULL;
	for(i=0;i<G1.vexnum;i++)
		for(j=0;j<G1.vexnum;j++)//这里j初始可以为i,因为下边有两次赋值
		{
			if(G1.arc[i][j]==1)
			{
				p=(ArcNode *)malloc(sizeof(ArcNode));
				p->data=j;
				p->next=G2->vertices[i].first;
				G2->vertices[i].first=p;
				
				p=(ArcNode *)malloc(sizeof(ArcNode));
				p->data=i;
				p->next=G2->vertices[j].first;
				G2->vertices[j].first=p;
			}
		}
}

 

显示全文