图的邻接表存储
作者:互联网
//date:2020.4.22
//图的邻接表存储 #include <bits/stdc++.h> using namespace std; //首先定义结点 typedef struct Node { int data;//结点的数据 struct Node *next; } EdgeNode; //表结点 typedef struct FirstNode { int index;//表头的数据 EdgeNode *first;//指向第一个结点的指针 } Vertex; typedef struct grap { Vertex vex[100];//表头结点 int n,e;//图的节点数和边数 } Graph; Graph graph; void createGraph() { //先建立顶点表,再建立边表 printf("输入顶点数:\n"); scanf("%d",&graph.n); printf("输入边数:\n"); scanf("%d",&graph.e); getchar(); cout<<"输入顶点"<<endl; for(int i=0; i<graph.n; i++) { cin>>graph.vex[i].index; graph.vex[i].first=NULL; } //输入边 cout<<"输入边"<<endl; for(int i=0; i<graph.e; i++) { int a,b; cin>>a>>b; //因为要创立结点 //在b的邻接链表上挂a EdgeNode *p; p=(EdgeNode *)malloc(sizeof(EdgeNode)); p->data=a; p->next=graph.vex[b].first; graph.vex[b].first=p; //在a的邻接表上挂b p=(EdgeNode *)malloc(sizeof(EdgeNode)); p->data=b; p->next=graph.vex[a].first; graph.vex[a].first=p; } } //int visited[100]= {0}; int main() { //Graph G; createGraph(); return 0; }
图的邻接表存储
标签:存储,EdgeNode,int,graph,结点,vex,邻接,first 来源: https://www.cnblogs.com/someonezero/p/12759823.html