#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct
Node {
int
data;
struct
Node* next;
};
typedef
struct
Node Node;
struct
LinkedList {
Node* head;
};
typedef
struct
LinkedList LinkedList;
void
printList(Node* head)
{
Node* temp = head;
while
(temp != NULL) {
printf
(
"%d "
, temp->data);
temp = temp->next;
}
printf
(
"\n"
);
}
void
push(Node** head,
int
new_data)
{
Node* new_node = (Node*)
malloc
(
sizeof
(Node));
if
(new_node == NULL) {
printf
(
"Memory allocation failed.\n"
);
exit
(1);
}
new_node->data = new_data;
new_node->next = *head;
*head = new_node;
}
void
append(Node** head,
int
new_data)
{
if
(*head == NULL) {
Node* new_node = (Node*)
malloc
(
sizeof
(Node));
if
(new_node == NULL) {
printf
(
"Memory allocation failed.\n"
);
exit
(1);
}
new_node->data = new_data;
new_node->next = NULL;
*head = new_node;
return
;
}
Node* n1 = *head;
Node* n2 = (Node*)
malloc
(
sizeof
(Node));
if
(n2 == NULL) {
printf
(
"Memory allocation failed.\n"
);
exit
(1);
}
n2->data = new_data;
n2->next = NULL;
while
(n1->next != NULL) {
n1 = n1->next;
}
n1->next = n2;
}
bool
isPresent(Node* head,
int
data)
{
Node* t = head;
while
(t != NULL) {
if
(t->data == data)
return
true
;
t = t->next;
}
return
false
;
}
LinkedList getIntersection(Node* head1, Node* head2)
{
int
hset[10000] = { 0 };
Node* n1 = head1;
Node* n2 = head2;
LinkedList result;
result.head = NULL;
while
(n1 != NULL) {
if
(hset[n1->data] == 0) {
hset[n1->data] = 1;
}
n1 = n1->next;
}
while
(n2 != NULL) {
if
(hset[n2->data] != 0) {
push(&(result.head), n2->data);
}
n2 = n2->next;
}
return
result;
}
LinkedList getUnion(Node* head1, Node* head2)
{
int
hmap[10000] = { 0 };
Node* n1 = head1;
Node* n2 = head2;
LinkedList result;
result.head = NULL;
while
(n1 != NULL) {
if
(hmap[n1->data] != 0) {
hmap[n1->data]++;
}
else
{
hmap[n1->data] = 1;
}
n1 = n1->next;
}
while
(n2 != NULL) {
if
(hmap[n2->data] != 0) {
hmap[n2->data]++;
}
else
{
hmap[n2->data] = 1;
}
n2 = n2->next;
}
for
(
int
i = 0; i < 10000; i++) {
if
(hmap[i] > 0) {
append(&(result.head), i);
}
}
return
result;
}
int
main()
{
LinkedList llist1, llist2, intersection, union_list;
llist1.head = NULL;
llist2.head = NULL;
intersection.head = NULL;
union_list.head = NULL;
push(&(llist1.head), 20);
push(&(llist1.head), 4);
push(&(llist1.head), 15);
push(&(llist1.head), 10);
push(&(llist2.head), 10);
push(&(llist2.head), 2);
push(&(llist2.head), 4);
push(&(llist2.head), 8);
intersection
= getIntersection(llist1.head, llist2.head);
union_list = getUnion(llist1.head, llist2.head);
printf
(
"First List is\n"
);
printList(llist1.head);
printf
(
"Second List is\n"
);
printList(llist2.head);
printf
(
"Intersection List is\n"
);
printList(intersection.head);
printf
(
"Union List is\n"
);
printList(union_list.head);
Node* temp;
while
(llist1.head != NULL) {
temp = llist1.head;
llist1.head = llist1.head->next;
free
(temp);
}
while
(llist2.head != NULL) {
temp = llist2.head;
llist2.head = llist2.head->next;
free
(temp);
}
while
(intersection.head != NULL) {
temp = intersection.head;
intersection.head = intersection.head->next;
free
(temp);
}
while
(union_list.head != NULL) {
temp = union_list.head;
union_list.head = union_list.head->next;
free
(temp);
}
return
0;
}