标签:fir type get stdio.h nbsp png flush malloc log
原文链接:C语言邻接表的实现
这篇博文的代码写的很好,我就直接合并在一起贴出来了,方便自己使用,至于文章内容有需要可以看上述原文
#define OK 1 #define ERROR 0 #define MVNum 100 #include<stdio.h> #include<stdlib.h> typedef int Status; typedef char VerTexType; typedef int OtherInfo; typedef struct ArcNode{ int adjvex; ArcNode *nextarc; OtherInfo info; }ArcNode; typedef struct VNode{ VerTexType data; ArcNode *firstarc; }VNode,AdjList[MVNum]; typedef struct{ AdjList vextices; int vexnum, arcnum; }ALGraph; int LocateVex(ALGraph *G, VerTexType v) { int i; for (i = 0; i < (G->vexnum); i++) { if (G->vextices[i].data == v) return i; } } Status CreateUDG(ALGraph *G) { int i, j, k; VerTexType v1, v2; ArcNode *p1, *p2; printf("输入总顶点数和总边数:"); scanf("%d %d", &G->vexnum, &G->arcnum); printf("输入各个顶点的值:"); fflush(stdin); for (i = 0; i < G->vexnum; i++) { scanf("%c", &G->vextices[i].data); G->vextices[i].firstarc = NULL; } for (k = 0; k < G->arcnum; k++) { printf("输入相连的两边(请一条边一条边输入):"); fflush(stdin); scanf("%c %c", &v1, &v2); i = LocateVex(G, v1); j = LocateVex(G, v2); p1 = (ArcNode *)malloc(sizeof(ArcNode)); p2 = (ArcNode *)malloc(sizeof(ArcNode)); p1->adjvex = j; p1->nextarc = G->vextices[i].firstarc; G->vextices[i].firstarc = p1; p2->adjvex = i; p2->nextarc = G->vextices[j].firstarc; G->vextices[j].firstarc = p2; } return OK; } int main(void) { int i; ALGraph G; ArcNode *p; CreateUDG(&G); for (i = 0; i < G.vexnum; i++) { p = G.vextices[i].firstarc; printf("%c相连的顶点有:", G.vextices[i].data); while (p != NULL) { printf("%c ", G.vextices[p->adjvex].data); p = p->nextarc; } printf("\n"); } return 0; } /* 输入样例: 5 6 abcde a b a d b c b e c d c e */
标签:fir type get stdio.h nbsp png flush malloc log
原文地址:https://www.cnblogs.com/exciting/p/10090040.html