#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node
{
int value;
struct node *Next;
}Node;
Node *t1,*t2,*head,*tail, *Union, *Intersection, *result1,*result2,*headnode,*newnode;
void Insert(Node **head_ref, int new_data);
void display(Node *headnode);
void get_Union(Node *head1,Node *head2);
void get_Intersection(Node *head1,Node *head2);
int compare(Node *headdnode,int data);
void display(Node *headnode)
{
if(headnode==0)
{
printf("Empty list");
}
else
{
tail=headnode;
while(tail!=0)
{
printf("%d\n",tail->value);
tail=tail->Next;
}
}
}
void Insert(Node **head_ref, int new_data)
{
/* 1. allocate node */
Node* new_node = (Node*) malloc(sizeof(Node));
/* 2. put in the data */
new_node->value = new_data;
/* 3. Make next of new node as head */
new_node->Next = (*head_ref);
/* 4. move the head to point to the new node */
(*head_ref) = new_node;
}
void get_Union(Node *head1,Node *head2)
{
Node* result1 = (Node*) malloc(sizeof(Node));
Node* h1 = (Node*) malloc(sizeof(Node));
Node* h2 = (Node*) malloc(sizeof(Node));
h1=NULL;
h2=NULL;
h1 = head1;
h2 = head2;
result1=NULL;
while(h1!=NULL)
{
Insert(&result1,h1->value);
h1 = h1->Next;
}
while(h2!=NULL)
{
if(!(compare(result1, h2 -> value)))
{
Insert(&result1,h2->value);
printf("during");
printf("%d\n",result1->value);
}
h2 = h2->Next;
}
printf("The result of union:");
while(result1!=0)
{
printf("%d\n",result1->value);
result1=result1->Next;
}
}
int compare(Node *headdnode,int data)
{
while(headdnode!=NULL)
{
if(headdnode->value == data)
{
return 1;
}
headdnode=headdnode->Next;
}
return 0;
}
void get_Intersection(Node *head1,Node *head2)
{
Node* result2 = (Node*) malloc(sizeof(Node));
result2=NULL;
Node* h1 = (Node*) malloc(sizeof(Node));
h1=NULL;
h1=head1;
while(h1!=NULL)
{
if(compare(head2,h1->value))
Insert(&result2,h1->value);
h1 = h1->Next;
}
printf("The result of intersection:");
while(result2!=0)
{
printf("%d\n",result2->value);
result2=result2->Next;
}
}
int main()
{
int total_first,total_second,first_list_value,second_list_value,i;
printf("Total nodes in first list:\n");
fflush(stdout);
scanf("%d",&total_first);
printf("Enter the %d elements ",total_first);
for(i=0;i<total_first;i++)
{ fflush(stdout);
scanf("%d",&first_list_value);
Insert(&t1,first_list_value);
}
printf("Elements in first list:\n");
display(t1);
printf("Total nodes in second list:\n");
fflush(stdout);
scanf("%d",&total_second);
printf("Enter the %d elements :",total_second);
for(i=0;i<total_second;i++)
{
fflush(stdout);
scanf("%d",&second_list_value);
Insert(&t2,second_list_value);
}
printf("Elements in second list:\n");
display(t2);
get_Union(t1,t2);
get_Intersection(t1,t2);
return 0;
}
