#include<stdio.h>
#include<stdlib.h>
#define MALLOC(p,n,type)\
p=(type*)malloc(n*sizeof(type));\
if(p==NULL)\
{\
printf("insufficient memeory\n");\
exit(0);\
}
struct node
{
int info;
struct node *link;
};
typedef struct node* NODE;
NODE insertion_rear(int,NODE);
NODE insertion_rear(int item,NODE first)
{
NODE temp,cur;
MALLOC(temp,1,struct node);
temp->info=item;
temp->link=NULL;
if(first==NULL)
{
return temp;
}
cur=first;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=temp;
return first;
}
void read_adjacency_list(int n,NODE a[])
{
int i,j,m,item;
for(i=0;i<n;i++)
{
scanf("%d",&m);
if(m==0) continue;
for(j=0;j<m;j++)
{
scanf("%d",&item);
a[i]=insertion_rear(item,a[i]);
}
}
}
void display_adjacency_list(int n,NODE a[])
{
int i;
NODE temp;
printf("Adjacency Linked List elements are:\n");
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
temp=a[i];
if(temp==NULL)
printf("NULL\n");
else
{
temp=a[i];
while(temp->link!=NULL)
{
printf("%d-->",temp->info);
temp=temp->link;
}
printf("%d\n",temp->info);
}
}
}
void main()
{
NODE a[10];
int vertices,choice,i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i]=NULL;
}
for(;;)
{
scanf("%d",&choice);
switch(choice)
{
case 1:read_adjacency_list(n,a);
break;
case 2:display_adjacency_list(n,a);
break;
case 3:exit(0);
default:printf("please enter valid choice\n");
}
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: