Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Live batch 5
1)Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List.
Aim: To implement the interface for mathematical Set Theory. By giving two list of data as input to get the output as union & intersection for given list of data.
Soln:
Overview : In this project, the interfaces for mathematical Set Theory are implemented using C programming language.Here user can give the input like two list of data and this program will give the output as the union and intersection of these two lists.
Union is a special data type available in C programming language that allows to store different data types in the same memory location. Unions provide an efficient way of using the same memory location for multiple-purpose.
Intersection of two sets is the set in which only those element is present which are present in both set A and set B.
Program:
#include //preprocessor section
#include //preprocessor section
// Link list node
struct Node //structure declaration
{
int data; //structure member
struct Node* next;
};
//function declaration
// A utility function to insert a node at the beginning of a linked list
void push(struct Node** head_ref, int new_data) //called function
{
// allocate node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
// put new_data in the data
new_node->data = new_data;
// link the old list off the new node
new_node->next = (*head_ref);
// move the head to point to the new node
(*head_ref) = new_node;
}
// function that returns true if data is present in linked list else return false
int isPresent(struct Node* head, int data) //called function
{
struct Node* t = head; //structure pointer with the head
while (t != NULL)
{
if (t->data == data) //if condition
{
return 1;
}
t = t->next; //moving next to t
}
return 0; //return function as 0
}
// Function to get intersection of two linked lists head1 and head2
struct Node* getIntersection(struct Node* head1, struct Node* head2)
{
struct Node* result = NULL; //structure pointer with the empty list
struct Node* t1 = head1; //structure pointer with the empty list
// Traverse list1 and search each element of it in list2. If the element is present in list 2, then insert the element to result
while (t1 != NULL)
{
if (isPresent(head2, t1->data)) //caller function
{
push(&result, t1->data); //caller function
}
t1 = t1->next; //assigning next to t1
}
return result;
}
// Function to get union of two linked lists head1 and head2
struct Node* getUnion(struct Node* head1, struct Node* head2) //called function
{
struct Node* result = NULL; //structure pointer with the empty list
struct Node *t1 = head1, *t2 = head2; //structure pointer with the empty list
// Insert all elements of list1 to the result list
while (t1 != NULL)
{
push(&result, t1->data); //caller function
t1 = t1->next; //moving next to t
}
// Insert those elements of list2 which are not present in result list
while (t2 != NULL)
{
if (!isPresent(result, t2->data)) //caller function to check ispresent
{
push(&result, t2->data); //caller function for push function
}
t2 = t2->next;
}
return result; //return result
}
// function to print a linked list
void printList(struct Node* node) // called function
{
while (node != NULL)
{
printf("%d ", node->data); //print the value in the console
fflush(stdout); //clear the buffer
node = node->next; //moving next to node
}
}
int main() //main function
{//open the main loop
int x,y,arr; //variable declaration
printf("enter the number of value to set 1 linked list:"); //print the value in the console
fflush(stdout); //clear the buffer
scanf("%d", &x); //scan the value form console
printf("enter the number of value to set 2 linked list:"); //print the value in the console
fflush(stdout); //clear the buffer
scanf("%d", &y); //scan the value form console
// Start structure pointer with the empty list
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersect = NULL;
struct Node* unin = NULL;
printf("Enter the value for set 1 in link list1 :\n"); //print the value in the console
fflush(stdout); //clear the buffer
//for loop for link list1
for(int i=0;i<x;i++)
{
scanf("%d", &arr); //scan the set of value form console
push(&head1, arr); //caller function for pushing set elements 2
}
//for loop for link list2
printf("Enter the value for set 2 in link list2 :\n"); //print the value in the console
fflush(stdout); //clear the buffer
for(int i=0;i<y;i++)
{
scanf("%d", &arr); //scan the set of value form console
push(&head2, arr); //caller function for pushing set elements 2
}
//call the function of intersection
intersect = getIntersection(head1, head2); //caller function for Intersection
//call the function of union
unin = getUnion(head1, head2); //caller function for union
printf("\n Intersection list is "); //print the value in the console
fflush(stdout); //clear the buffer
printList(intersect); //caller function to print for Intersection
printf("\n Union list is "); //print the value in the console
fflush(stdout); //clear the buffer
printList(unin);//caller function to print for union
return 0; //return function
}
//close the main loop
Output:

Conclusion : Thus the Mathematical Set theory(union and intersection) using linked list is built and executed succesfully.