#define N 100//图最大顶点个数
#define MAX 32767
#include "stdio.h"
typedef struct//图的邻接矩阵类型
{
int AdjMatrix[N][N]; //邻接矩阵
int VexNum, ArcNum; //顶点数,弧数
//int vexs[N];//存放顶点信息---如该顶点的下一个顶点
} AM_Graph;
//输出图的邻接矩阵
void DisplayAM(AM_Graph g)/*输出邻接矩阵*/
{
int i, j;
for (i = 0; i < g.VexNum; i++)
{
for (j = 0; j < g.VexNum; j++)
if (g.AdjMatrix[i][j] == MAX) // CHANGE
printf("%4s", "∞");
else printf("%4d", g.AdjMatrix[i][j]); // CHANGE
printf("\n");
}
}
//图的邻接表建立
//图的邻接表存储结构描述
#include "stdlib.h"
#define VERTEX_NUM 8 //顶点数
#define ARC_NUM 9 //边数
typedef int VexType;
typedef struct AdjNode //邻接结点结构
{
int adjvex; //邻接点编号
int weight; //权值——可选项
struct AdjNode* next; // 邻接点指针 CHANGE
}AL_AdjNode;
typedef struct //邻接表顶点结点结构
{
VexType vertex; //顶点
int indegree; //入度——此为可选项
struct AdjNode* link; // 邻接点头指针 CHANGE
} AL_VexNode;
typedef struct //总的邻接表结构
{
AL_VexNode VexList[VERTEX_NUM]; //顶点表
int VexNum, ArcNum; //顶点数,弧(边)数
} AL_Graph;
//建立邻接表
AL_Graph Create_AdjList(int AdjMatrix[N][N])
{
//int G;
AL_Graph G = { {0,NULL},VERTEX_NUM,ARC_NUM };
int j;
AL_AdjNode* Ptr, * nextPtr;
for (int i = 0; i < VERTEX_NUM; i++)
{
G.VexList->vertex = i;
G.VexList->link = NULL;
j = 0;
while (j < VERTEX_NUM)
{
if (AdjMatrix[j] != 0)//有邻接点
{
Ptr = (AL_AdjNode*)malloc(sizeof(AL_AdjNode));
Ptr->adjvex = j;
Ptr->next = NULL;
if (G.VexList->link == NULL)//首次加入邻接点
{
G.VexList->link = Ptr;
nextPtr = Ptr;
}
else
{
nextPtr->next = Ptr;
nextPtr = Ptr;
}
}
j++;
}
}
return G;
}
//调试程序
int main()
{
//邻接表Adjacency List--AL
int AdjMatrix[N][N] =//邻接矩阵——测试样例1
{ {0,1,1,0,0,0,0,0},
{1,0,0,1,1,0,0,0},
{1,0,0,0,0,1,1,0},
{0,1,0,0,0,0,0,1},
{0,1,0,0,0,0,0,1},
{0,0,1,0,0,0,1,0},
{0,0,1,0,0,1,0,0},
{0,0,0,1,1,0,0,0} };
/*===================
邻接矩阵——测试样例2
{{0,1,1,1,0,0},
{0,0,0,0,0,0},
{0,1,0,0,1,0},
{0,0,0,0,1,0},
{0,0,0,0,0,0},
{0,0,0,1,1,0}};
=====================*/
AL_Graph G;
G = Create_AdjList(AdjMatrix);
return 0;
}