Graph-Adjacency List

Learn Theory - Graph Representations

/* Implementation- Graph representation *****/
#include <stdio.h>
#include <stdlib.h>
struct vertex
{
int data;
struct vertex *next;
}*adj[50],*tail[50],*temp=NULL;

int nv;
void read()
{ int adv;
printf("Enter the Number of vertices\n");
scanf("%d",&nv);

// storing element from [1][1]
//// assuming the vertices are numbered v1 v2..vn and we will enter only the index 1,2...n
for(int i=1;i<=nv;i++)
{
printf("Enter the list of vertices adjacent to v%d -1 to stop\n",i);
adj[i]=NULL;tail[i]=NULL;
while(1)
{
    scanf("%d",&adv);
    if(adv==-1) break;
    temp=(struct vertex *)malloc(sizeof(struct vertex));
    temp->data=adv;
    temp->next=NULL;
    if(adj[i]==NULL) {adj[i]=temp;tail[i]=temp;} else { tail[i]->next=temp;tail[i]=temp;}
}
}
}//end of read

void disp()
{
printf("Adjacency List..\n");
for(int i=1;i<=nv;i++)
{ temp=adj[i];
printf("V%d-->",i);
while(temp!=NULL)
{
    printf("V%d--->",temp->data);
    temp=temp->next;
}
printf("NULL\n");
}
}
int main()
{
read();
disp();
return 0;
}

Comments

Popular posts from this blog

Data Structure Lab CSL 201 Manual KTU - Dr Binu V P-9847390760

Lab Assignments

Polynomial Addition using Arrays