关于图的邻接矩阵建立,我真的改不动了
//图的邻接矩阵建立#define N 100//图最大顶点个数
#define MAX 32767
#include "stdio.h"
typedef struct//图的邻接矩阵类型
{
int AdjMatrix; //邻接矩阵
int VexNum, ArcNum; //顶点数,弧数
//int vexs;//存放顶点信息---如该顶点的下一个顶点
} 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 == MAX)
printf("%4s", "∞");
elseprintf("%4d", g.AdjMatrix);
printf("\n");
}
}
//图的邻接表建立
//图的邻接表存储结构描述
#include "stdlib.h"
#define VERTEX_NUM 8 //顶点数
#define ARC_NUM 9 //边数
typedef int VexType;
typedef struct AdjNode //邻接结点结构
{
int adjvex; //邻接点编号
int weight; //权值——可选项
AdjNode* next; // 邻接点指针
}AL_AdjNode;
typedef struct//邻接表顶点结点结构
{
VexTypevertex; //顶点
intindegree; //入度——此为可选项
AdjNode* link; // 邻接点头指针
} AL_VexNode;
typedef struct//总的邻接表结构
{
AL_VexNodeVexList;//顶点表
int VexNum, ArcNum; //顶点数,弧(边)数
} AL_Graph;
//建立邻接表
AL_Graph Create_AdjList(int AdjMatrix)
{
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 != 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 =//邻接矩阵——测试样例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;
}
感觉没毛病,但是一运行就各种未声明的标识符,可是明明声明了,声明顺序也是对的,哭死 #define N 100//图最大顶点个数
#define MAX 32767
#include "stdio.h"
typedef struct//图的邻接矩阵类型
{
int AdjMatrix; //邻接矩阵
int VexNum, ArcNum; //顶点数,弧数
//int vexs;//存放顶点信息---如该顶点的下一个顶点
} 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 == MAX)// CHANGE
printf("%4s", "∞");
elseprintf("%4d", g.AdjMatrix);// 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//邻接表顶点结点结构
{
VexTypevertex; //顶点
intindegree; //入度——此为可选项
struct AdjNode* link; // 邻接点头指针CHANGE
} AL_VexNode;
typedef struct//总的邻接表结构
{
AL_VexNodeVexList;//顶点表
int VexNum, ArcNum; //顶点数,弧(边)数
} AL_Graph;
//建立邻接表
AL_Graph Create_AdjList(int AdjMatrix)
{
//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 != 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 =//邻接矩阵——测试样例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;
}
页:
[1]