Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insert(struct Node** head_ref, int new_data);
int isPresent(struct Node* head, int data);
struct Node* Union(struct Node* head1, struct Node* head2)
{
struct Node* result = NULL;
struct Node *t1 = head1, *t2 = head2;
//printf("\nRESULT : %d",result);
while (t1 != NULL)
{
insert(&result, t1->data);
t1 = t1->next;
}
//printf("\nRESULT : %d",result);
while (t2 != NULL)
{
if (!isPresent(result, t2->data))
{
insert(&result, t2->data);
}
t2 = t2->next;
}
return result;
}
struct Node* Intersection(struct Node* head1, struct Node* head2)
{
struct Node* result = NULL;
struct Node* t1 = head1;
while (t1 != NULL)
{
if (isPresent(head2, t1->data))
{
insert(&result, t1->data);
}
t1 = t1->next;
}
return result;
}
void insert(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node* node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
int isPresent(struct Node* head, int data)
{
struct Node* t = head;
while (t != NULL)
{
if (t->data == data)
{
return 1;
}
t = t->next;
}
return 0;
}
int main()
{
int n,m,temp;
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersecn = NULL;
struct Node* unin = NULL;
printf("\nEnter the value of 1st linked list: ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter the value in link list1 %d:-",i);
fflush(stdout);
scanf("%d", &temp);
insert(&head1, temp);
}
printf("\nEnter the value of 2nd linked list: ");
scanf("%d",&m);
for(int i=0;i<m;i++)
{
printf("Enter the value in link list2 %d:-",i);
fflush(stdout);
scanf("%d", &temp);
insert(&head2, temp);
}
intersecn = Intersection(head1, head2);
unin = Union(head1, head2);
printf("\n\tFirst list is ");
printList(head1);
printf("\n\tSecond list is ");
printList(head2);
printf("\n\tIntersection list is ");
printList(intersecn);
printf("\n\tUnion list is ");
printList(unin);
return 0;
}