100% found this document useful (2 votes)
9K views

Graph Implementation Using Linked List

This program implements a graph data structure using linked lists in C. It defines vertex and edge node structures to represent the graph. Functions are included to create the graph, insert vertices and edges, and display the graph. The main function provides a menu driver that allows the user to create a graph, add vertices and edges, and view the graph representation.

Uploaded by

shameedp
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
9K views

Graph Implementation Using Linked List

This program implements a graph data structure using linked lists in C. It defines vertex and edge node structures to represent the graph. Functions are included to create the graph, insert vertices and edges, and display the graph. The main function provides a menu driver that allows the user to create a graph, add vertices and edges, and view the graph representation.

Uploaded by

shameedp
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

GRAPH IMPLEMENTATION USING LINKED LIST

#include<stdio.h>
#include<conio.h>
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<nvertex;v++)
insertvertex();
for(r=0;r<nvertex;r++)
for(c=0;c<nvertex;c++)
if(r!=c)
insertedge(r+1,c+1);
}
void viewgraph()
int edges=0,wstatus=0,nvertex=0;
vnode *pv; enode *pe;
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");
}

You might also like