1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include<stdio.h> #include<stdlib.h> #define MAXVEX 100
typedef char VertexType; typedef int EdgeType;
typedef struct EdgeNode{ int adjVex; EdgeType weigth; struct EdgeNode* next;
}EdgeNode;
typedef struct VertrNode{ VertexType data; struct EdgeNode* firstEdge; }VertrNode,AdjList[MAXVEX];
typedef struct GraphAdj{ int numVertexes,numEdges; AdjList adjList; }GraphAdjList;
void CreateGraphAdjList(GraphAdjList* G){ int i,j,k,w; printf("请输入顶点数量与边的数量:\n"); scanf("%d%d",&G->numVertexes,&G->numEdges); getchar(); printf("请输入每个顶点的数据:\n"); for(i = 0;i < G->numVertexes;i ++){ scanf("%c",&G->adjList[i].data); G->adjList[i].firstEdge = NULL; }
for(k = 0;k < G->numEdges;k ++) { printf("请输入边(vi,vj)上的顶点的下标,i,j,及权重w:\n"); scanf("%d%d%d",&i,&j,&w); EdgeNode* q = (EdgeNode*)malloc(sizeof (EdgeNode)); q->adjVex = j; q->weigth = w; q->next = G->adjList[i].firstEdge; G->adjList[i].firstEdge = q;
} }
int main(){ GraphAdjList GA; printf("--------邻接表的创建--------"); CreateGraphAdjList(&GA); return 0; }
|