数据结构与算法 ~ 图 ~ 最小生成树 ~ 克鲁斯卡尔算法(图采用邻接表方式存储)
/* graph ------adjacency List */
#include<stdlib.h>
#include<stdio.h>
#define MAX 20
/*弧结点信息*/
struct ArcNode{
int adjvex;/*该弧所指向的顶点的信息*/
int cost;
int visited;
struct ArcNode *nextarc; /*指向下一条弧的指针*/
};
struct VexNode{
int data;/*顶点信息*/
int muster;/*顶点所属集合*/
struct ArcNode *firstarc;/*指向第一条依附于该顶点的弧的指针*/
};
struct VexNode head[MAX];/*顶点数组*/
int vexnum=-1;
int graphtype;
int formatlist(){
printf("\n1---创建无向图:");
printf("\n2---创建有向图:");
printf("\n0---退出");
printf("\n请输入选择:");
scanf("%d",&graphtype);
return graphtype;
}
void constr(struct VexNode *head,int source, int destination,int cost){
int max;
struct ArcNode *newNode,*pointer;
newNode=(struct ArcNode*)malloc(sizeof(struct ArcNode));/*分配空间*/
if (newNode==NULL){
printf("错误!");
exit(0);
}else{
newNode->adjvex=destination; /*生成弧结点*/
newNode->cost=cost;
newNode->nextarc=NULL;
newNode->visited=0;
if ( head[source].firstarc==NULL) /*头结点没有邻接点*/
head[source].firstarc=newNode;
else{