/*
* uniinter.c
*
* Created on: Mar 21, 2023
* Author: hp
*/
/*
* union.c
*
* Created on: Mar 21, 2023
* Author: hp
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
int print(struct node *head)
{
int count=0;
if(head==NULL)
{
printf("Linked list is empty");fflush(stdout);
}
struct node *ptr1=NULL;
ptr1=head;
while(ptr1!=NULL)
{
count++;
printf("\n %d",ptr1->data);fflush(stdout);
ptr1=ptr1->link;
}
// printf("\n The number of nodes is: %d",count);fflush(stdout);
return count;
}
int main()
{
int a,b;
int i=0,j=0;
printf("\n enter the number of nodes in first list");fflush(stdout);
scanf("%d",&a);
printf("\n %d",a);
int num;
struct node *head1=malloc(sizeof(struct node));
//struct node *current=malloc(sizeof(struct node));
printf("\n Enter the data of node 1:");fflush(stdout);
scanf("%d",&num);
head1->data=num;
head1->link=NULL;
struct node *temp=head1;
for(i=2;i<=a;i++)
{
printf("\nEnter the data of node %d:",i);fflush(stdout);
scanf("%d",&num);
struct node *newnode=malloc(sizeof(struct node));
newnode->data=num;
newnode->link=NULL;
temp->link=newnode;
temp=temp->link;
}
printf("\n enter the number of nodes in second list");fflush(stdout);
scanf("%d",&b);
printf("\n %d",b);
struct node *head2=malloc(sizeof(struct node));
//struct node *current=malloc(sizeof(struct node));
printf("\n Enter the data of node 1:");fflush(stdout);
scanf("%d",&num);
head2->data=num;
head2->link=NULL;
temp=head2;
for(j=2;j<=b;j++)
{
printf("\nEnter the data of node %d:",j);fflush(stdout);
scanf("%d",&num);
struct node *newnode=malloc(sizeof(struct node));
newnode->data=num;
newnode->link=NULL;
temp->link=newnode;
temp=temp->link;
}
//print(head1);
//print(head2);
struct node *temp1,*temp2;
temp1=head1;
temp2=head2;
struct node *union_A=malloc(sizeof(struct node));
union_A->data=head1->data;
union_A->link=head1->link;
//print(union_A);fflush(stdout);
int c =print(head1);
// printf("\n Node in the first list is: %d",c);
while(temp2!=NULL)
{
int c1=0;
temp1=head1;
while(temp1!=NULL)
{
if((temp2->data)!=(temp1->data))
{
c1++;
}
// printf("\n %d",c1);
temp1=temp1->link;
}
if(c1==c)
{
struct node *newnode=malloc(sizeof(struct node));
newnode->data=temp2->data;
newnode->link=union_A;
union_A=newnode;
}
temp2=temp2->link;
}
printf("\n The union is:");fflush(stdout);
print(union_A);
struct node *temp3,*temp4;
temp3=head1;
temp4=head2;
struct node *inter_A=malloc(sizeof(struct node));
inter_A=NULL;
while(temp3!=NULL)
{
temp4=head2;
while(temp4!=NULL)
{
if((temp3->data)==(temp4->data))
{
struct node *newnode=malloc(sizeof(struct node));
newnode->data=temp4->data;
newnode->link=inter_A;
inter_A=newnode;
}
temp4=temp4->link;
}
temp3=temp3->link;
}
printf("\n The intersection is:");fflush(stdout);
print(inter_A);
}



