adjacency table Implementation of graphs
This structure creates adjacency tables when adding nodes more around the suggested breakpoint debugging under check
adjacency tables and adjacency matrices save space compared to adjacency tables but it's hard to tell if there are edges between the two nodes. This structure is cost-effective under sparse graphs.
Here is the code
1 #defineVertex_max 202#include"stdio.h"3#include"malloc.h"4typedefstructEdgenode5 {6 intVertex;//Vertex ordinal7 intweight;8 structEdgenode *next;//point to next vertex with Edge9}edgenode;//structure of adjacent table edgesTen Onetypedefstruct A { -Edgenode *adjlist[vertex_max];//array of struct-body pointers for vertices - intVextexnum,edgenum;//number of vertices number of edges the intGraphtype;//type of diagram - -}listgraph;//adjacency Table Structure - voidCreategraph (Listgraph *g);//Creating adjacency Tables + voidOutlist (listgraph);//Output adjacency Table - + voidCreategraph (Listgraph *G) A { at intI,weight; - intStart,end; -Edgenode *s; - for(i=1; i<=g->vextexnum;i++) -g->adjlist[i]=null;//all vertices are initialized to null - for(i=1; i<=g->edgenum;i++) in { - GetChar (); toprintf"Side%d:", i); +scanf"%d%d%d",&start,&end,&weight); -S= (edgenode*)malloc(sizeof(Edgenode));//request to save an edge of memory thes->next=g->adjlist[start];//point to the previous vertex location *s->vertex=end;//Save end Vertex $s->weight=weight; Panax Notoginsengg->adjlist[start]=s;//The adjacency table pair should point to that point - if(g->graphtype==0) the { +s= (Edgenode *)malloc(sizeof(Edgenode)); AS->next=g->Adjlist[end]; thes->vertex=start; +s->weight=weight; -g->adjlist[end]=s; $ } $ } - } - the voidOutlist (Listgraph *G) - {Wuyi inti; theEdgenode *s; - for(i=1; i<=g->vextexnum;i++) Wu { -printf"Vertex%d", i); AboutS=g->Adjlist[i]; $ while(s) - { -printf"->%d (%d)",s->vertex,s->weight); -S=s->Next; A } +printf"\ n"); the } - } $ Main () the { the listgraph G; theprintf"type of input generation diagram (0. No-map, 1. map)"); thescanf"%d",&g.graphtype); -printf"Enter the number of vertices and the number of edges of the graph"); inscanf"%d%d",&g.vextexnum,&g.edgenum); theprintf"enter the two vertices and weights that make up each edge: \ n"); theCreategraph (&G); Aboutprintf"output adjacency table \ n"); theOutlist (&G); the return 0; the}
A two-step algorithm for Graph adjacency table implementation