GRAPH IMPLEMENTATION USING LINKED LIST #include<stdio.h> #include typedef struct vnode { int vname; struct vnode *vlink; struct enode *elink; }vnode; typedef struct enode { int vname; int eweight; struct enode *elink; }enode; vnode *adjlist=NULL; vnode* getvnode(); enode* getenode(); void insertvertex(); void insertedge(int vstart,int vend); void creategraph(); void viewgraph(); void display_menu(); void main() { char choice='y'; int ch,vs,ve,vd,vi; display_menu(); while((choice=='y')||(choice=='y')) { printf("\n?"); fflush(stdin); scanf("%d",&ch); switch(ch) { case 0 : display_menu(); break; case 1: creategraph();
break; case 2: insertvertex(); break; case 3: printf("\n enter tne starting & ending vertex to insert edge:"); scanf("%d%d",&vs,&ve); insertedge(vs,ve); break; case 4: viewgraph(); break; default: printf("end of run ur program"); exit(0); }}} vnode *getvnode() { int size; vnode *newvnode; size=sizeof(vnode); newvnode=(vnode *)malloc(size); newvnode->vlink=NULL; newvnode->elink=NULL; return(newvnode); } enode* getenode() { int size; enode *newenode; size=sizeof(enode); newenode=(enode*)malloc(size); newenode->elink=NULL; return(newenode); } void insertvertex() { vnode *tv,*nv; nv=getvnode(); nv->vname=1; if(adjlist==NULL) {
adjlist=nv; return; } for(tv=adjlist;tv->vlink!=NULL;tv=tv->vlink); tv->vlink=nv; nv->vname=tv->vname+1; } void insertedge(int vstart,int vend) { vnode *pv; enode *te,*pe; for (pv=adjlist;pv!=NULL&&pv->vname!=vstart;pv=pv->vlink); if(pv==NULL) return; te=getenode(); printf("enter edge weight from v%d to bv%d:",vstart,vend); scanf("%d",&te->eweight); te->vname=vend; if(pv->elink==NULL) { pv->elink=te; return; } for(pe=pv->elink;pe->elink!=NULL;pe=pe->elink); pe->elink=te; } void creategraph() { int r,c,v,nvertex; printf("\n enter the no of vertices:"); scanf("%d",&nvertex); adjlist=NULL; for(v=0;v
for(pv=adjlist;pv!=NULL;pv=pv->vlink) { printf("\nv%d-2d to",pv->vname); nvertex++; for(pe=pv->elink;pe!=NULL;pe=pe->elink) { edges++; printf("v%d-2d(%d)->",pe->vname,pe->eweight); if(pe->eweight>1) wstatus=1; } printf("NULL"); } if(adjlist!=NULL) { printf("\n %s graph",(wstatus)?"weighted":"unweighted"); printf("\n number of vertices:%5d",nvertex); printf(" \n number of edges :%5d",edges); } } void display_menu() { printf("\n\n basic operation in an adjacent list"); printf("\n\t 0.display menu"); printf("\n\t 1. creation of a graph"); printf("\n\t 2.insert a vertex"); printf("\n\t 3.insert an edge"); printf("\n\t 4.view the graph"); printf("\n\t 5.exit"); }