#include#include const int MAXV=5;typedef char ELEMENT_TYPE;typedef int InfoType;//树的邻接矩阵表示方法,分别表示了点,和边typedef struct{ int no; ELEMENT_TYPE info;}VertexType; typedef struct{ int edges[MAXV][MAXV]; int n,e;//分别表示定点数和边数 VertexType vexs[MAXV];}MGraph;int A[MAXV][MAXV]={ 0,1,1,1,0, 1,0,1,0,1,1,1,0,1,1,1,0,1,0,1,0,1,1,1,0};//树的邻接表存储typedef struct ANode{ int adjvex;//边的终点位置 ANode *nextarc; InfoType info;}ArcNode;typedef struct { ELEMENT_TYPE data; ArcNode *firstarc;}Vnode;typedef struct{ Vnode adjlist[MAXV]; int n,e;}AGraph;//通过一个a[][]矩阵生成一个MGraph类型的gvoid CreatMat(MGraph &g,int (*a)[MAXV],int n){ int i,j; g.n=n; g.e=0; for (i=0;i n=n; G->e=0; for(i=0;i adjlist[i].firstarc=NULL; for(i=0;i =0;j--) { if (a[i][j]!=0) { p=(ArcNode*)malloc(sizeof(ArcNode)); p->adjvex=j; p->nextarc=G->adjlist[i].firstarc; G->adjlist[i].firstarc=p; G->e++; } }}//输出图的邻接表void DispAdj(AGraph *G){ int i; ArcNode *p; for(i=0;i n;i++) { printf("[%d]->",i+1); p=G->adjlist[i].firstarc; while(p!=NULL) { printf("%d->",p->adjvex+1); p=p->nextarc; } printf("\n"); }}//求用邻接表存储的图的chu度void OutDs(AGraph *B){ int i; ArcNode *p; int A[MAXV]={ 0}; for (i=0;i n;i++) { p=B->adjlist[i].firstarc; while(p!=NULL) { A[i]++; p=p->nextarc; } } printf("各个点的入度:\n"); for (i=0;i n;i++) printf(" 顶点%d:%d\n",i+1,A[i]);}void main(){ MGraph g; AGraph *G; CreatMat(g,A,MAXV); printf("输出MGraph g:\n"); DispMat(g); GreateAdj(G,A,MAXV); printf("输出AGraph *G:\n"); DispAdj(G); printf("输出AGraph *G的各点的入度:\n"); OutDs(G);}