#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*start1=NULL,*start2=NULL;
void main()
{
struct node *temp,*q,*p;
int n,i,x,f=0;
clrscr();
printf("\n First linked list");
printf("\n Enter How many number:-");
scanf("%d",&n);
for(i=0;i<n;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n Insufficient memory");
getch();
exit(0);
}
printf("\n Enter number:-");
scanf("%d",&x);
temp->info=x;
temp->link=NULL;
if(start1==NULL)
{
start1=temp;
}
else
{
q=start1;
while(q->link!=NULL)
{
q=q->link;
}
q->link=temp;
}
}
printf("\n first list\n");
q=start1;
while(q!=NULL)
{
printf("\t%d",q->info);
q=q->link;
}
start2=NULL;
printf("\n Second linked list");
printf("\n Enter How many number:-");
scanf("%d",&n);
for(i=0;i<n;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n Insufficient memory");
getch();
exit(0);
}
printf("\n Enter number:-");
scanf("%d",&x);
temp->info=x;
temp->link=NULL;
if(start2==NULL)
{
start2=temp;
}
else
{
q=start2;
while(q->link!=NULL)
{
q=q->link;
}
q->link=temp;
}
}
printf("\n second list\n");
q=start2;
while(q!=NULL)
{
printf("\t%d",q->info);
q=q->link;
}
printf("\n intersection of two list is:-\n");
q=start1;
while(q!=NULL)
{
p=start2;
while(p!=NULL)
{
if(q->info==p->info)
{
printf("\t%d",q->info);
}
p=p->link;
}
q=q->link;
}
getch();
}
/* OUTPUT
First linked list
Enter How many number:-5
Enter number:-1
Enter number:-2
Enter number:-3
Enter number:-4
Enter number:-5
first list
1 2 3 4 5
Second linked list
Enter How many number:-4
Enter number:-1
Enter number:-2
Enter number:-5
Enter number:-6
second list
1 2 5 6
intersection of two list is:-
1 2 5
*/
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*start1=NULL,*start2=NULL;
void main()
{
struct node *temp,*q,*p;
int n,i,x,f=0;
clrscr();
printf("\n First linked list");
printf("\n Enter How many number:-");
scanf("%d",&n);
for(i=0;i<n;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n Insufficient memory");
getch();
exit(0);
}
printf("\n Enter number:-");
scanf("%d",&x);
temp->info=x;
temp->link=NULL;
if(start1==NULL)
{
start1=temp;
}
else
{
q=start1;
while(q->link!=NULL)
{
q=q->link;
}
q->link=temp;
}
}
printf("\n first list\n");
q=start1;
while(q!=NULL)
{
printf("\t%d",q->info);
q=q->link;
}
start2=NULL;
printf("\n Second linked list");
printf("\n Enter How many number:-");
scanf("%d",&n);
for(i=0;i<n;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n Insufficient memory");
getch();
exit(0);
}
printf("\n Enter number:-");
scanf("%d",&x);
temp->info=x;
temp->link=NULL;
if(start2==NULL)
{
start2=temp;
}
else
{
q=start2;
while(q->link!=NULL)
{
q=q->link;
}
q->link=temp;
}
}
printf("\n second list\n");
q=start2;
while(q!=NULL)
{
printf("\t%d",q->info);
q=q->link;
}
printf("\n intersection of two list is:-\n");
q=start1;
while(q!=NULL)
{
p=start2;
while(p!=NULL)
{
if(q->info==p->info)
{
printf("\t%d",q->info);
}
p=p->link;
}
q=q->link;
}
getch();
}
/* OUTPUT
First linked list
Enter How many number:-5
Enter number:-1
Enter number:-2
Enter number:-3
Enter number:-4
Enter number:-5
first list
1 2 3 4 5
Second linked list
Enter How many number:-4
Enter number:-1
Enter number:-2
Enter number:-5
Enter number:-6
second list
1 2 5 6
intersection of two list is:-
1 2 5
*/