Follow us on Facebook

Header Ads

Welcome to JAVA POint

Write a C Program to find the Adjacency List in C (Data Structure )

                     DATA STRUCTURE AND ALGORITHM  

Write a C Program to find the Adjacency List in C (Data Structure )

struct node //If you  have plzz write the Header file

{

int vertex;

struct node *next;

}*v[10];

void create(int m[20][20],int n)

{

int i,j;

char ans;

for(i=0;i<n;i++)

for(j=0;j<n;j++)

{

printf("is there an edge between %d and %d",i+1,j+1);

scanf("%d",&m[i][j]);

}

}

void disp(int m[20][20],int n)

{

int i,j;

printf("\nthe adjancy matrix is:\n");

for(i=0;i<n;i++)

{

for(j=0;j<n;j++)

printf("%d",m[i][j]);

printf("\n");

}

}

void create1(int m[20][20],int n)

{

int i,j;

struct node *temp,*newnode;

for(i=0;i<n;i++)

{

v[i]=NULL;

for(j=0;j<n;j++)

{

if(m[i][j]==1)

{

newnode=(struct node *)malloc(sizeof(struct node));

newnode->next=NULL;

newnode->vertex=j+1;

if(v[i]==NULL)

v[i]=temp=newnode;

else

{

temp->next=newnode;

temp=newnode;

}

}

}

}

}

void displist(int n)

{

struct node *temp;

int i;

for(i=0;i<n;i++)

{

printf("\n v %d |",i+1);

temp=v[i];

while(temp)

{

 

printf("v %d->",temp->vertex);

temp=temp->next;

}

printf("NULL");

}

}

void main()

{

int m[20][20],n;

printf("\n enter no of vertex");

scanf("%d",&n);

create(m,n);

disp(m,n);

create1(m,n);

displist(n);

}

_______________________________________________________________________                           OUTPUT

 

ame@amr-virtual-machine:~$ ./graph

 

enter no of vertex3

is there an edge between 1 and 10

is there an edge between 1 and 21

is there an edge between 1 and 30

is there an edge between 2 and 11

is there an edge between 2 and 20

is there an edge between 2 and 31

is there an edge between 3 and 10

is there an edge between 3 and 20

is there an edge between 3 and 30

 

the adjancy matrix is:

010

101

000

 

 v 1 |v 2->NULL

 v 2 |v 1->v 3->NULL

 v 3 |NULL


0 Comments:

Post a Comment

C language and C++ progaming