Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
/*
* Project Name: IntersecUnionProject.c
* Created on 16-10-2022
* Author: NOORUL AMEEN S M
*
*/
#include<stdio.h> /* Pre - Processor section */
#include<stdlib.h>
#include<stdbool.h>
struct node /* Structure Declaration for Node DATA and LINK */
{
int data;
struct node* link;
};
bool check(struct node* head, int data);
// To Insert all NODE DATA:
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 ->link = *head_ref;
*head_ref = new_node;
}
// To Display all NODE DATA:
void printLinkedList(struct node* p)
{
while(p != NULL)
{
printf("%d\t", p ->data);
p = p ->link;
}
}
// To get Same values of Data from Two NODES:
struct node* getIntersection(struct node* head1, struct node* head2)
{
struct node* result = NULL;
struct node* F_node = head1;
while(F_node != NULL)
{
if(check(head2, F_node ->data))
Insert(&result, F_node ->data);
F_node = F_node ->link;
}
return result;
}
// To get Combination of Data from Two NODES:
struct node* getUnion(struct node* head1, struct node* head2)
{
struct node* result = NULL;
struct node* F_node = head1;
struct node* S_Node = head2;
while(F_node != NULL)
{
Insert(&result, F_node ->data);
F_node = F_node ->link;
}
while(S_Node != NULL)
{
if(!check(result, S_Node ->data))
Insert(&result, S_Node ->data);
S_Node = S_Node ->link;
}
return result;
}
// To Inspect and Traverse into Two NODE to find SAME and COMBINATION of NODE DATA:
bool check(struct node* head, int data)
{
struct node* C_Node = head;
while(C_Node != NULL)
{
if(C_Node ->data == data)
return 1;
C_Node = C_Node ->link;
}
return 0;
}
// Main program:
int main()
{
int a,b,temp1,temp2;
struct node* First = NULL;
struct node* Second = NULL;
struct node* Intersection = NULL;
struct node* Union = NULL;
printf("\n ENTER NUMBER OF DATA IN FIRST NODE: \n");
scanf("%d",&a);
printf("\n ENTER FIRST NODE DATA: \n"); /* To get First node data from console: */
for(int i=0; i<a; i++)
{
scanf("%d",&temp1);
Insert(&First,temp1);
}
printf("\n ENTER NUMBER OF DATA IN SECOND NODE: \n");
scanf("%d",&b);
printf("\n ENTER SECOND NODE DATA: \n"); /* To get Second node data from console: */
for(int i=0; i<b; i++)
{
scanf("%d",&temp2);
Insert(&Second,temp2);
}
printf("\n FIRST LINKED LIST \n"); /* To display Data of FIRST NODE: */
printLinkedList(First);
printf("\n SECOND LINKED LIST \n"); /* To display Data of SECOND NODE: */
printLinkedList(Second);
Intersection = getIntersection(First, Second);
Union = getUnion(First, Second);
printf("\n INTERSECTION OF DATA OF TWO LINKED LIST \n"); /* To display Data of INTERSECTION NODE: */
printLinkedList(Intersection);
printf("\n UNION OF DATA OF TWO LINKED LIST \n"); /* To display Data of UNION NODE: */
printLinkedList(Union);
return 0;
}