#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node{//creating a structurs
int value;
struct node *next;
}Node;
static Node *h1;
static Node *h2;
static Node *head=NULL;
static Node *tail=NULL;
static Node *Union=NULL;
static Node *Intersection=NULL;
static Node *result_union=NULL;
static Node *result_intersection=NULL;
static Node *newnode;
Node *insert(Node *head,int n);
void display_list(Node *head);
Node *unions(Node *h1,Node *h2);
Node *intersection(Node *h1,Node *h2);
int compare(Node *head,int n);
Node *insert(Node *head,int n)//for inseerting a new elements
{
newnode=(Node*)malloc(sizeof(Node));
newnode->value=n;
newnode->next=head;
head=newnode;
return head;
}
void display_list(Node *head)//to display the list
{
if(head==0)
printf("list is empty");
else
{
tail=head;
while(tail!=0)
{
printf("\n%d",tail->value);
tail=tail->next;
}
tail=newnode;
}
}
Node *unions(Node *h1,Node *h2)//union operation
{
while(h1!=0)
{
Union=insert(Union,h1->value);
h1=h1->next;
}
while(h2!=0)
{
if(!compare(Union,h2->value))
Union=insert(Union,h2->value);
h2=h2->next;
}
return Union;
}
Node *intersection(Node *h1,Node *h2)//intersection operation
{
while(h1!=0)
{
if(compare(h2,h1->value))
Intersection=insert(Intersection,h1->value);
h1=h1->next;
}
return Intersection;
}
int compare(Node *head,int n)//comparing of two values in the lists
{
while(head!=NULL)
{
if(head->value==n)
{
return 1;
}
head=head->next;
}
return 0;
}
int main()
{
int i,total_first,total_second,firstlist_value,secondlist_value;
printf("Enter the total nodes in first list:");
fflush(stdout);
scanf("\n%d",&total_first);//getting no.of nodes in h1
printf("\nEnter the total nodes in second list:");
fflush(stdout);
scanf("\n%d",&total_second);//getting no.of nodes in h2
printf("\nenter the values of first list:");
fflush(stdout);
for(i=0;i<total_first;i++)
{
scanf("\n%d",&firstlist_value);
h1=insert(h1,firstlist_value);
}
printf("\nThe first list values are:");
display_list(h1);
printf("\nEnter the values of second list:");
fflush(stdout);
for(i=0;i<total_second;i++)
{
scanf("\n%d",&secondlist_value);
h2=insert(h2,secondlist_value);
}
printf("\nThe values of second list are:");
display_list(h2);
result_union=unions(h1,h2);
printf("\nThe union list is:");
display_list(result_union);
result_intersection=intersection(h1,h2);
printf("\nThe intersection list is:");
display_list(result_intersection);
return 0;
}
