无向图的领接表

来源:互联网 发布:手机桌面便签软件 编辑:程序博客网 时间:2024/05/15 23:57

无向图的领接表。

#include <stdio.h>#include <stdlib.h>typedef struct EdgeNode{     // 边表结点 int adjvex;              // 领接点域,存储该顶点对应的下标             struct EdgeNode *next;   // 链域,指向下一个领接点 }EdgeNode;typedef struct VertexNode{    // 顶点表结点 int data;                 // 顶点域,存储顶点信息EdgeNode *firstedge;      // 边表头指针 }VertexNode;typedef struct{VertexNode adjList[100];int numVertex,numEdges;   // 图中当前顶点数和边数 }GraphAdjList;void CreateALGraph(GraphAdjList *G){int i,j,k;EdgeNode *e;printf("请输入顶点数和边数:");scanf("%d%d",&G->numVertex,&G->numEdges);  // 读入顶点个数与边数 for(i=0;i<G->numVertex;i++){printf("请输入第%d个顶点:",i);scanf("%d",&G->adjList[i].data);     // 读入各个顶点信息 G->adjList[i].firstedge=NULL;        // 将边表置为空表 }for(k=0;k<G->numEdges;k++){printf("输入边(vi,vj)上的顶点下标:");   // 读入与边关联的顶点下标 scanf("%d%d",&i,&j);e=(EdgeNode *)malloc(sizeof(EdgeNode));    // 创建边表结点 e->adjvex=j;                             e->next=G->adjList[i].firstedge;G->adjList[i].firstedge=e;// 若是有向图,下面这个可以不要 e=(EdgeNode *)malloc(sizeof(EdgeNode));e->adjvex=i;e->next=G->adjList[j].firstedge;G->adjList[j].firstedge=e;}for(i=0;i<G->numVertex;i++){printf("与第%d个顶点相领接的顶点下标有:",i);while(G->adjList[i].firstedge!=NULL){printf("%d ",G->adjList[i].firstedge->adjvex);G->adjList[i].firstedge=G->adjList[i].firstedge->next;}printf("\n");}}int main(){GraphAdjList G;CreateALGraph(&G);}


0 0